Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace free() malloc() sequences with realloc #2894

Open
AndreasFuchsTPM opened this issue Aug 21, 2024 · 1 comment
Open

Replace free() malloc() sequences with realloc #2894

AndreasFuchsTPM opened this issue Aug 21, 2024 · 1 comment

Comments

@AndreasFuchsTPM
Copy link
Member

Static code analysis gets confused by free() & malloc() sequences and claims use-after-free.

We can avoid this by using realloc instead; e.g.

free(object->misc.key.private.buffer);
object->misc.key.private.buffer = malloc(object->misc.key.private.size);

@Hinara
Copy link

Hinara commented Aug 23, 2024

I think it might be because realloc error handling is cumbersome, meaning instead of the lines you showed you would have

            uint8_t *new_buffer = malloc(object->misc.key.private.size);
            goto_if_null2(new_buffer, "Out of memory.",
                    r, TSS2_FAPI_RC_MEMORY, error_cleanup);
            object->misc.key.private.buffer = new_buffer;

instead of

            free(object->misc.key.private.buffer);
            object->misc.key.private.buffer = malloc(object->misc.key.private.size);
            goto_if_null2(object->misc.key.private.buffer, "Out of memory.",
                    r, TSS2_FAPI_RC_MEMORY, error_cleanup);

But might be better in term of perfomance in case the zone allocated by malloc is already big enough to handle that, however, realloc include a memcpy when this is not the case which is useless, which might counter act any potential benefit of using realloc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants