Skip to content

fix: use %zu/size_t for message count in C example#6086

Open
prql-bot wants to merge 1 commit into
mainfrom
fix/c-example-size-t-format
Open

fix: use %zu/size_t for message count in C example#6086
prql-bot wants to merge 1 commit into
mainfrom
fix/c-example-size-t-format

Conversation

@prql-bot

Copy link
Copy Markdown
Collaborator

Nightly survey finding. The minimal-c example printed the error count with %ld and iterated the message array with an int loop counter, but CompileResult.messages_len is a size_t.

On LP64 (Linux/macOS) long and size_t are both 64-bit, so this happens to work — which is why it went unnoticed. On LLP64 (Windows x64) long is 32-bit while size_t is 64-bit, so %ld is a width/signedness mismatch: passing a size_t where a long is expected is undefined behavior and can print a garbage error count. The int i < res.messages_len loop is also a signed/unsigned comparison.

Fix: use %zu and a size_t loop counter, both correct on every platform.

The example is copy-paste starter code for C consumers of prqlc-c, so getting the idiom right matters.

Verified with gcc -fsyntax-only -Wall -Wextra — compiles cleanly with no warnings (the previous version emitted a -Wformat and a -Wsign-compare).

No automated regression test: the minimal-c example is built by its own Makefile (not compiled in CI with -Werror), and the misbehavior only manifests at runtime on LLP64 targets, so there's no CI harness to assert against. The clean -Wall -Wextra compile above is the practical check.

`res.messages_len` is a `size_t`, but main.c printed it with `%ld` and
iterated with an `int` loop counter. On LLP64 platforms (Windows x64)
`long` is 32-bit while `size_t` is 64-bit, so the format specifier is a
width/signedness mismatch (undefined behavior, can print a garbage count).
Use `%zu` and a `size_t` counter, which are correct on every platform.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant