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

json2cbor: don't use the buffer variable after realloc() #262

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

thiagomacieira
Copy link
Member

There's a discussion in the C and C++ communities whether you're allowed to use the values of pointers that have been deallocated, if you don't dereference them. Some argue that it is Undefined Behaviour in spite of the numeric value stored in the variable not having changed.

Instead of arguing, let's just make sure we don't use the pointers after they have become dangling. We only needed the offset of how far we've written into the buffer to restore the state and we have a function that returns exactly that.

Seen while debugging #259.

@thiagomacieira thiagomacieira force-pushed the json2cbor_don_t_use_the_buffer_variable_after_realloc_ branch 2 times, most recently from feba0c1 to 2e2efde Compare November 7, 2024 15:35
@Nable80
Copy link

Nable80 commented Nov 8, 2024

Oops, see the updated comment: https://github.com/intel/tinycbor/pull/262/files#r1833533412

There's a discussion in the C and C++ communities whether you're allowed
to use the values of pointers that have been deallocated, if you don't
dereference them. Some argue that it is Undefined Behaviour in spite of
the numeric value stored in the variable not having changed.

Instead of arguing, let's just make sure we don't use the pointers after
they have become dangling. We only needed the offset of how far we've
written into the buffer to restore the state and we have a function that
returns exactly that.

Seen while debugging intel#259.

Drive-by keep the `buffersize` global variable unchanged until after
`realloc()` has returned with success.

Signed-off-by: Thiago Macieira <[email protected]>
@thiagomacieira thiagomacieira force-pushed the json2cbor_don_t_use_the_buffer_variable_after_realloc_ branch from 2e2efde to e1837ae Compare November 8, 2024 15:52
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.

2 participants