Mikejmoffitt / mdk

Barebones megadrive development setup.
33 stars 5 forks source link

binclude: fix printf and snprintf calls #1

Closed pinobatch closed 5 years ago

pinobatch commented 5 years ago

Because int and size_t are different types, GCC's printf format string checking warns when attempting to print one as the other.

util/binclude.c: In function ‘main’:
util/binclude.c:84:17: warning: format ‘%d’ expects argument of type
    ‘int’, but argument 2 has type ‘size_t {aka long unsigned int}’
    [-Wformat=]
  printf("Wrote %d bytes to \"%s\".\n", bytes_written, out_fpath);
                ~^
                %ld

So instead of %d which means int, use C99's %zu which means size_t. https://stackoverflow.com/q/2524611/2738262

But when I made this change, binclude didn't automatically rebuild:

$ make util/binclude
make: 'util/binclude' is up to date.

I found and fixed a missing prerequisite in the makefile.

Finally, snprintf does not NUL terminate the result if the result is exactly as long as the buffer. Work around this by passing 1 less than the length of the buffer and explicitly write the terminator.