flock
flock copied to clipboard
Compilation error with GCC
When building flock 0.4.0 with GCC 11.1.0, I get the following compilation error:
src/flock.c: In function ‘main’:
src/flock.c:323:32: error: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘__suseconds_t’ {aka ‘long int’} [-Werror=format=]
323 | printf("took %1u microseconds\n", (t_l_acq.tv_usec - t_l_req.tv_usec)); // not adding due to time constraints
| ~~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| unsigned int __suseconds_t {aka long int}
| %1lu
cc1: all warnings being treated as errors
As the compiler recommends, it can be fixed by simply replacing %1u
with %1lu
. After making this change Flock was successfully built on my system.
A brief search shows me this is at least correct for Linux. Need to confirm this is correct for illumos and bsd system types. @mohammad-akhlaghi want to draft the changeset for this, or shall I?
Please go ahead @josephholsten :wink:...
With the help of Zahra Sharbaf, we recently discovered that simply replacing %1u
with %1lu
is not a generic solution and will cause a crash on macOS (using LLVM compiler)! The more generic solution is to replace it with %ld
(as the error message in my first comment also reports, the actual type is: long int
). I have tested this fix with GCC and it fixes the problem.
There was a mistake in my previous message: on macOS, the format is int
, not long int
! So this is what our building script looks like now (modifying the source of flock after unpacking it to build safely on GNU/Linux and macOS, I can now confirm that it works in both cases):
case $on_mac_os in
yes) sed -e's/\%1u/\%d/' src/flock.c > src/flock-new.c;;
no) sed -e's/\%1u/\%ld/' src/flock.c > src/flock-new.c;;
*) echo "pre-make-build.sh: '$on_mac_os' unrecognized value for on_mac_os";;
esac
It would be great if this can be taken into the source of flock for future versions so we can remove this extra manual correction :wink:.
Well, I don’t love putting this into the build script, but fixed in an ugly way is better than broken! I’m sorry I’m not really able to draft this in the near future. I’m giving you a commit bit, please push us a PR!
This never got fixed, apparently. The proper fix, in my opinion, is to cast the result to either long
or unsigned long
, and use either %1ld
or (respectively) %1lu
. This would be portable, safe and non-hacky — there's no need to identify platforms.