Skip to content

fix c-libcurl generator for int and boolean values by moving to int*#23052

Open
hirishh wants to merge 6 commits intoOpenAPITools:masterfrom
hirishh:fix-c-liburcl-int-boolean
Open

fix c-libcurl generator for int and boolean values by moving to int*#23052
hirishh wants to merge 6 commits intoOpenAPITools:masterfrom
hirishh:fix-c-liburcl-int-boolean

Conversation

@hirishh
Copy link
Contributor

@hirishh hirishh commented Feb 25, 2026

This is a follow up PR about #15782

FYI C coommittee @zhemant (2018/11) @ityuhui (2019/12) @michelealbano (2020/03)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • 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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • 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

Switch C-libcurl model generation to int*/long*/bool* for optional fields, with deep-copying in create(), pointer dereferencing in JSON, and full error‑path cleanup to resolve memory leaks. Regenerated petstore samples (c and useJsonUnformatted) and fixed manual unit tests.

  • Bug Fixes

    • Numeric/boolean fields generated as pointers; structs zero‑initialized and _library_owned set early; create() accepts pointers and deep‑copies with malloc‑failure guards; free() releases numeric/boolean pointers and NULLs them.
    • JSON: dereference pointers when serializing; parse allocates numeric/boolean pointers, duplicates strings/uuids/emails/dates/dateTimes/byte arrays, and frees all locals on any error or failed create_internal.
    • Petstore samples regenerated; manual tests updated to pass addresses and dereference in assertions.
  • Migration

    • Models: numeric/boolean fields become pointers (e.g., long* id, int* quantity, int* complete).
    • Callers: pass addresses to create() and dereference when reading (e.g., *order->quantity). Inputs remain caller‑owned; create() copies values; use model_free to free allocated numeric/boolean pointers.

Written for commit f79341f. Summary will update on new commits.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

24 issues found across 37 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/petstore/c-useJsonUnformatted/model/tag.c">

<violation number="1" location="samples/client/petstore/c-useJsonUnformatted/model/tag.c:20">
P2: Allocation failure path leaks tag_local_var because tag_free returns early before _library_owned is set.</violation>

<violation number="2" location="samples/client/petstore/c-useJsonUnformatted/model/tag.c:126">
P2: Memory leak: tag_parseFromJSON allocates id_local_var, but tag_create_internal copies the value into a new allocation and does not take ownership, so id_local_var is never freed on the success path.</violation>
</file>

<file name="samples/client/petstore/c/model/pet.c">

<violation number="1" location="samples/client/petstore/c/model/pet.c:41">
P2: Calling pet_free before _library_owned is set causes an early return, leaking pet_local_var on id allocation failure.</violation>

<violation number="2" location="samples/client/petstore/c/model/pet.c:225">
P2: id_local_var is allocated in pet_parseFromJSON and passed to pet_create_internal, which copies it into a new allocation; the original id_local_var is never freed on the success path, leaking memory each parse.</violation>
</file>

<file name="samples/client/petstore/c/model/api_response.c">

<violation number="1" location="samples/client/petstore/c/model/api_response.c:21">
P2: Allocation failure leaks api_response_local_var because api_response_free returns early when _library_owned is still zeroed.</violation>
</file>

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

<violation number="1" location="modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache:212">
P2: Allocation failure path now calls {{classname}}_free before _library_owned is set, so cleanup is skipped and the partially built object leaks.</violation>

<violation number="2" location="modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache:784">
P2: Temporary numeric/boolean allocations in parseFromJSON are never freed on success; create_internal copies values into newly allocated memory, leaving name_local_var leaked.</violation>
</file>

<file name="samples/client/petstore/c-useJsonUnformatted/model/api_response.c">

<violation number="1" location="samples/client/petstore/c-useJsonUnformatted/model/api_response.c:21">
P2: Leak on code allocation failure because api_response_free returns early before _library_owned is set.</violation>

