Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 885d457

Browse files
committed
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Also changed <>-style #includes to ""-style in some places where the former didn't make sense.
1 parent 05bd787 commit 885d457

15 files changed

Lines changed: 61 additions & 42 deletions

Demo/pysvr/pysvr.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ can log in on your machine. Use with caution!
2525
Python.h defines a typedef destructor, which conflicts with pthread.h.
2626
So Python.h must be included after pthread.h. */
2727

28-
#include <Python.h>
28+
#include "Python.h"
2929

3030
extern int Py_VerboseFlag;
3131

@@ -364,6 +364,7 @@ static void
364364
ps(void)
365365
{
366366
char buffer[100];
367-
sprintf(buffer, "ps -l -p %d </dev/null | tail +2l\n", getpid());
367+
PyOS_snprintf(buffer, sizeof(buffer),
368+
"ps -l -p %d </dev/null | tail +2l\n", getpid());
368369
system(buffer);
369370
}

Modules/_hotshot.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* This is the High Performance Python Profiler portion of HotShot.
33
*/
44

5-
#include <Python.h>
6-
#include <compile.h>
7-
#include <eval.h>
8-
#include <frameobject.h>
9-
#include <structmember.h>
5+
#include "Python.h"
6+
#include "compile.h"
7+
#include "eval.h"
8+
#include "frameobject.h"
9+
#include "structmember.h"
1010

1111
#ifdef HAVE_UNISTD_H
1212
#include <unistd.h>
@@ -1452,12 +1452,12 @@ write_header(ProfilerObject *self)
14521452
pack_add_info(self, "executable-version", buffer);
14531453

14541454
#ifdef MS_WIN32
1455-
sprintf(cwdbuffer, "%I64d", frequency.QuadPart);
1455+
PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%I64d", frequency.QuadPart);
14561456
pack_add_info(self, "reported-performance-frequency", cwdbuffer);
14571457
#else
1458-
sprintf(cwdbuffer, "%lu", rusage_diff);
1458+
PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", rusage_diff);
14591459
pack_add_info(self, "observed-interval-getrusage", cwdbuffer);
1460-
sprintf(cwdbuffer, "%lu", timeofday_diff);
1460+
PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", timeofday_diff);
14611461
pack_add_info(self, "observed-interval-gettimeofday", cwdbuffer);
14621462
#endif
14631463

Modules/_localemodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ PyLocale_getdefaultlocale(PyObject* self, PyObject* args)
376376
if (!PyArg_NoArgs(args))
377377
return NULL;
378378

379-
sprintf(encoding, "cp%d", GetACP());
379+
PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
380380

