-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
gh-112301: Add -Wformat=2 compiler option to NODIST #122474
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?
Changes from 6 commits
c5396a1
19437ef
cba84fe
178bcc6
3bc76ca
c4b8cf9
8cff32e
95c612c
a3b0c46
fa52862
6cd1def
13e9c80
b62d155
9806f88
88fbc65
b6db214
304aed1
5afb8d8
1e664f6
1b4089b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add -Wformat=2 to NODIST build flags to warn about potential vulnerabilities related to format strings. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2851,6 +2851,14 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer, | |
default: fmt = formats[sizemod]; break; | ||
} | ||
int issigned = (*f == 'd' || *f == 'i'); | ||
// Format strings for sprintf are selected from constant arrays of | ||
// constant strings, and the variable used to index into the arrays | ||
// is only assigned known constant values. Ignore warnings related | ||
// to the format string not being a string literal. | ||
#if defined(__GNUC__) || defined(__clang__) | ||
#pragma GCC diagnostic push | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @encukou created macro for ignoring format nonliterals and applied it to this block |
||
#pragma GCC diagnostic ignored "-Wformat-nonliteral" | ||
#endif | ||
switch (sizemod) { | ||
case F_LONG: | ||
len = issigned ? | ||
|
@@ -2881,6 +2889,9 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer, | |
sprintf(buffer, fmt, va_arg(*vargs, unsigned int)); | ||
break; | ||
} | ||
#if defined(__GNUC__) || defined(__clang__) | ||
#pragma GCC diagnostic pop | ||
#endif | ||
assert(len >= 0); | ||
|
||
int sign = (buffer[0] == '-'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,17 @@ void _Py_InitVersion(void) | |
#else | ||
const char *buildinfo_format = "%.80s (%.80s) %.80s"; | ||
#endif | ||
// The format string is defined above and is observably safe. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could it be switched to a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just pushed a change. I figure instead of going through the trouble defining a format string in the preprocessor #if/#else blocks and adding all of the diagnostic pragmas we can just put the |
||
// Ignore warnings related to non-literal format strings. | ||
#if defined(__GNUC__) && !defined(__clang__) | ||
#pragma GCC diagnostic push | ||
#pragma GCC diagnostic ignored "-Wformat-nonliteral" | ||
#endif | ||
PyOS_snprintf(version, sizeof(version), buildinfo_format, | ||
PY_VERSION, Py_GetBuildInfo(), Py_GetCompiler()); | ||
#if defined(__GNUC__) && !defined(__clang__) | ||
#pragma GCC diagnostic pop | ||
#endif | ||
} | ||
|
||
const char * | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.