Skip to content

Commit 90b6890

Browse files
committed
Add function attributes that allow GCC to check the arguments of printf-like
functions.
1 parent 1569108 commit 90b6890

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

Include/pgenheaders.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ extern "C" {
2525

2626
#include "pydebug.h"
2727

28-
DL_IMPORT(void) PySys_WriteStdout(const char *format, ...);
29-
DL_IMPORT(void) PySys_WriteStderr(const char *format, ...);
28+
DL_IMPORT(void) PySys_WriteStdout(const char *format, ...)
29+
__attribute__((format(printf, 1, 2)));
30+
DL_IMPORT(void) PySys_WriteStderr(const char *format, ...)
31+
__attribute__((format(printf, 1, 2)));
3032

3133
#define addarc _Py_addarc
3234
#define addbit _Py_addbit

Include/pyerrors.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ extern DL_IMPORT(int) PyErr_BadArgument(void);
7777
extern DL_IMPORT(PyObject *) PyErr_NoMemory(void);
7878
extern DL_IMPORT(PyObject *) PyErr_SetFromErrno(PyObject *);
7979
extern DL_IMPORT(PyObject *) PyErr_SetFromErrnoWithFilename(PyObject *, char *);
80-
extern DL_IMPORT(PyObject *) PyErr_Format(PyObject *, const char *, ...);
80+
extern DL_IMPORT(PyObject *) PyErr_Format(PyObject *, const char *, ...)
81+
__attribute__((format(printf, 2, 3)));
8182
#ifdef MS_WINDOWS
8283
extern DL_IMPORT(PyObject *) PyErr_SetFromWindowsErrWithFilename(int, const char *);
8384
extern DL_IMPORT(PyObject *) PyErr_SetFromWindowsErr(int);
@@ -126,8 +127,10 @@ extern DL_IMPORT(PyObject *) PyErr_ProgramText(char *, int);
126127

127128
#ifndef HAVE_SNPRINTF
128129
#include <stdarg.h>
129-
extern DL_IMPORT(int) PyOS_snprintf(char *str, size_t size, const char *format, ...);
130-
extern DL_IMPORT(int) PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va);
130+
extern DL_IMPORT(int) PyOS_snprintf(char *str, size_t size, const char *format, ...)
131+
__attribute__((format(printf, 3, 4)));
132+
extern DL_IMPORT(int) PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
133+
__attribute__((format(printf, 3, 0)));
131134
#else
132135
# define PyOS_vsnprintf vsnprintf
133136
# define PyOS_snprintf snprintf

Include/stringobject.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ extern DL_IMPORT(PyTypeObject) PyString_Type;
5656

5757
extern DL_IMPORT(PyObject *) PyString_FromStringAndSize(const char *, int);
5858
extern DL_IMPORT(PyObject *) PyString_FromString(const char *);
59-
extern DL_IMPORT(PyObject *) PyString_FromFormatV(const char*, va_list);
60-
extern DL_IMPORT(PyObject *) PyString_FromFormat(const char*, ...);
59+
extern DL_IMPORT(PyObject *) PyString_FromFormatV(const char*, va_list)
60+
__attribute__((format(printf, 1, 0)));
61+
extern DL_IMPORT(PyObject *) PyString_FromFormat(const char*, ...)
62+
__attribute__((format(printf, 1, 2)));
6163
extern DL_IMPORT(int) PyString_Size(PyObject *);
6264
extern DL_IMPORT(char *) PyString_AsString(PyObject *);
6365
extern DL_IMPORT(void) PyString_Concat(PyObject **, PyObject *);

Include/sysmodule.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ DL_IMPORT(FILE *) PySys_GetFile(char *, FILE *);
1313
DL_IMPORT(void) PySys_SetArgv(int, char **);
1414
DL_IMPORT(void) PySys_SetPath(char *);
1515

16-
DL_IMPORT(void) PySys_WriteStdout(const char *format, ...);
17-
DL_IMPORT(void) PySys_WriteStderr(const char *format, ...);
16+
DL_IMPORT(void) PySys_WriteStdout(const char *format, ...)
17+
__attribute__((format(printf, 1, 2)));
18+
DL_IMPORT(void) PySys_WriteStderr(const char *format, ...)
19+
__attribute__((format(printf, 1, 2)));
1820

1921
extern DL_IMPORT(PyObject *) _PySys_TraceFunc, *_PySys_ProfileFunc;
2022
extern DL_IMPORT(int) _PySys_CheckInterval;

0 commit comments

Comments
 (0)