Skip to content
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

Compilation error with GCC #33

Open
mohammad-akhlaghi opened this issue Oct 4, 2021 · 6 comments · May be fixed by #38
Open

Compilation error with GCC #33

mohammad-akhlaghi opened this issue Oct 4, 2021 · 6 comments · May be fixed by #38

Comments

@mohammad-akhlaghi
Copy link

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.

@josephholsten
Copy link
Contributor

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?

@mohammad-akhlaghi
Copy link
Author

Please go ahead @josephholsten 😉...

@mohammad-akhlaghi
Copy link
Author

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.

@mohammad-akhlaghi
Copy link
Author

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 😉.

@josephholsten
Copy link
Contributor

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!

@mralusw
Copy link

mralusw commented Dec 24, 2022

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.

@mralusw mralusw linked a pull request Dec 24, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants