fix: use %zu/size_t for message count in C example#6086
Open
prql-bot wants to merge 1 commit into
Open
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Nightly survey finding. The
minimal-cexample printed the error count with%ldand iterated the message array with anintloop counter, butCompileResult.messages_lenis asize_t.On LP64 (Linux/macOS)
longandsize_tare both 64-bit, so this happens to work — which is why it went unnoticed. On LLP64 (Windows x64)longis 32-bit whilesize_tis 64-bit, so%ldis a width/signedness mismatch: passing asize_twhere alongis expected is undefined behavior and can print a garbage error count. Theint i < res.messages_lenloop is also a signed/unsigned comparison.Fix: use
%zuand asize_tloop 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-Wformatand a-Wsign-compare).No automated regression test: the
minimal-cexample is built by its ownMakefile(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 -Wextracompile above is the practical check.