Skip to content

Simplify realloc logic #175

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

Merged
merged 2 commits into from
Jun 20, 2025
Merged

Simplify realloc logic #175

merged 2 commits into from
Jun 20, 2025

Conversation

febo
Copy link
Collaborator

@febo febo commented Jun 7, 2025

Problem

The current way that realloc tracks the original data length is unnecessarily complex.

Solution

This PR simplifies the logic on realloc to store difference between the original data length and the current length.

@febo febo requested review from joncinque and rustopian June 7, 2025 11:22
Copy link

@rustopian rustopian left a comment

Choose a reason for hiding this comment

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

Looks good overall, but I do wonder about a possible exploit of multiple realloc() calls given the limitations of the delta approach - see comment

rustopian
rustopian previously approved these changes Jun 10, 2025
ilya-bobyr
ilya-bobyr previously approved these changes Jun 12, 2025
Copy link

@ilya-bobyr ilya-bobyr left a comment

Choose a reason for hiding this comment

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

Using zero initialization to both avoid length assignment and make the logic simpler is really smart :)

Base automatically changed from febo/deprecate-realloc to main June 13, 2025 10:28
@febo febo dismissed stale reviews from ilya-bobyr and rustopian June 13, 2025 10:28

The base branch was changed.

Copy link
Collaborator

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

The change looks good to me!

There is an edge case if someone calls close() and then resize() within the same instruction.

With the current code, resize() would incorrectly return an error for going over the limit, even if the account previously had some space.

How about setting the difference during close()? close_unchecked() can stay as it is, but it would be good to add a comment saying to avoid resizing afterwards.

@febo febo force-pushed the febo/refactor-realloc branch from 7902606 to 7da489a Compare June 14, 2025 01:53
@febo
Copy link
Collaborator Author

febo commented Jun 14, 2025

The change looks good to me!

There is an edge case if someone calls close() and then resize() within the same instruction.

With the current code, resize() would incorrectly return an error for going over the limit, even if the account previously had some space.

How about setting the difference during close()? close_unchecked() can stay as it is, but it would be good to add a comment saying to avoid resizing afterwards.

Good point! Fixed close() as suggested.

@febo febo requested a review from joncinque June 14, 2025 01:53
Copy link
Collaborator

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

Looks great to me!

@febo febo merged commit bf4fdb7 into main Jun 20, 2025
9 checks passed
@febo febo deleted the febo/refactor-realloc branch June 20, 2025 12:40
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.

5 participants