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

lvstring: fix size handling #538

Merged
merged 4 commits into from
Oct 26, 2023

Conversation

benoit-pierre
Copy link
Contributor

@benoit-pierre benoit-pierre commented Oct 24, 2023

The chunk size is the allocated size minus one (the terminating \0). Fix methods that don't have the same definition.


This change is Reviewable

- `size` is unnecessary and confusing (same as `length`)
- `capacity` return value was wrong (off by one)
The chunk `size` is the allocated size minus one (the terminating `\0`).
The chunk `size` is the allocated size minus one (the terminating `\0`).
@poire-z
Copy link
Contributor

poire-z commented Oct 26, 2023

Looks like it is the case indeed.
This is all confirmed by the fact there is n+1 in all the calls to malloc & realloc : sizeof(char8/32) * (n+1), right?
So, we were just always allocating one slot too much, that ended up just being not used ?
Any other method that was using size right ?

@benoit-pierre
Copy link
Contributor Author

Looks like it is the case indeed. This is all confirmed by the fact there is n+1 in all the calls to malloc & realloc : sizeof(char8/32) * (n+1), right? So, we were just always allocating one slot too much, that ended up just being not used ?

No, the +1 allocation is right (for the extra terminating \0), again size is the usable size.

The problem is setting size to n+1, which means a following call to append('c') for example would overflow the buffer by one character.

Any other method that was using size right ?

Yes, reserve.

@poire-z
Copy link
Contributor

poire-z commented Oct 26, 2023

No, the +1 allocation is right (for the extra terminating \0), again size is the usable size.

I meant it the other way around:
It's because size is indeed the usable size that the code was already using everywhere malloc & realloc : sizeof(char8/32) * (n+1).
Or I still don't get it ? :/

@benoit-pierre
Copy link
Contributor Author

No, the +1 allocation is right (for the extra terminating \0), again size is the usable size.

I meant it the other way around: It's because size is indeed the usable size that the code was already using everywhere malloc & realloc : sizeof(char8/32) * (n+1).

Yes, and setting size = n, and checking for not enough capacity with pchunk->size < len.

@poire-z
Copy link
Contributor

poire-z commented Oct 26, 2023

Same dilemna as in #543 (comment) in here. Waiting for a common decision :)

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