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

Fix a printf format string signedness warning #724

Merged
merged 1 commit into from
Mar 1, 2024

Conversation

sulix
Copy link
Contributor

@sulix sulix commented Feb 25, 2024

gcc 13.2 outputs several warnings for mismatched printf format strings, such as:

/home/david/Development/julius/test/sav/sav_compare.c: In function ‘compare_part’: /home/david/Development/julius/test/sav/sav_compare.c:522:45: warning: format ‘%X’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int’ [-Wformat=]
  522 |                 printf("record %d offset 0x%X", i / save_game_parts[index].record_length, i % save_game_parts[index].record_length);
      |                                            ~^                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                             |                                               |
      |                                             unsigned int                                    int
      |                                            %X

This patch doesn't fix all of them, but does fix the simple ones. The more complicated ones include signedness issues with '%X' in the sav_compare test, which would either require a rework of the signedness of a whole bunch of code, or a hacky cast:

/home/david/Development/julius/test/sav/sav_compare.c: In function ‘compare_part’:
/home/david/Development/julius/test/sav/sav_compare.c:522:45: warning: format ‘%X’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int’ [-Wformat=]
  522 |                 printf("record %d offset 0x%X", i / save_game_parts[index].record_length, i % save_game_parts[index].record_length);
      |                                            ~^                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                             |                                               |
      |                                             unsigned int                                    int
      |                                            %X

There's also a warning about the format string not being a literal in the screenshot code, which would probably require a more clever fix:

/home/david/Development/julius/src/graphics/screenshot.c: In function ‘generate_filename’:
/home/david/Development/julius/src/graphics/screenshot.c:103:55: warning: format not a string literal, format string not checked [-Wformat-nonliteral]
  103 |     strftime(filename, FILE_NAME_MAX, filename_formats[city_screenshot], loctime);
      |                                       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

All of these are pretty harmless, so it's not particularly important that all (or any) of them actually get fixed. But it's nice to remove a warning if we can. :-)

gcc 13.2 outputs several warnings for mismatched printf format strings, such as:

/home/david/Development/julius/test/sav/sav_compare.c: In function ‘compare_part’:
/home/david/Development/julius/test/sav/sav_compare.c:522:45: warning: format ‘%X’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int’ [-Wformat=]
  522 |                 printf("record %d offset 0x%X", i / save_game_parts[index].record_length, i % save_game_parts[index].record_length);
      |                                            ~^                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                             |                                               |
      |                                             unsigned int                                    int
      |                                            %X

This patch doesn't fix all of them, but does fix the simple ones.

Signed-off-by: David Gow <david@ingeniumdigital.com>
Copy link
Owner

@bvschaik bvschaik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's nice to remove a warning if we can. :-)

Exactly :-)

The ones in sav_compare are not important since that code is only used in tests. I'm not sure how we can fix the screenshot one. And the two you fixed in this PR are good to fix indeed. Thanks!

@bvschaik bvschaik merged commit af7996c into bvschaik:master Mar 1, 2024
13 checks passed
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 this pull request may close these issues.

2 participants