Skip to content

curl_memrchr: keep reverse searches within the buffer - #23538

Closed
Huge wants to merge 1 commit into
curl:masterfrom
Huge:fix/memrchr-bounds
Closed

Huge wants to merge 1 commit into
curl:masterfrom
Huge:fix/memrchr-bounds

Conversation

@Huge

@Huge Huge commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

When no match is found, the previous fallback in Curl_memrchr() decremented p before the start of the buffer (s - 1), which is undefined pointer arithmetic under C 6.5.6p8.

Use a decreasing index p[--n] guarded by while(n) instead. This avoids forming an out-of-bounds pointer and simplifies the loop while keeping behavior identical.


Vulnerability identified by BudgetScan static analysis.

@github-actions github-actions Bot added the tests label Oct 21, 2026
Comment thread tests/unit/unit1694.c Outdated
fail_unless(memrchr(data, 'a', 3) == data,
"search must include the first byte and respect the length");
fail_unless(memrchr(data, 0, sizeof(data)) == data + 1,
"search must include embedded zero bytes");

@vszakats vszakats Oct 21, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"search must include embedded zero bytes");
"search must include embedded NUL bytes");

Are these tests useful? Isn't this function heavily tested already in indirect ways?

Looks like trivia to me, like an LLM would generate in its infinite spare time.

@vszakats vszakats Oct 21, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nevermind, I just saw this patch wants to alter this internal
function's behavior to match a C standard. Why do we do
that?

@Huge Huge Oct 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The behavior (return values for any input) is actually 100% unchanged.

The only difference is that the old loop decremented p before s when no match was found (p becomes s - 1), which is undefined pointer arithmetic in C (C99/C11 6.5.6p8) and can trip UB sanitizers (-fsanitize=pointer-overflow). Using while(n) with p[--n] avoids forming the out-of-bounds pointer and simplifies the loop.

You were/are right about unit1694 being a redundant trivia given existing test coverage, I've dropped the unit test so the PR is just the 6-line simplification in lib/curl_memrchr.c.

When no match is found, the previous fallback decremented p before
the start of the buffer (s - 1), which is undefined pointer arithmetic.

Use a decreasing index p[--n] guarded by while(n) instead. This avoids
forming an out-of-bounds pointer and simplifies the loop while keeping
behavior unchanged.

Reported-by: BS.ehlas.cz
@Huge
Huge force-pushed the fix/memrchr-bounds branch from 49bf80d to 1125b47 Compare October 21, 2026 14:42
@bagder

bagder commented Oct 21, 2026

Copy link
Copy Markdown
Member

Yeah, however much I dislike it, the point is correct. That section actually do say this is undefined behavior.

@bagder bagder closed this in c4f294a Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants