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

Skip to content

Commit 518ab1c

Browse files
committed
Use PyOS_snprintf instead of sprintf.
1 parent ef58b31 commit 518ab1c

13 files changed

Lines changed: 45 additions & 39 deletions

Python/bltinmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ builtin_map(PyObject *self, PyObject *args)
779779
static char errmsg[] =
780780
"argument %d to map() must support iteration";
781781
char errbuf[sizeof(errmsg) + 25];
782-
sprintf(errbuf, errmsg, i+2);
782+
PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
783783
PyErr_SetString(PyExc_TypeError, errbuf);
784784
goto Fail_2;
785785
}

Python/dynload_dl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
2121
{
2222
char funcname[258];
2323

24-
sprintf(funcname, "init%.200s", shortname);
24+
PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname);
2525
return dl_loadmod(Py_GetProgramName(), pathname, funcname);
2626
}

Python/dynload_hpux.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
3939
char buf[256];
4040
if (Py_VerboseFlag)
4141
perror(pathname);
42-
sprintf(buf, "Failed to load %.200s", pathname);
42+
PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s",
43+
pathname);
4344
PyErr_SetString(PyExc_ImportError, buf);
4445
return NULL;
4546
}
46-
sprintf(funcname, FUNCNAME_PATTERN, shortname);
47+
PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN, shortname);
4748
if (Py_VerboseFlag)
4849
printf("shl_findsym %s\n", funcname);
4950
shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p);

Python/dynload_mac.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
6666
err = ResolveAliasFile(&libspec, 1, &isfolder, &didsomething);
6767
#endif
6868
if ( err ) {
69-
sprintf(buf, "%.200s: %.200s", pathname, PyMac_StrError(err));
69+
PyOS_snprintf(buf, sizeof(buf),
70+
"%.200s: %.200s", pathname, PyMac_StrError(err));
7071
PyErr_SetString(PyExc_ImportError, buf);
7172
return NULL;
7273
}
@@ -91,25 +92,26 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
9192
** the dynamic module was meant for a different Python.
9293
*/
9394
if (errMessage[0] == 10 && strncmp((char *)errMessage+1, "PythonCore", 10) == 0 ) {
94-
sprintf(buf, "Dynamic module was built for %s version of MacPython",
95-
(err == cfragImportTooOldErr ? "a newer" : "an older"));
95+
PyOS_snprintf(buf, sizeof(buf),
96+
"Dynamic module was built for %s version of MacPython",
97+
(err == cfragImportTooOldErr ? "a newer" : "an older"));
9698
PyErr_SetString(PyExc_ImportError, buf);
9799
return NULL;
98100
}
99101
}
100102
if ( err ) {
101-
sprintf(buf, "%.*s: %.200s",
103+
PyOS_snprintf(buf, sizeof(buf), "%.*s: %.200s",
102104
errMessage[0], errMessage+1,
103105
PyMac_StrError(err));
104106
PyErr_SetString(PyExc_ImportError, buf);
105107
return NULL;
106108
}
107109
/* Locate the address of the correct init function */
108-
sprintf(funcname, "init%.200s", shortname);
110+
PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname);
109111
err = FindSymbol(connID, Pstring(funcname), &symAddr, &class);
110112
if ( err ) {
111-
sprintf(buf, "%s: %.200s",
112-
funcname, PyMac_StrError(err));
113+
PyOS_snprintf(buf, sizeof(buf), "%s: %.200s",
114+
funcname, PyMac_StrError(err));
113115
PyErr_SetString(PyExc_ImportError, buf);
114116
return NULL;
115117
}

Python/dynload_next.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
4444
dl_funcptr p = NULL;
4545
char funcname[258];
4646

47-
sprintf(funcname, "_init%.200s", shortname);
47+
PyOS_snprintf(funcname, sizeof(funcname), "_init%.200s", shortname);
4848

