strcmp(s, y) -> memcmp(s,y,2) where y is string with single char, e.g. "x")
Details
Diff Detail
Event Timeline
Oh, okay, that makes more sense.
Unfortunately, this transform is still illegal: if the unknown operand is the empty string, memcmp can read past the end of the string, which is undefined behavior (and may crash in practice).
Ah yes :/ I wanted to do similar transformation as GCC does. But with memcmp this is not possible.
If you want to expand strcmp inline, probably the best way to do that is to extend the existing ExpandMemCmpPass pass.
What about
char buf[10];
if (strcmp(buf, "key") eq / ne 0)
?
We can promote it to memcmp now, no?
Not sure how often that shows up, but yes, if you can prove the the pointer is dereferenceable, it's okay to transform to memcmp.
It is not so rare, I see it in many code bases.
Motivating example;
https://searchcode.com/file/71153892/src/resmom/linux/mom_mach.c