sprintf(buf, "%s%s", buf, str) -> strcat(buf, str)
snprintf(buf, n, "%s%s", buf, str) -> strncat(buf, str, n)
Differential D46648
[SimplifyLibcalls] Optimize string concats using s(n)printf xbolva00 on May 9 2018, 10:17 AM. Authored by
Details
Diff Detail
Event Timeline
Comment Actions Yes, we can do anything if we prove the code has undefined behavior, but I'm not sure why we would prefer a call to strncat over "unreachable". Comment Actions "unreachable" is emitted when (in terms of this transformation)? Comment Actions If your results appear to be "correct", you're just getting lucky that your particular combination of compiler and C library don't do something else. We should probably just add clang -Wformat warning, rather than try to optimize it in SimplifyLibCalls. Comment Actions -Wformat does not handle it. UBsan.. I tried but I got some error on my system Anyway ok, I will close this. Comment Actions If you're not planning to move forward with the diff, it's better to abandon it rather than close it. Close usually implies it was committed. Comment Actions I am just wondering... snprintf(buf , n, "some data %s", buf) is UB.. Eli? Comment Actions The C standard is very clear: "If copying takes place between objects that overlap, the behavior is undefined." There is no exception for format strings which start with %s. |