Skip to content

mbstring: save the write offset before reallocating - #25570

Open
realFlowControl wants to merge 2 commits into
php:masterfrom
realFlowControl:florian/mbstring-save-write-offset
Open

realFlowControl wants to merge 2 commits into
php:masterfrom
realFlowControl:florian/mbstring-save-write-offset

Conversation

@realFlowControl

Copy link
Copy Markdown
Contributor

When mb_convert_buf_ensure() grows the conversion buffer, it calculates the write offset by subtracting two pointers into the old buffer after calling safe_ferealloc(). If the allocation moves, those pointers are no longer valid. Subtracting them is undefined behavior even though neither is dereferenced.

This PR saves the offset before reallocating, then add it to the new buffer’s base address.

@alexdowad

Copy link
Copy Markdown
Contributor

@realFlowControl Thanks for this contribution.

Do you think that a comment similar to /* Save offset before realloc, which may move the buffer */ would be helpful to future readers? Or not necessary?

@realFlowControl

Copy link
Copy Markdown
Contributor Author

@alexdowad depends on the "future reader". ggc/clang static analyzers do not see it, because they are not aware that safe_ferealloc() behaves like libc realloc(). Funnny thing: if we change to actually use libc realloc() further down, the gcc static analyzer complains.
I'll see if there is a way to test this in CI, otherwise I think a future reader using git blame sees this PR and sees it was changed from undefined behavior to this.

@realFlowControl

Copy link
Copy Markdown
Contributor Author

@alexdowad I found a way, but it involves ASan and it finds more undefined behavior like this. I'll work on adding this check in a follow up PR if that is fine for you, WDYT?

@alexdowad

Copy link
Copy Markdown
Contributor

Dear @realFlowControl, strictly speaking, you are right that the answer to "what comments will benefit a future human reader" is "it depends on the reader". However, my personal judgement is that a short explanatory comment here would help many human readers. (What gcc and clang see is a different issue and not related to my comment.)

Ideas for how to check for UB issues in CI are certainly welcome. But I think that adding such checks to CI relates to more than just mbstring; any other part of PHP might have UB which would be good to avoid. So I would suggest discussing such ideas on the mailing list, with the target of possibly applying them to the entire codebase. (I hope this doesn't result in a flood of thousands of patches 😅)

@realFlowControl
realFlowControl force-pushed the florian/mbstring-save-write-offset branch from 2706eec to 2de0ffd Compare November 25, 2026 07:45
@realFlowControl

Copy link
Copy Markdown
Contributor Author

Added the comment. Good point on the CI checks!

@alexdowad

Copy link
Copy Markdown
Contributor

I'm busy at this moment but will merge this as soon as I have some time. If you are a committer, please feel free to go ahead and merge if you get to it first.

@alexdowad

Copy link
Copy Markdown
Contributor

@realFlowControl, your other commit 23970fa got merged first, and it conflicts with this one. Can you rebase?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants