nothings / stb

stb single-file public domain libraries for C/C++
https://twitter.com/nothings
Other
26.31k stars 7.69k forks source link

stb_sprintf gcc warning when using -Wextra #1480

Open dnotq opened 1 year ago

dnotq commented 1 year ago

Describe the bug

When compiling with gcc.exe (Rev6, Built by MSYS2 project) 13.1.0 using -Wextra, the following error is produced:

./stb/stb_sprintf.h: In function 'stbsp_vsprintfcb':
./stb/stb_sprintf.h:594:51: warning: operand of '?:' changes signedness from 'int' to 'unsigned int' due to unsignedness of other operand [-Wsign-compare]
  594 |          l = stbsp__strlen_limited(s, (pr >= 0) ? pr : ~0u);
      |                                                   ^~

To Reproduce

Write a simple "Hello, World" program using stb_sprintf and compile with gcc using -Wall and -Wextra options.

Clang does not produce the warning for the same code and compiler options.

The simple fix is to change line 594 (cast pr):

-         l = stbsp__strlen_limited(s, (pr >= 0) ? pr : ~0u);
+         l = stbsp__strlen_limited(s, (pr >= 0) ? (stbsp__uint32)pr : ~0u);