<violation number="2" location="samples/client/petstore/c-useJsonUnformatted/model/api_response.c:120">
P2: code_local_var is allocated in api_response_parseFromJSON but never freed on the success path after api_response_create_internal copies the value, causing a memory leak for each parsed response with a code.</violation>
</file>

<file name="samples/client/petstore/c-useJsonUnformatted/model/order.c">

<violation number="1" location="samples/client/petstore/c-useJsonUnformatted/model/order.c:41">
P2: Allocation failure calls order_free before _library_owned is set, so order_free returns early and leaks the partially allocated order object and fields.</violation>
</file>

<file name="samples/client/petstore/c-useJsonUnformatted/model/category.c">

<violation number="1" location="samples/client/petstore/c-useJsonUnformatted/model/category.c:20">
P2: Leak on id allocation failure: category_free returns early because _library_owned is still 0, so category_local_var is not freed on this error path.</violation>

<violation number="2" location="samples/client/petstore/c-useJsonUnformatted/model/category.c:126">
P2: id_local_var is allocated in category_parseFromJSON and passed to category_create_internal, which copies the value into a new allocation. The original id_local_var is never freed on the success path, so each successful parse leaks memory.</violation>
</file>

<file name="samples/client/petstore/c/model/user.c">

<violation number="1" location="samples/client/petstore/c/model/user.c:28">
P2: user_free is called on allocation failure before _library_owned is set, so user_free returns early and leaks user_local_var (and any allocated fields) on those error paths.</violation>
</file>

<file name="samples/client/petstore/c/model/order.c">

<violation number="1" location="samples/client/petstore/c/model/order.c:37">
P2: Allocation failure paths call order_free before _library_owned is set, so order_free returns early and leaks order_local_var (and any allocated fields).</violation>

<violation number="2" location="samples/client/petstore/c/model/order.c:212">
P2: Temporary allocations in order_parseFromJSON are leaked on success: id/pet_id/quantity/complete are heap-allocated, order_create_internal copies their values into new allocations, and the locals are never freed before returning on the success path.</violation>
</file>

<file name="samples/client/petstore/c-useJsonUnformatted/model/pet.c">

<violation number="1" location="samples/client/petstore/c-useJsonUnformatted/model/pet.c:41">
P2: pet_create_internal calls pet_free before _library_owned is set, so pet_free early-returns and leaks pet_local_var on id allocation failure.</violation>

<violation number="2" location="samples/client/petstore/c-useJsonUnformatted/model/pet.c:225">
P2: `id_local_var` is heap-allocated in `pet_parseFromJSON` and passed to `pet_create_internal`, which copies it into a new allocation. The original allocation is never freed on the success path (or when create returns NULL), causing a leak.</violation>
</file>

<file name="samples/client/petstore/c-useJsonUnformatted/model/user.c">

<violation number="1" location="samples/client/petstore/c-useJsonUnformatted/model/user.c:28">
P3: user_free is invoked on allocation failures before _library_owned is set, so user_free returns early and leaks user_local_var (and any assigned fields) on those error paths.</violation>
</file>

<file name="samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c">

<violation number="1" location="samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c:20">
P2: Error path leak: MappedModel_free returns early because _library_owned is still 0 when another_property allocation fails, so the allocated struct is never freed.</violation>

<violation number="2" location="samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c:126">
P2: Memory leak: another_property_local_var allocated in parseFromJSON is not freed on the success path after MappedModel_create_internal copies the value into a new allocation.</violation>
</file>

<file name="samples/client/petstore/c/model/mapped_model.c">

<violation number="1" location="samples/client/petstore/c/model/mapped_model.c:20">
P3: Allocation failure path leaks MappedModel_local_var because MappedModel_free returns early when _library_owned is still 0.</violation>

<violation number="2" location="samples/client/petstore/c/model/mapped_model.c:104">
P2: Temporary allocation `another_property_local_var` in `MappedModel_parseFromJSON` is never freed on the success path. Since `MappedModel_create_internal` makes its own heap copy, this allocation leaks on every successful parse.</violation>
</file>

