-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix compiler warning about comparison of integers of different signs #805
Conversation
Here's the error: ``` libarchive/archive_write_disk_posix.c:3869:19: error: comparison of integers of different signs: 'unsigned long' and 'time_t' (aka 'long') [-Werror,-Wsign-compare] if (st->st_mtime < archive_entry_mtime(entry)) ~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~ libarchive/archive_write_disk_posix.c:3872:19: error: comparison of integers of different signs: 'unsigned long' and 'time_t' (aka 'long') [-Werror,-Wsign-compare] if (st->st_mtime > archive_entry_mtime(entry)) ~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~ 2 errors generated. ``` st->st_mtime is an unsigned long on this platform while time_t is a long, so converting an unsigned long into a long might lose some precision, but on the other side this is the only object for which we know the type without having to know about portability. Android seems to be the only platform where time_t is an unsigned long. https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/+/jb-dev/sysroot/usr/include/asm/stat.h
My willingness to workaround clearly broken platforms is limited. This is one of those cases I really, really dislike. |
Yep ... Maybe the -W flag can be overridden so that this error is silenced when building for Android ? I'm not sure what can be done for that problem, but in the current state Android does not build. I can easily workaround that on my local build copy of libarchive but other Android users might run into this problem. |
Yes, this does look like a bug in Android. The problem isn't that
To compare them correctly, we need a common type. The best candidate is probably I think the following might help:
Note this has to be a macro because we don't actually know the type of the input, only that it's numeric. The above assumes that the input is an integer type of no more than 64 bits. If the number is less than zero, This should allow us to do things like:
|
@@ -4044,10 +4044,10 @@ older(struct stat *st, struct archive_entry *entry) | |||
{ | |||
/* First, test the seconds and return if we have a definite answer. */ | |||
/* Definitely older. */ | |||
if (st->st_mtime < archive_entry_mtime(entry)) | |||
if ((time_t) st->st_mtime < archive_entry_mtime(entry)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still broken: if st->st_mtime
is out of range, then casting will change the meaning here.
See my comments in the bug about an approach that might actually do the right thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also a style error here: Please don't put a space between a cast and the value.
Thanks for the detailed explanation and proper solution to the problem @kientzle |
Btw tell me if you want the comment to be reworked/rewritten, it might not be perfectly clear. |
Please make sure the comment has a reference to Issue #805 on github so that people who are confused by this non-obvious logic can read the full discussion. |
Will do. On Fri, Oct 14, 2016 at 11:29 PM, Tim Kientzle notifications@github.com
|
Here's the error:
st->st_mtime is an unsigned long on this platform while time_t is a long, so converting an unsigned long into a long might lose some precision, but on the other side this is the only object for which we know the type without having to know about portability.
Android seems to be the only platform where time_t is an unsigned long.
https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/+/jb-dev/sysroot/usr/include/asm/stat.h