[PATCH] D130494: [InstCombine] Adjust snprintf folding of constant strings (PR #56598)

Martin Sebor via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 28 10:46:37 PDT 2022


msebor updated this revision to Diff 448379.
msebor added a comment.

I think I just equated the 32-bit `StringRef` limit with `INT_MAX`.  Thanks for pointing that out!

While re-reviewing the function I noticed a couple more instances of the same problem that I had missed.  Rather than duplicating the fix in all three places I factored it out into a new helper and added more tests.  I also removed the 32-bit `int` assumption noted by @majnemer.

As for testing the handling of excessive output, I can't think of a way without enhancing the function to handle more complex format strings, including directives with a width.  Then we could use `snprintf(0, 0, "x%*s", INT_MAX, "")` to exceed the limit without hardcoding long strings.  It's probably not worth the complexity just to test this unlikely case but supporting more involved format strings with multiple `%s` directives would make it possible to transform the idiom of using `snprintf` to concatenate strings, like `snprintf(buf, sizeof buf, "%s/%s", dirname, filename)`, and supporting precision (and width) is simple.  Another improvement to make this generally useful is to handle nonconstant strings allocated in arrays of known size.  But that's for another day.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D130494/new/

https://reviews.llvm.org/D130494

Files:
  llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
  llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
  llvm/test/Transforms/InstCombine/snprintf-2.ll
  llvm/test/Transforms/InstCombine/snprintf-3.ll
  llvm/test/Transforms/InstCombine/snprintf-4.ll
  llvm/test/Transforms/InstCombine/snprintf.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130494.448379.patch
Type: text/x-patch
Size: 36980 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220728/3f29f232/attachment.bin>


More information about the llvm-commits mailing list