bvschaik / julius

An open source re-implementation of Caesar III
GNU Affero General Public License v3.0
2.79k stars 311 forks source link

Fix a printf format string signedness warning #724

Closed sulix closed 6 months ago

sulix commented 6 months ago

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. :-)