<file name="samples/client/petstore/c/model/tag.c">

<violation number="1" location="samples/client/petstore/c/model/tag.c:20">
P2: Leak on id allocation failure: tag_free returns early because _library_owned is still 0, so tag_local_var is never freed.</violation>

<violation number="2" location="samples/client/petstore/c/model/tag.c:104">
P2: id_local_var is allocated in tag_parseFromJSON and passed to tag_create_internal, which now copies it into a new allocation. The original id_local_var is never freed on success, causing a memory leak.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@hirishh
Copy link
Contributor Author

hirishh commented Feb 25, 2026

@cubic-dev-ai re-run a review

@cubic-dev-ai
Copy link
Contributor

cubic-dev-ai bot commented Feb 25, 2026

@cubic-dev-ai re-run a review

@hirishh I have started the AI code review. It will take a few minutes to complete.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

4 issues found across 37 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/petstore/c-useJsonUnformatted/model/tag.c">

<violation number="1" location="samples/client/petstore/c-useJsonUnformatted/model/tag.c:123">
P2: tag_parseFromJSON leaks id_local_var and the duplicated name when tag_create_internal fails, because there’s no NULL check and cleanup after the call.</violation>
</file>

<file name="samples/client/petstore/c-useJsonUnformatted/model/category.c">

<violation number="1" location="samples/client/petstore/c-useJsonUnformatted/model/category.c:123">
P3: Memory leak on category_create_internal failure: id_local_var and duplicated name are not freed if allocation of category_local_var fails.</violation>
</file>

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

<violation number="1" location="modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache:295">
P2: {{classname}}_create allocates numeric/boolean copies but never frees them if {{classname}}_create_internal fails, causing a leak on allocation failure.</violation>

<violation number="2" location="modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache:782">
P2: {{classname}}_parseFromJSON returns immediately after {{classname}}_create_internal without freeing allocated numeric/boolean locals if the create fails, leaking memory on allocation failure.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@hirishh
Copy link
Contributor Author

hirishh commented Feb 25, 2026

@cubic-dev-ai re-run a review again :) (you are a smart ai bot)

@cubic-dev-ai
Copy link
Contributor

cubic-dev-ai bot commented Feb 25, 2026

@cubic-dev-ai re-run a review again :) (you are a smart ai bot)

@hirishh I have started the AI code review. It will take a few minutes to complete.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

3 issues found across 37 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/petstore/c/model/api_response.c">

<violation number="1" location="samples/client/petstore/c/model/api_response.c:160">
P2: Error path leaks the strdup’d type/message strings when api_response_create_internal fails; only code_local_var is freed at end.</violation>
</file>

<file name="samples/client/petstore/c/model/category.c">

<violation number="1" location="samples/client/petstore/c/model/category.c:131">
P2: Memory leak: on category_create_internal failure, the strdup’d name is not freed before jumping to end, so the allocation leaks.</violation>
</file>

<file name="samples/client/petstore/c/model/pet.c">

<violation number="1" location="samples/client/petstore/c/model/pet.c:331">
P2: Memory leak on error path: strdup(name->valuestring) is not freed if pet_create_internal fails and goto end is taken.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@hirishh
Copy link
Contributor Author

hirishh commented Feb 25, 2026

@cubic-dev-ai re-run a review

@cubic-dev-ai
Copy link
Contributor

cubic-dev-ai bot commented Feb 25, 2026

@cubic-dev-ai re-run a review

@hirishh I have started the AI code review. It will take a few minutes to complete.

@hirishh hirishh force-pushed the fix-c-liburcl-int-boolean branch from 61bb901 to f79341f Compare February 25, 2026 03:04
@hirishh
Copy link
Contributor Author

hirishh commented Feb 25, 2026

@cubic-dev-ai re-run a review

@cubic-dev-ai
Copy link
Contributor

cubic-dev-ai bot commented Feb 25, 2026

@cubic-dev-ai re-run a review

@hirishh I have started the AI code review. It will take a few minutes to complete.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 37 files

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