llvm-project
7625048a - [sanitizer_common] Measure strstr's haystack only if there is no match (#239871)

Commit
3 hours ago
[sanitizer_common] Measure strstr's haystack only if there is no match (#239871) StrstrCheck, used by the interceptors of strstr and strcasestr, calls internal_strlen on the haystack unconditionally, but uses that length only if there was no match. A match at the start of a long string therefore costs time proportional to the length of the string, and the usual loop `while ((p = strstr(p, needle))) { ...; p++; }` is quadratic in the size of the buffer. Compute the length only when it is needed. The ranges that are checked are the same as before. One case differs: for a haystack that has no terminator in front of an unmapped page, the old code crashes in internal_strlen even if strstr finds a match. Unless strict_string_checks is set, the new code does not read past what strstr itself reads. The new test strstr-match.c pins down which part of the haystack is checked when there is a match. It passes before and after this change, as do strstr-1.c, strstr-2.c, strstr_strict.c and the strcasestr tests. cc @jfbastien Assisted-by: Claude (I have reviewed every line of this PR)
Author
Parents
Loading