Skip to content

Conversation

@dkrupp
Copy link
Owner

@dkrupp dkrupp commented Nov 7, 2025

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.

Copy link

@NagyDonat NagyDonat left a 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.

Comment on lines +31 to +32
anyOf(hasArgument(0, stringLiteral().bind("format")),
hasArgument(1, stringLiteral().bind("format"))))

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.

-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
Copy link
Owner Author

@dkrupp dkrupp left a 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.

@dkrupp dkrupp requested a review from NagyDonat November 12, 2025 12:58
Copy link

@NagyDonat NagyDonat left a 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).

Comment on lines 5 to 31
#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, ... );

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants