[PATCH] D45050: [clang-tidy] New checker for not null-terminated result caused by strlen(), size() or equal length

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 26 08:59:00 PDT 2018


aaron.ballman added inline comments.


================
Comment at: clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp:1038
+
+  SmallString<128> NewAddNullTermExprStr;
+  NewAddNullTermExprStr = "\n";
----------------
JonasToth wrote:
> whisperity wrote:
> > aaron.ballman wrote:
> > > This should be done using a `Twine`.
> > Can you please give an example of how to well-approach this? We had issues with using Twines on the stack and then later on running into weird undefined behaviour. The documentation is not clear to a lot of our colleagues on where the `Twine`'s usage barriers are... (Asking this not just for @Charusso but also for a few more colleagues, @baloghadamsoftware e.g.)
> naive approach:
> 
> ```
> std::string New = Twine("\n").concat(SpaceBeforeStmtStr).concat(exprToStr(..))...).str();
> ```
> I think retrieving a `SmallString` is not supported. Please note, that `Twine` does support taking integer and floats directly without prior conversion.
> You can use `operator+` instead of `concat` too.
> 
> Did this help or did you have more specific issues? 
The important bit is -- don't try to store a Twine (locally or otherwise). It's fine as a parameter in a function, but otherwise you should try to use it only as part of an expression. e.g., `(Twine("foo") + "bar" + baz).str()` is a good pattern to get used to.


https://reviews.llvm.org/D45050





More information about the cfe-commits mailing list