Skip to content

Commit 783bed4

Browse files
[3.7] bpo-36254: Fix invalid uses of %d in format strings in C. (GH-12264). (GH-12322)
(cherry picked from commit d53fe5f) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent a84f9bc commit 783bed4

18 files changed

+34
-34
lines changed

Modules/_ctypes/callbacks.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ static void _CallPythonObject(void *mem,
160160
if (cnv)
161161
dict = PyType_stgdict(cnv);
162162
else {
163-
PrintError("Getting argument converter %d\n", i);
163+
PrintError("Getting argument converter %zd\n", i);
164164
goto Done;
165165
}
166166

167167
if (dict && dict->getfunc && !_ctypes_simple_instance(cnv)) {
168168
PyObject *v = dict->getfunc(*pArgs, dict->size);
169169
if (!v) {
170-
PrintError("create argument %d:\n", i);
170+
PrintError("create argument %zd:\n", i);
171171
Py_DECREF(cnv);
172172
goto Done;
173173
}
@@ -181,14 +181,14 @@ static void _CallPythonObject(void *mem,
181181
/* Hm, shouldn't we use PyCData_AtAddress() or something like that instead? */
182182
CDataObject *obj = (CDataObject *)_PyObject_CallNoArg(cnv);
183183
if (!obj) {
184-
PrintError("create argument %d:\n", i);
184+
PrintError("create argument %zd:\n", i);
185185
Py_DECREF(cnv);
186186
goto Done;
187187
}
188188
if (!CDataObject_Check(obj)) {
189189
Py_DECREF(obj);
190190
Py_DECREF(cnv);
191-
PrintError("unexpected result of create argument %d:\n", i);
191+
PrintError("unexpected result of create argument %zd:\n", i);
192192
goto Done;
193193
}
194194
memcpy(obj->b_ptr, *pArgs, dict->size);
@@ -199,7 +199,7 @@ static void _CallPythonObject(void *mem,
199199
} else {
200200
PyErr_SetString(PyExc_TypeError,
201201
"cannot build parameter");
202-
PrintError("Parsing argument %d\n", i);
202+
PrintError("Parsing argument %zd\n", i);
203203
Py_DECREF(cnv);
204204
goto Done;
205205
}

Modules/_ctypes/callproc.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1125,20 +1125,20 @@ PyObject *_ctypes_callproc(PPROC pProc,
11251125
converter = PyTuple_GET_ITEM(argtypes, i);
11261126
v = PyObject_CallFunctionObjArgs(converter, arg, NULL);
11271127
if (v == NULL) {
1128-
_ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
1128+
_ctypes_extend_error(PyExc_ArgError, "argument %zd: ", i+1);
11291129
goto cleanup;
11301130
}
11311131

11321132
err = ConvParam(v, i+1, pa);
11331133
Py_DECREF(v);
11341134
if (-1 == err) {
1135-
_ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
1135+
_ctypes_extend_error(PyExc_ArgError, "argument %zd: ", i+1);
11361136
goto cleanup;
11371137
}
11381138
} else {
11391139
err = ConvParam(arg, i+1, pa);
11401140
if (-1 == err) {
1141-
_ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
1141+
_ctypes_extend_error(PyExc_ArgError, "argument %zd: ", i+1);
11421142
goto cleanup; /* leaking ? */
11431143
}
11441144
}

Modules/_ctypes/malloc_closure.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static void more_core(void)
7676

7777
#ifdef MALLOC_CLOSURE_DEBUG
7878
printf("block at %p allocated (%d bytes), %d ITEMs\n",
79-
item, count * sizeof(ITEM), count);
79+
item, count * (int)sizeof(ITEM), count);
8080
#endif
8181
/* put them into the free list */
8282
for (i = 0; i < count; ++i) {

Modules/_io/winconsoleio.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ readinto(winconsoleio *self, char *buf, Py_ssize_t len)
724724

725725
if (u8n) {
726726
PyErr_Format(PyExc_SystemError,
727-
"Buffer had room for %d bytes but %d bytes required",
727+
"Buffer had room for %zd bytes but %u bytes required",
728728
len, u8n);
729729
return -1;
730730
}

Modules/_localemodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ PyLocale_getdefaultlocale(PyObject* self)
394394
char encoding[100];
395395
char locale[100];
396396

397-
PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
397+
PyOS_snprintf(encoding, sizeof(encoding), "cp%u", GetACP());
398398

399399
if (GetLocaleInfo(LOCALE_USER_DEFAULT,
400400
LOCALE_SISO639LANGNAME,

Modules/_lzmamodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ parse_filter_spec_lzma(PyObject *spec)
219219

220220
if (lzma_lzma_preset(options, preset)) {
221221
PyMem_Free(options);
222-
PyErr_Format(Error, "Invalid compression preset: %d", preset);
222+
PyErr_Format(Error, "Invalid compression preset: %u", preset);
223223
return NULL;
224224
}
225225

@@ -630,7 +630,7 @@ Compressor_init_alone(lzma_stream *lzs, uint32_t preset, PyObject *filterspecs)
630630
lzma_options_lzma options;
631631

632632
if (lzma_lzma_preset(&options, preset)) {
633-
PyErr_Format(Error, "Invalid compression preset: %d", preset);
633+
PyErr_Format(Error, "Invalid compression preset: %u", preset);
634634
return -1;
635635
}
636636
lzret = lzma_alone_encoder(lzs, &options);

Modules/_multiprocessing/semaphore.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
141141
default:
142142
PyErr_Format(PyExc_RuntimeError, "WaitForSingleObject() or "
143143
"WaitForMultipleObjects() gave unrecognized "
144-
"value %d", res);
144+
"value %u", res);
145145
return NULL;
146146
}
147147
}

Modules/_ssl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3348,7 +3348,7 @@ _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self,
33483348
#if HAVE_ALPN
33493349
if ((size_t)protos->len > UINT_MAX) {
33503350
PyErr_Format(PyExc_OverflowError,
3351-
"protocols longer than %d bytes", UINT_MAX);
3351+
"protocols longer than %u bytes", UINT_MAX);
33523352
return NULL;
33533353
}
33543354

Modules/binascii.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
520520
*/
521521
PyErr_Format(Error,
522522
"Invalid base64-encoded string: "
523-
"number of data characters (%d) cannot be 1 more "
523+
"number of data characters (%zd) cannot be 1 more "
524524
"than a multiple of 4",
525525
(bin_data - bin_data_start) / 3 * 4 + 1);
526526
} else {

Modules/socketmodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4102,7 +4102,7 @@ sock_sendto(PySocketSockObject *s, PyObject *args)
41024102
break;
41034103
default:
41044104
PyErr_Format(PyExc_TypeError,
4105-
"sendto() takes 2 or 3 arguments (%d given)",
4105+
"sendto() takes 2 or 3 arguments (%zd given)",
41064106
arglen);
41074107
return NULL;
41084108
}
@@ -4642,7 +4642,7 @@ sock_ioctl(PySocketSockObject *s, PyObject *arg)
46424642
return PyLong_FromUnsignedLong(recv); }
46434643
#endif
46444644
default:
4645-
PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
4645+
PyErr_Format(PyExc_ValueError, "invalid ioctl command %lu", cmd);
46464646
return NULL;
46474647
}
46484648
}

