Skip to content

[C-libcurl] Heap-allocate boolean primitive list items - #25632

Open
hirishh wants to merge 4 commits into
OpenAPITools:masterfrom
hirishh:c-libcurl-bool-list-fix
Open

hirishh wants to merge 4 commits into
OpenAPITools:masterfrom
hirishh:c-libcurl-bool-list-fix

Conversation

@hirishh

@hirishh hirishh commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

cc @zhemant @ityuhui @michelealbano @eafer (C technical committee)

Problem

For an array of booleans (type: array, items: {type: boolean}), the C-libcurl
deserializer adds each item with the raw int value:

list_addElement({{name}}List, {{name}}_local->valueint);

list_addElement takes a pointer, so the generated code does not compile under
-Werror=int-conversion. The generated _free also calls free() on every list
entry, and _convertToJSON reads each entry as *(cJSON_bool *)data, so a cast
alone would not be correct.

The same line exists in two templates:

  • model-body.mustache (default output)
  • model-body-v7_20_0.mustache (used with declareNumberBooleanWithoutPointer=true)

Real-world trigger: Discovered this issue while building the c library for Kubernetes 1.37 spec (DeviceAttribute.bools). It failed to build before this fix.

Fix

Heap-allocate each item, as the numeric branch already does, in both templates:

cJSON_bool *{{name}}_local_value = calloc(1, sizeof(cJSON_bool));
if(!{{name}}_local_value) { goto end; }
*{{name}}_local_value = {{name}}_local->valueint;
list_addElement({{name}}List, {{name}}_local_value);

Testing

  • Flags schema with a boolean array added to 2_0/c/petstore.yaml.
  • The c and c-useJsonUnformatted samples build in CI with -Werror=int-conversion, so the
    generated model is compiled there. Both builds fail with the previous template (checked).
  • samples/client/petstore/c/unit-tests/manual-flags.c: round trip (parse, inspect, serialize,
    free). Passes with the fix.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fixes generated C-libcurl code for boolean array items so it compiles under -Werror=int-conversion and matches how numeric items are handled.

  • Boolean array items are heap-allocated as cJSON_bool pointers before being added to the list, in both model-body.mustache and model-body-v7_20_0.mustache.
  • Adds a Flags schema with a boolean array to the C petstore spec and includes a manual round-trip test.
  • Fixes a leak where a parse error after a boolean item left the item unfreed, and makes the v7_20_0 template return NULL when allocation fails instead of skipping cleanup.
  • The manual test's failing-parse case stores the result before asserting, so the error path runs even under NDEBUG.

Written for commit 467cf6e. Summary will update on new commits.

View guided diff

Boolean array items were added to the list with the raw int value
(valueint). list_addElement takes a pointer, so the generated code does
not compile under -Werror=int-conversion. The generated _free functions
call free() on every list entry, and _convertToJSON reads each entry as
*(cJSON_bool *)data, so a cast would not be correct either.

Store a heap-allocated cJSON_bool, as the numeric branch already does.
Applies to model-body.mustache and model-body-v7_20_0.mustache (used
with declareNumberBooleanWithoutPointer=true).
Add a Flags schema with a boolean array to the C petstore spec. The
sample builds (c and c-useJsonUnformatted) compile the generated model
with -Werror=int-conversion, which fails on the previous template.
Add a manual round-trip test in samples/client/petstore/c/unit-tests.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 18 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread samples/client/petstore/c/model/flags.c Resolved
- The end: cleanup freed string and numeric list items but not boolean
  ones, so a parse error after a boolean item leaked it.
- The unformatted template returned the create_internal result directly,
  skipping the end: cleanup when the allocation failed.
- Regenerate c and c-useJsonUnformatted samples; add a failing-parse case
  to manual-flags.c (valgrind: 4 bytes leaked before, none after).

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 15 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/C-libcurl/model-body-v7_20_0.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/C-libcurl/model-body-v7_20_0.mustache:1031">
P3: On the new create-failure path, the `end:` cleanup doesn't free the `strdup`'d string/date/binary fields that were passed to `_create_internal`, so they leak when allocation fails. This predates the PR (the sibling `model-body.mustache` has the same gap), so it can be left for a follow-up.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

With NDEBUG the assert and the parse call are removed, so the error path
would not run. Store the result before checking it.
@hirishh

hirishh commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

cc @zhemant @ityuhui @michelealbano @eafer (C technical committee)

This branch has not been deployed

No deployments
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