-
Notifications
You must be signed in to change notification settings - Fork 1
Bugprone unsafe format string #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
NagyDonat
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks reasonable overall, but I have a few suggestions in inline comments. I didn't inspect the test files, but based on a brief look they are probably fine as well.
clang-tools-extra/clang-tidy/bugprone/UnsafeFormatStringCheck.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/clang-tidy/bugprone/UnsafeFormatStringCheck.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/clang-tidy/bugprone/UnsafeFormatStringCheck.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/clang-tidy/bugprone/UnsafeFormatStringCheck.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/clang-tidy/bugprone/UnsafeFormatStringCheck.cpp
Outdated
Show resolved
Hide resolved
| anyOf(hasArgument(0, stringLiteral().bind("format")), | ||
| hasArgument(1, stringLiteral().bind("format")))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is a bit hacky -- it would be more elegant if the checker knew that e.g. the format string of sprintf is always at argument index 1, while the format string of scanf is always at argument index 0. However, code that would confuse this check is wildly incorrect, won't occur in the wild and would produce compiler errors (or at least severe warnings), so this is not a serious issue.
clang-tools-extra/clang-tidy/bugprone/UnsafeFormatStringCheck.cpp
Outdated
Show resolved
Hide resolved
-Only matching global functions in c and std namespace in c++ -Adding C++ namespace tests -Removing fixit hints -Removing standard header includes from the test files and adding function/type definitions -Other small fixes
dkrupp
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review. Your remarks are fixed.
clang-tools-extra/clang-tidy/bugprone/UnsafeFormatStringCheck.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/clang-tidy/bugprone/UnsafeFormatStringCheck.cpp
Outdated
Show resolved
Hide resolved
NagyDonat
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM if you switch to using system header simulators (as far as it's possible).
| #include <stdarg.h> | ||
|
|
||
| typedef __SIZE_TYPE__ size_t; | ||
| typedef __WCHAR_TYPE__ wchar_t; | ||
| typedef void *FILE; | ||
| extern FILE *stdin; | ||
| extern FILE *stderr; | ||
|
|
||
| extern int fscanf ( FILE * stream, const char * format, ... ); | ||
| extern int scanf ( const char * format, ... ); | ||
| extern int sscanf ( const char * s, const char * format, ...); | ||
| extern int vscanf( const char *restrict format, va_list vlist ); | ||
| extern int vfscanf ( FILE * stream, const char * format, va_list arg ); | ||
|
|
||
| extern int vsscanf( const char *restrict buffer, const char *restrict format, va_list vlist ); | ||
| extern int vwscanf( const wchar_t* format, va_list vlist ); | ||
| extern int vfwscanf( FILE* stream, const wchar_t* format, va_list vlist ); | ||
| extern int vswscanf( const wchar_t* buffer, const wchar_t* format, va_list vlist ); | ||
| extern int swscanf (const wchar_t* ws, const wchar_t* format, ...); | ||
| extern int wscanf( const wchar_t *format, ... ); | ||
| extern int fwscanf( FILE *stream, const wchar_t *format, ... ); | ||
|
|
||
| extern int printf( const char* format, ... ); | ||
| extern int sprintf( char* buffer, const char* format, ... ); | ||
| extern int vsprintf (char * s, const char * format, va_list arg ); | ||
| extern int vsnprintf (char * s, size_t n, const char * format, va_list arg ); | ||
| extern int fprintf( FILE* stream, const char* format, ... ); | ||
| extern int snprintf( char* restrict buffer, size_t bufsz, | ||
| const char* restrict format, ... ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a system header simulator file.
Add a new bugprone-unsafe-format-string clang-tidy checker, which warns for scanf and sprintf like functions invocations with a format string literal with unbounded %s specifier that can cause buffer overflow.