plougher / squashfs-tools

tools to create and extract Squashfs filesystems

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

%ld used for time_t

Emill opened this issue · comments

commented

I get a compile warning for this line in unsquashfs.c:

res = dprintf(writer_fd, "%s %c %ld %o %s %s", filename, type, inode->time, inode->mode & ~S_IFMT, userstr, groupstr);

that %ld expects long int but the argument is of type time_t (long long int).

My system is 32-bit but has 64-bit time_t.

I get no other warnings when compiling squashfs-tools than this line.

Traditionally time_t is a "long int" which matches "%ld". On 32-bit systems both are 32-bit, and on 64-bit systems both are 64-bit.

The issue is that there are recent efforts to make time_t 64-bit on 32-bit systems. For example recent versions of glibc support 64-bit time_t on 32-bit systems if _TIME_BITS=64.

This breaks the previous fact that time_t always matches "%ld". No other code will be affected by this change because the compiler/glibc will automatically switch to the 64-bit APIs, and "long int"s are not used elsewhere.

As it is Undefined Behaviour to specify the wrong type in printf, may I suggest the following?

res = dprintf(writer_fd, "%s %c %lld %o %s %s", filename, type, (long long int) inode->time, inode->mode & ~S_IFMT, userstr, groupstr);

Alternatively, explicitly cast to int64_t and use the PRId64 macro from inttypes.h.

Oh nice, you fixed it at the same time I was commenting :)