Objects/bytesobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ PyObject *_PyBytes_DecodeEscape(const char *s,
12091209

12101210
if (!errors || strcmp(errors, "strict") == 0) {
12111211
PyErr_Format(PyExc_ValueError,
1212-
"invalid \\x escape at position %d",
1212+
"invalid \\x escape at position %zd",
12131213
s - 2 - (end - len));
12141214
goto failed;
12151215
}

Objects/odictobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ odict_init(PyObject *self, PyObject *args, PyObject *kwds)
15381538
if (len == -1)
15391539
return -1;
15401540
if (len > 1) {
1541-
const char *msg = "expected at most 1 arguments, got %d";
1541+
const char *msg = "expected at most 1 arguments, got %zd";
15421542
PyErr_Format(PyExc_TypeError, msg, len);
15431543
return -1;
15441544
}
@@ -2211,7 +2211,7 @@ mutablemapping_update(PyObject *self, PyObject *args, PyObject *kwargs)
22112211
assert(args == NULL || PyTuple_Check(args));
22122212
len = (args != NULL) ? PyTuple_GET_SIZE(args) : 0;
22132213
if (len > 1) {
2214-
const char *msg = "update() takes at most 1 positional argument (%d given)";
2214+
const char *msg = "update() takes at most 1 positional argument (%zd given)";
22152215
PyErr_Format(PyExc_TypeError, msg, len);
22162216
return NULL;
22172217
}

Objects/structseq.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ structseq_repr(PyStructSequence *obj)
194194

195195
cname = typ->tp_members[i].name;
196196
if (cname == NULL) {
197-
PyErr_Format(PyExc_SystemError, "In structseq_repr(), member %d name is NULL"
197+
PyErr_Format(PyExc_SystemError, "In structseq_repr(), member %zd name is NULL"
198198
" for type %.500s", i, typ->tp_name);
199199
return NULL;
200200
}

PC/launcher.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ invoke_child(wchar_t * executable, wchar_t * suffix, wchar_t * cmdline)
718718
}
719719
child_command = calloc(child_command_size, sizeof(wchar_t));
720720
if (child_command == NULL)
721-
error(RC_CREATE_PROCESS, L"unable to allocate %d bytes for child command.",
721+
error(RC_CREATE_PROCESS, L"unable to allocate %zd bytes for child command.",
722722
child_command_size);
723723
if (no_suffix)
724724
_snwprintf_s(child_command, child_command_size,
@@ -1189,7 +1189,7 @@ maybe_handle_shebang(wchar_t ** argv, wchar_t * cmdline)
11891189

11901190
if (rc == 0) {
11911191
read = fread(buffer, sizeof(char), BUFSIZE, fp);
1192-
debug(L"maybe_handle_shebang: read %d bytes\n", read);
1192+
debug(L"maybe_handle_shebang: read %zd bytes\n", read);
11931193
fclose(fp);
11941194

11951195
if ((read >= 4) && (buffer[3] == '\n') && (buffer[2] == '\r')) {
@@ -1209,7 +1209,7 @@ maybe_handle_shebang(wchar_t ** argv, wchar_t * cmdline)
12091209
bom = BOMs; /* points to UTF-8 entry - the default */
12101210
}
12111211
else {
1212-
debug(L"maybe_handle_shebang: BOM found, code page %d\n",
1212+
debug(L"maybe_handle_shebang: BOM found, code page %u\n",
12131213
bom->code_page);
12141214
start = &buffer[bom->length];
12151215
}

Python/dynload_win.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
254254
This should not happen if called correctly. */
255255
if (theLength == 0) {
256256
message = PyUnicode_FromFormat(
257-
"DLL load failed with error code %d",
257+
"DLL load failed with error code %u",
258258
errorCode);
259259
} else {
260260
/* For some reason a \r\n

Python/getargs.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,14 @@ vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs,
371371
if (nargs < min || max < nargs) {
372372
if (message == NULL)
373373
PyErr_Format(PyExc_TypeError,
374-
"%.150s%s takes %s %d argument%s (%ld given)",
374+
"%.150s%s takes %s %d argument%s (%zd given)",
375375
fname==NULL ? "function" : fname,
376376
fname==NULL ? "" : "()",
377377
min==max ? "exactly"
378378
: nargs < min ? "at least" : "at most",
379379
nargs < min ? min : max,
380380
(nargs < min ? min : max) == 1 ? "" : "s",
381-
Py_SAFE_DOWNCAST(nargs, Py_ssize_t, long));
381+
nargs);
382382
else
383383
PyErr_SetString(PyExc_TypeError, message);
384384
return cleanreturn(0, &freelist);
@@ -1718,7 +1718,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
17181718
else {
17191719
PyErr_Format(PyExc_TypeError,
17201720
"%.200s%s takes %s %d positional arguments"
1721-
" (%d given)",
1721+
" (%zd given)",
17221722
(fname == NULL) ? "function" : fname,
17231723
(fname == NULL) ? "" : "()",
17241724
(min != INT_MAX) ? "at most" : "exactly",
@@ -1797,7 +1797,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
17971797
if (skip) {
17981798
PyErr_Format(PyExc_TypeError,
17991799
"%.200s%s takes %s %d positional arguments"
1800-
" (%d given)",
1800+
" (%zd given)",
18011801
(fname == NULL) ? "function" : fname,
18021802
(fname == NULL) ? "" : "()",
18031803
(Py_MIN(pos, min) < i) ? "at least" : "exactly",
@@ -2103,7 +2103,7 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
21032103
}
21042104
else {
21052105
PyErr_Format(PyExc_TypeError,
2106-
"%.200s%s takes %s %d positional arguments (%d given)",
2106+
"%.200s%s takes %s %d positional arguments (%zd given)",
21072107
(parser->fname == NULL) ? "function" : parser->fname,
21082108
(parser->fname == NULL) ? "" : "()",
21092109
(parser->min != INT_MAX) ? "at most" : "exactly",
@@ -2152,7 +2152,7 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
21522152
Py_ssize_t min = Py_MIN(pos, parser->min);
21532153
PyErr_Format(PyExc_TypeError,
21542154
"%.200s%s takes %s %d positional arguments"
2155-
" (%d given)",
2155+
" (%zd given)",
21562156
(parser->fname == NULL) ? "function" : parser->fname,
21572157
(parser->fname == NULL) ? "" : "()",
21582158
min < parser->max ? "at least" : "exactly",

Python/hamt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,7 @@ hamt_node_array_dump(PyHamtNode_Array *node,
20032003
goto error;
20042004
}
20052005

2006-
if (_hamt_dump_format(writer, "%d::\n", i)) {
2006+
if (_hamt_dump_format(writer, "%zd::\n", i)) {
20072007
goto error;
20082008
}
20092009

Python/pyarena.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ PyArena_Free(PyArena *arena)
160160
#if defined(Py_DEBUG)
161161
/*
162162
fprintf(stderr,
163-
"alloc=%d size=%d blocks=%d block_size=%d big=%d objects=%d\n",
163+
"alloc=%zu size=%zu blocks=%zu block_size=%zu big=%zu objects=%zu\n",
164164
arena->total_allocs, arena->total_size, arena->total_blocks,
165165
arena->total_block_size, arena->total_big_blocks,
166166
PyList_Size(arena->a_objects));

0 commit comments

Comments
 (0)