stb_sprintf gcc warning when using -Wextra

Open Beginner friendly
#38 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
60/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
c
Domain
tooling

Research direction

The warning comes from the ?: expression passed to stbsp__strlen_limited at line 594 of stb/stb_sprintf.h, inside stbsp_vsprintfcb. Reproduce it by compiling a small program that includes the header with gcc -Wall -Wextra. Done means the -Wsign-compare warning no longer appears under gcc with those flags, and clang still builds cleanly.

Written by the indexing model from the issue text.

Description

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);
Dominant language
C
Stars
18
Forks
5
Avg merge
1m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nothings/stb_sprintf

All issues in nothings/stb_sprintf

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.