gh-112301: Add -Wformat=2 compiler option to NODIST#122474
gh-112301: Add -Wformat=2 compiler option to NODIST#122474nohlson wants to merge 20 commits intopython:mainfrom
Conversation
Python/getversion.c
Outdated
| #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.
Could it be switched to a #defined literal? That way it could look safe to the compiler, too.
There was a problem hiding this comment.
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 PyOS_snprintf() with the relevant format string literals in the respective #if/#else blocks. We eliminate the root cause of the warning instead of ignore it.
Objects/unicodeobject.c
Outdated
| // 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.
Consider using _Py_COMP_DIAG_PUSH/_Py_COMP_DIAG_POP, and adding a macro like _Py_COMP_DIAG_IGNORE_DEPR_DECLS, to make this easier to port to other compilers.
There was a problem hiding this comment.
@encukou created macro for ignoring format nonliterals and applied it to this block
|
I wanted to see if it's possible to avoid the warning altogether -- and had an implementation before I realized I should have probably delegated it. |
|
That PR is merged now :) |
Misc/NEWS.d/next/Security/2024-07-30-17-34-47.gh-issue-112301.8J8WhZ.rst
Outdated
Show resolved
Hide resolved
| Objects/mimalloc/ * | ||
| Python/pylifecycle.c 1 | ||
| Python/sysmodule.c 1 | ||
| Python/tracemalloc.c 1 No newline at end of file |
There was a problem hiding this comment.
Nit: can you please add the missing newline at EOF?
Is this still relevant? I stumbled upon that and wondered whether it's something relevant to add in this PR or in a separate PR (I was working with |
This adds
-Wformatand-Wformat=2to theCFLAGS_NODISTset of compiler flags. This is a warning flag that relates for format strings and for more information you can take a look at the OpenSSF guidance on this flag.This does generate a few warnings in
build_ubuntuand the new warning checker catches them catches them:unicodeobject.cwarnings look like they could be ignored since format strings forsprintfoperations are pulled from a const array of const format strings, and the variable that indexes these arrays is set from constants. However if we add this file to.warningignore_ubuntuthen if either of those things change maybe vulnerabilities could be introduced.As for
getversion.cit also could be ignored. I don't think there is too much of a concern in putting this in the warning ignore file.This change should require pre-merge build bots @corona10
Attn: @hugovk
EDIT: Removed content from the issue that pertained to already merged tooling options