381381
if (GetLocaleInfo(LOCALE_USER_DEFAULT,
382382
LOCALE_SISO639LANGNAME,

Modules/_testcapimodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ raiseTestError(const char* test_name, const char* msg)
1919
if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
2020
PyErr_SetString(TestError, "internal error msg too large");
2121
else {
22-
sprintf(buf, "%s: %s", test_name, msg);
22+
PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
2323
PyErr_SetString(TestError, buf);
2424
}
2525
return NULL;
@@ -36,7 +36,8 @@ sizeof_error(const char* fatname, const char* typename,
3636
int expected, int got)
3737
{
3838
char buf[1024];
39-
sprintf(buf, "%.200s #define == %d but sizeof(%.200s) == %d",
39+
PyOS_snprintf(buf, sizeof(buf),
40+
"%.200s #define == %d but sizeof(%.200s) == %d",
4041
fatname, expected, typename, got);
4142
PyErr_SetString(TestError, buf);
4243
return (PyObject*)NULL;

Modules/_tkinter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,8 +1579,8 @@ Tktt_Repr(PyObject *self)
15791579
TkttObject *v = (TkttObject *)self;
15801580
char buf[100];
15811581

1582-
sprintf(buf, "<tktimertoken at %p%s>", v,
1583-
v->func == NULL ? ", handler deleted" : "");
1582+
PyOS_snprintf(buf, sizeof(buf), "<tktimertoken at %p%s>", v,
1583+
v->func == NULL ? ", handler deleted" : "");
15841584
return PyString_FromString(buf);
15851585
}
15861586

Modules/arraymodule.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,12 +1313,13 @@ array_repr(arrayobject *a)
13131313
int i, len;
13141314
len = a->ob_size;
13151315
if (len == 0) {
1316-
sprintf(buf, "array('%c')", a->ob_descr->typecode);
1316+
PyOS_snprintf(buf, sizeof(buf), "array('%c')",
1317+
a->ob_descr->typecode);
13171318
return PyString_FromString(buf);
13181319
}
13191320
if (a->ob_descr->typecode == 'c') {
13201321
PyObject *t_empty = PyTuple_New(0);
1321-
sprintf(buf, "array('c', ");
1322+
PyOS_snprintf(buf, sizeof(buf), "array('c', ");
13221323
s = PyString_FromString(buf);
13231324
v = array_tostring(a, t_empty);
13241325
Py_DECREF(t_empty);
@@ -1328,7 +1329,7 @@ array_repr(arrayobject *a)
13281329
PyString_ConcatAndDel(&s, PyString_FromString(")"));
13291330
return s;
13301331
}
1331-
sprintf(buf, "array('%c', [", a->ob_descr->typecode);
1332+
PyOS_snprintf(buf, sizeof(buf), "array('%c', [", a->ob_descr->typecode);
13321333
s = PyString_FromString(buf);
13331334
comma = PyString_FromString(", ");
13341335
for (i = 0; i < len && !PyErr_Occurred(); i++) {

Modules/flmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ static PyObject *
370370
generic_repr(genericobject *g)
371371
{
372372
char buf[100];
373-
sprintf(buf, "<FORMS_object at %p, objclass=%d>",
374-
g, g->ob_generic->objclass);
373+
PyOS_snprintf(buf, sizeof(buf), "<FORMS_object at %p, objclass=%d>",
374+
g, g->ob_generic->objclass);
375375
return PyString_FromString(buf);
376376
}
377377

@@ -1580,8 +1580,8 @@ static PyObject *
15801580
form_repr(formobject *f)
15811581
{
15821582
char buf[100];
1583-
sprintf(buf, "<FORMS_form at %p, window=%ld>",
1584-
f, f->ob_form->window);
1583+
PyOS_snprintf(buf, sizeof(buf), "<FORMS_form at %p, window=%ld>",
1584+
f, f->ob_form->window);
15851585
return PyString_FromString(buf);
15861586
}
15871587

Modules/gdbmmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,8 @@ dbmopen(PyObject *self, PyObject *args)
477477
break;
478478
#endif
479479
default:
480-
sprintf(buf, "Flag '%c' is not supported.", *flags);
480+
PyOS_snprintf(buf, sizeof(buf), "Flag '%c' is not supported.",
481+
*flags);
481482
PyErr_SetString(DbmError, buf);
482483
return NULL;
483484
}

Modules/pcremodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ PyPcre_expand_escape(unsigned char *pattern, int pattern_len,
263263
case('U'): case('l'): case('u'):
264264
{
265265
char message[50];
266-
sprintf(message, "\\%c is not allowed", c);
266+
PyOS_snprintf(message, sizeof(message),
267+
"\\%c is not allowed", c);
267268
PyErr_SetString(ErrorObject, message);
268269
return NULL;
269270
}
@@ -495,7 +496,7 @@ PyPcre_expand(PyObject *self, PyObject *args)
495496
if (result==Py_None)
496497
{
497498
char message[50];
498-
sprintf(message,
499+
PyOS_snprintf(message, sizeof(message),
499500
"group did not contribute to the match");
500501
PyErr_SetString(ErrorObject,
501502
message);

Modules/posixmodule.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,8 @@ os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
432432
if (rc == NO_ERROR)
433433
os2_formatmsg(msgbuf, msglen, reason);
434434
else
435-
sprintf(msgbuf, "unknown OS error #%d", errorcode);
435+
PyOS_snprintf(msgbuf, sizeof(msgbuf),
436+
"unknown OS error #%d", errorcode);
436437

437438
return msgbuf;
438439
}
@@ -5814,8 +5815,9 @@ static int insertvalues(PyObject *d)
58145815
case 40: ver = "4.00"; break;
58155816
case 50: ver = "5.00"; break;
58165817
default:
5817-
sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
5818-
values[QSV_VERSION_MINOR]);
5818+
PyOS_snprintf(tmp, sizeof(tmp),
5819+
"%d-%d", values[QSV_VERSION_MAJOR],
5820+
values[QSV_VERSION_MINOR]);
58195821
ver = &tmp[0];
58205822
}
58215823

0 commit comments

Comments
 (0)