4949
#ifdef USE_RLD
5050
{

Python/dynload_os2.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
3131

3232
if (rc != NO_ERROR) {
3333
char errBuf[256];
34-
sprintf(errBuf,
35-
"DLL load failed, rc = %d: %.200s",
36-
rc, failreason);
34+
PyOS_snprintf(errBuf, sizeof(errBuf),
35+
"DLL load failed, rc = %d: %.200s",
36+
rc, failreason);
3737
PyErr_SetString(PyExc_ImportError, errBuf);
3838
return NULL;
3939
}
4040

41-
sprintf(funcname, "init%.200s", shortname);
41+
PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname);
4242
rc = DosQueryProcAddr(hDLL, 0L, funcname, &p);
4343
if (rc != NO_ERROR)
4444
p = NULL; /* Signify Failure to Acquire Entrypoint */

Python/dynload_shlib.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
5757

5858
if (strchr(pathname, '/') == NULL) {
5959
/* Prefix bare filename with "./" */
60-
sprintf(pathbuf, "./%-.255s", pathname);
60+
PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
6161
pathname = pathbuf;
6262
}
6363

64-
sprintf(funcname, LEAD_UNDERSCORE "init%.200s", shortname);
64+
PyOS_snprintf(funcname, sizeof(funcname),
65+
LEAD_UNDERSCORE "init%.200s", shortname);
6566

6667
if (fp != NULL) {
6768
int i;

Python/dynload_win.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
159159
dl_funcptr p;
160160
char funcname[258], *import_python;
161161

162-
sprintf(funcname, "init%.200s", shortname);
162+
PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname);
163163

164164
#ifdef MS_WIN32
165165
{
@@ -201,9 +201,9 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
201201
/* Problem: could not get the error message.
202202
This should not happen if called correctly. */
203203
if (theLength == 0) {
204-
sprintf(errBuf,
205-
"DLL load failed with error code %d",
206-
errorCode);
204+
PyOS_snprintf(errBuf, sizeof(errBuf),
205+
"DLL load failed with error code %d",
206+
errorCode);
207207
} else {
208208
size_t len;
209209
/* For some reason a \r\n
@@ -225,16 +225,16 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
225225
} else {
226226
char buffer[256];
227227

228-
sprintf(buffer,"python%d%d.dll",
228+
PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll",
229229
PY_MAJOR_VERSION,PY_MINOR_VERSION);
230230
import_python = GetPythonImport(hDLL);
231231

232232
if (import_python &&
233233
strcasecmp(buffer,import_python)) {
234-
sprintf(buffer,
235-
"Module use of %.150s conflicts "
236-
"with this version of Python.",
237-
import_python);
234+
PyOS_snprintf(buffer, sizeof(buffer),
235+
"Module use of %.150s conflicts "
236+
"with this version of Python.",
237+
import_python);
238238
PyErr_SetString(PyExc_ImportError,buffer);
239239
FreeLibrary(hDLL);
240240
return NULL;
@@ -251,14 +251,16 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
251251
strchr(pathname, '/') == NULL)
252252
{
253253
/* Prefix bare filename with ".\" */
254-
sprintf(pathbuf, ".\\%-.13s", pathname);
254+
PyOS_snprintf(pathbuf, sizeof(pathbuf),
255+
".\\%-.13s", pathname);
255256
pathname = pathbuf;
256257
}
257258
hDLL = LoadLibrary(pathname);
258259
if (hDLL < HINSTANCE_ERROR){
259260
char errBuf[256];
260-
sprintf(errBuf,
261-
"DLL load failed with error code %d", hDLL);
261+
PyOS_snprintf(errBuf, sizeof(errBuf),
262+
"DLL load failed with error code %d",
263+
hDLL);
262264
PyErr_SetString(PyExc_ImportError, errBuf);
263265
return NULL;
264266
}

Python/getversion.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const char *
99
Py_GetVersion(void)
1010
{
1111
static char version[250];
12-
sprintf(version, "%.80s (%.80s) %.80s", PY_VERSION,
13-
Py_GetBuildInfo(), Py_GetCompiler());
12+
PyOS_snprintf(version, sizeof(version), "%.80s (%.80s) %.80s",
13+
PY_VERSION, Py_GetBuildInfo(), Py_GetCompiler());
1414
return version;
1515
}

Python/pythonrun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
933933
else
934934
PyFile_WriteString(filename, f);
935935
PyFile_WriteString("\", line ", f);
936-
sprintf(buf, "%d", lineno);
936+
PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
937937
PyFile_WriteString(buf, f);
938938
PyFile_WriteString("\n", f);
939939
if (text != NULL)

0 commit comments

Comments
 (0)