[PATCH] D46648: [SimplifyLibcalls] Optimize string concats using s(n)printf

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 9 10:26:51 PDT 2018


lebedev.ri added inline comments.


================
Comment at: lib/Transforms/Utils/SimplifyLibCalls.cpp:1841
 
+  // sprintf(buf, "%s%s", buf, str) -> strcat(buf, str)
+  if (FormatStr == "%s%s" && CI->getNumArgOperands() == 4) {
----------------
```
PRINTF(3)

<...>

NOTES
       Some programs imprudently rely on code such as the following

           sprintf(buf, "%s some further text", buf);

       to  append  text to buf.  However, the standards explicitly note that the results are undefined if source and destination buffers overlap when calling sprintf(), snprintf(), vsprintf(), and vsnprintf().  Depending on the version of gcc(1) used, and the com‐
       piler options employed, calls such as the above will not produce the expected results.

       The glibc implementation of the functions snprintf() and vsnprintf() conforms to the C99 standard, that is, behaves as described above, since glibc version 2.1.  Until glibc 2.0.6, they would return -1 when the output was truncated.
```


Repository:
  rL LLVM

https://reviews.llvm.org/D46648





More information about the llvm-commits mailing list