This is an archive of the discontinued LLVM Phabricator instance.

Check for CERT ERR34-C. Detect errors when converting a string to a number
ClosedPublic

Authored by aaron.ballman on Apr 27 2016, 7:03 AM.

Details

Reviewers
alexfh
sbenza
Summary

Some numeric conversion APIs like atoi() and scanf() do not check the validity of the value being converted, so it is impossible to tell whether range errors have occurred. There are better APIs that can be used to ensure that input is validated properly, such as strtol() and related functions. This clang-tidy check flags calls to conversion functions that have insufficient error checking and diagnoses them, suggesting a better alternative.

This check corresponds to: https://www.securecoding.cert.org/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number.

Diff Detail

Event Timeline

aaron.ballman retitled this revision from to Check for CERT ERR34-C. Detect errors when converting a string to a number.
aaron.ballman updated this object.
aaron.ballman added reviewers: alexfh, sbenza.
aaron.ballman added a subscriber: cfe-commits.
alexfh accepted this revision.Apr 29 2016, 9:39 AM
alexfh edited edge metadata.

Looks good with a few nits.

clang-tidy/cert/StrToNumCheck.cpp
182

Too many abbreviations for my taste. How about CE -> Call, FD -> Function or FuncDecl, CK -> Conversion, CFD -> ConverterFunc?

224
  1. s/std::string/StringRef/
  2. I'd make the functions return StringRef instead of a const char *
  3. One variable per declaration, please.
  4. I'm not sure the variables help making the code easier to read.
docs/clang-tidy/checks/cert-err34-c.rst
9

Maybe add a couple of examples?

This revision is now accepted and ready to land.Apr 29 2016, 9:39 AM
aaron.ballman closed this revision.Apr 29 2016, 2:03 PM
aaron.ballman marked 3 inline comments as done.

Thanks! I've commit in r268100 with the changes you requested.

clang-tidy/cert/StrToNumCheck.cpp
182

My Clang-isms are spilling over. ;-) I've converted to the more verbose names.