Repository navigation
Conversation
| 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"); |
There was a problem hiding this comment.
| "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.
There was a problem hiding this comment.
Nevermind, I just saw this patch wants to alter this internal
function's behavior to match a C standard. Why do we do
that?
There was a problem hiding this comment.
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
49bf80d to
1125b47
Compare
|
Yeah, however much I dislike it, the point is correct. That section actually do say this is undefined behavior. |
When no match is found, the previous fallback in
Curl_memrchr()decrementedpbefore the start of the buffer (s - 1), which is undefined pointer arithmetic under C 6.5.6p8.Use a decreasing index
p[--n]guarded bywhile(n)instead. This avoids forming an out-of-bounds pointer and simplifies the loop while keeping behavior identical.Vulnerability identified by BudgetScan static analysis.