tinyprintf icon indicating copy to clipboard operation
tinyprintf copied to clipboard

Use "unsigned char:1" for flags

Open pengi opened this issue 2 years ago • 0 comments

"char" is treated, at least in many cases, as signed values. Having that field as 1 bit means it can be treated as one sign bit, no data bits. Thus only values 0 and -1 will be accepted

It seems to differ between versions of the compiler. But in any case, explicitly defining it as unsigned means only the values 0 and 1 will be accepted, and therefore there shouldn't be any issues between compiler versions.

This is a fix for an issue I had using:

gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

Was compiling with flags:

gcc -g -O3 -Wdouble-promotion -Wall -Werror -Wpedantic

And the errors I got was:

src/vendor/tinyprintf/tinyprintf.c: In function ‘tfp_format’:
src/vendor/tinyprintf/tinyprintf.c:282:36: error: overflow in conversion from ‘int’ to ‘signed char:1’ changes value from ‘1’ to ‘-1’ [-Werror=overflow]
  282 |                     p.align_left = 1;
      |                                    ^
src/vendor/tinyprintf/tinyprintf.c:285:28: error: overflow in conversion from ‘int’ to ‘signed char:1’ changes value from ‘1’ to ‘-1’ [-Werror=overflow]
  285 |                     p.lz = 1;
      |                            ^
src/vendor/tinyprintf/tinyprintf.c:288:29: error: overflow in conversion from ‘int’ to ‘signed char:1’ changes value from ‘1’ to ‘-1’ [-Werror=overflow]
  288 |                     p.alt = 1;
      |                             ^
src/vendor/tinyprintf/tinyprintf.c:305:22: error: overflow in conversion from ‘int’ to ‘signed char:1’ changes value from ‘1’ to ‘-1’ [-Werror=overflow]
  305 |               p.lz = 1;  /* zero-padding */
      |                      ^
src/vendor/tinyprintf/tinyprintf.c:374:25: error: overflow in conversion from ‘int’ to ‘signed char:1’ changes value from ‘1’ to ‘-1’ [-Werror=overflow]
  374 |                 p.alt = 1;
      |                         ^

This patch should solve it.

I've been trying to also add the corresponding warning to the make file, but can't seem to reproduce it in tinyprintfs build environment.

pengi avatar Feb 14 '23 14:02 pengi