Skip to content

[BUG][C] intToStr() macro is broken #20376

Closed
@imaami

Description

@imaami

The generated C client code defines this macro:

#define intToStr(dst, src) \
    do {\
    char dst[256];\
    snprintf(dst, 256, "%ld", (long int)(src));\
}while(0)

This is broken because the destination array goes out of scope before it can be used. The generated code shows clearly that there is shadowing going on. This is what the result looks like after macro expansion:

~/git/openapi-generator$ cpp -P samples/client/petstore/c/api/PetAPI.c | grep -A2 localVarBuff_petId | head -3 | clang-format --assume-filename=x.c -
char localVarBuff_petId[256];
do {
	char localVarBuff_petId[256];
	snprintf(localVarBuff_petId, 256, "%ld", (long int)(petId));
} while (0);
localVarPath = strReplace(localVarPath, localVarToReplace_petId, localVarBuff_petId);

I don't know how the generated code is able to work. Probably just a lucky case of compiler optimizations hiding the undefined behavior.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions