Skip to content

Commit d53fe5f

Browse files
bpo-36254: Fix invalid uses of %d in format strings in C. (GH-12264)
1 parent 10f8ce6 commit d53fe5f

20 files changed

+35
-35
lines changed

Modules/_collectionsmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2388,7 +2388,7 @@ tuplegetter_descr_get(PyObject *self, PyObject *obj, PyObject *type)
23882388
return self;
23892389
}
23902390
PyErr_Format(PyExc_TypeError,
2391-
"descriptor for index '%d' for tuple subclasses "
2391+
"descriptor for index '%zd' for tuple subclasses "
23922392
"doesn't apply to '%s' object",
23932393
index,
23942394
obj->ob_type->tp_name);

Modules/_ctypes/callbacks.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ static void _CallPythonObject(void *mem,
165165
if (cnv)
166166
dict = PyType_stgdict(cnv);
167167
else {
168-
PrintError("Getting argument converter %d\n", i);
168+
PrintError("Getting argument converter %zd\n", i);
169169
goto Done;
170170
}
171171

172172
if (dict && dict->getfunc && !_ctypes_simple_instance(cnv)) {
173173
PyObject *v = dict->getfunc(*pArgs, dict->size);
174174
if (!v) {
175-
PrintError("create argument %d:\n", i);
175+
PrintError("create argument %zd:\n", i);
176176
Py_DECREF(cnv);
177177
goto Done;
178178
}
@@ -186,14 +186,14 @@ static void _CallPythonObject(void *mem,
186186
/* Hm, shouldn't we use PyCData_AtAddress() or something like that instead? */
187187
CDataObject *obj = (CDataObject *)_PyObject_CallNoArg(cnv);
188188
if (!obj) {
189-
PrintError("create argument %d:\n", i);
189+
PrintError("create argument %zd:\n", i);
190190
Py_DECREF(cnv);
191191
goto Done;
192192
}
193193
if (!CDataObject_Check(obj)) {
194194
Py_DECREF(obj);
195195
Py_DECREF(cnv);
196-
PrintError("unexpected result of create argument %d:\n", i);
196+
PrintError("unexpected result of create argument %zd:\n", i);
197197
goto Done;
198198
}
199199
memcpy(obj->b_ptr, *pArgs, dict->size);
@@ -204,7 +204,7 @@ static void _CallPythonObject(void *mem,
204204
} else {
205205
PyErr_SetString(PyExc_TypeError,
206206
"cannot build parameter");
207-
PrintError("Parsing argument %d\n", i);
207+
PrintError("Parsing argument %zd\n", i);
208208
Py_DECREF(cnv);
209209
goto Done;
210210
}

Modules/_ctypes/callproc.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1131,20 +1131,20 @@ PyObject *_ctypes_callproc(PPROC pProc,
11311131
converter = PyTuple_GET_ITEM(argtypes, i);
11321132
v = PyObject_CallFunctionObjArgs(converter, arg, NULL);
11331133
if (v == NULL) {
1134-
_ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
1134+
_ctypes_extend_error(PyExc_ArgError, "argument %zd: ", i+1);
11351135
goto cleanup;
11361136
}
11371137

11381138
err = ConvParam(v, i+1, pa);
11391139
Py_DECREF(v);
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;
11431143
}
11441144
} else {
11451145
err = ConvParam(arg, i+1, pa);
11461146
if (-1 == err) {
1147-
_ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
1147+
_ctypes_extend_error(PyExc_ArgError, "argument %zd: ", i+1);
11481148
goto cleanup; /* leaking ? */
11491149
}
11501150
}

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
@@ -725,7 +725,7 @@ readinto(winconsoleio *self, char *buf, Py_ssize_t len)
725725

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

Modules/_localemodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ PyLocale_getdefaultlocale(PyObject* self, PyObject *Py_UNUSED(ignored))
392392
char encoding[20];
393393
char locale[100];
394394

395-
PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
395+
PyOS_snprintf(encoding, sizeof(encoding), "cp%u", GetACP());
396396

397397
if (GetLocaleInfo(LOCALE_USER_DEFAULT,
398398
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

@@ -622,7 +622,7 @@ Compressor_init_alone(lzma_stream *lzs, uint32_t preset, PyObject *filterspecs)
622622
lzma_options_lzma options;
623623

624624
if (lzma_lzma_preset(&options, preset)) {
625-
PyErr_Format(Error, "Invalid compression preset: %d", preset);
625+
PyErr_Format(Error, "Invalid compression preset: %u", preset);
626626
return -1;
627627
}
628628
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
@@ -3347,7 +3347,7 @@ _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self,
33473347
#if HAVE_ALPN
33483348
if ((size_t)protos->len > UINT_MAX) {
33493349
PyErr_Format(PyExc_OverflowError,
3350-
"protocols longer than %d bytes", UINT_MAX);
3350+
"protocols longer than %u bytes", UINT_MAX);
33513351
return NULL;
33523352
}
33533353

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
@@ -4198,7 +4198,7 @@ sock_sendto(PySocketSockObject *s, PyObject *args)
41984198
break;
41994199
default:
42004200
PyErr_Format(PyExc_TypeError,
4201-
"sendto() takes 2 or 3 arguments (%d given)",
4201+
"sendto() takes 2 or 3 arguments (%zd given)",
42024202
arglen);
42034203
return NULL;
42044204
}
@@ -4741,7 +4741,7 @@ sock_ioctl(PySocketSockObject *s, PyObject *arg)
47414741
return PyLong_FromUnsignedLong(recv); }
47424742
#endif
47434743
default:
4744-
PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
4744+
PyErr_Format(PyExc_ValueError, "invalid ioctl command %lu", cmd);
47454745
return NULL;
47464746
}
47474747
}

Objects/bytesobject.c

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

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

Objects/odictobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ odict_init(PyObject *self, PyObject *args, PyObject *kwds)
15391539
if (len == -1)
15401540
return -1;
15411541
if (len > 1) {
1542-
const char *msg = "expected at most 1 arguments, got %d";
1542+
const char *msg = "expected at most 1 arguments, got %zd";
15431543
PyErr_Format(PyExc_TypeError, msg, len);
15441544
return -1;
15451545
}
@@ -2213,7 +2213,7 @@ mutablemapping_update(PyObject *self, PyObject *args, PyObject *kwargs)
22132213
assert(args == NULL || PyTuple_Check(args));
22142214
len = (args != NULL) ? PyTuple_GET_SIZE(args) : 0;
22152215
if (len > 1) {
2216-
const char *msg = "update() takes at most 1 positional argument (%d given)";
2216+
const char *msg = "update() takes at most 1 positional argument (%zd given)";
22172217
PyErr_Format(PyExc_TypeError, msg, len);
22182218
return NULL;
22192219
}

Objects/structseq.c

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

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

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/coreconfig.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ get_locale_encoding(char **locale_encoding)
11031103
{
11041104
#ifdef MS_WINDOWS
11051105
char encoding[20];
1106-
PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
1106+
PyOS_snprintf(encoding, sizeof(encoding), "cp%u", GetACP());
11071107
#elif defined(__ANDROID__) || defined(__VXWORKS__)
11081108
const char *encoding = "UTF-8";
11091109
#else

Python/dynload_win.c

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

Python/getargs.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,14 @@ vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs,
372372
if (nargs < min || max < nargs) {
373373
if (message == NULL)
374374
PyErr_Format(PyExc_TypeError,
375-
"%.150s%s takes %s %d argument%s (%ld given)",
375+
"%.150s%s takes %s %d argument%s (%zd given)",
376376
fname==NULL ? "function" : fname,
377377
fname==NULL ? "" : "()",
378378
min==max ? "exactly"
379379
: nargs < min ? "at least" : "at most",
380380
nargs < min ? min : max,
381381
(nargs < min ? min : max) == 1 ? "" : "s",
382-
Py_SAFE_DOWNCAST(nargs, Py_ssize_t, long));
382+
nargs);
383383
else
384384
PyErr_SetString(PyExc_TypeError, message);
385385
return cleanreturn(0, &freelist);
@@ -1741,7 +1741,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
17411741
else {
17421742
PyErr_Format(PyExc_TypeError,
17431743
"%.200s%s takes %s %d positional argument%s"
1744-
" (%d given)",
1744+
" (%zd given)",
17451745
(fname == NULL) ? "function" : fname,
17461746
(fname == NULL) ? "" : "()",
17471747
(min != INT_MAX) ? "at most" : "exactly",
@@ -1826,7 +1826,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
18261826
if (skip) {
18271827
PyErr_Format(PyExc_TypeError,
18281828
"%.200s%s takes %s %d positional argument%s"
1829-
" (%d given)",
1829+
" (%zd given)",
18301830
(fname == NULL) ? "function" : fname,
18311831
(fname == NULL) ? "" : "()",
18321832
(Py_MIN(pos, min) < i) ? "at least" : "exactly",
@@ -2194,7 +2194,7 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
21942194
Py_ssize_t min = Py_MIN(pos, parser->min);
21952195
PyErr_Format(PyExc_TypeError,
21962196
"%.200s%s takes %s %d positional argument%s"
2197-
" (%d given)",
2197+
" (%zd given)",
21982198
(parser->fname == NULL) ? "function" : parser->fname,
21992199
(parser->fname == NULL) ? "" : "()",
22002200
min < parser->max ? "at least" : "exactly",

Python/hamt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,7 @@ hamt_node_array_dump(PyHamtNode_Array *node,
20052005
goto error;
20062006
}
20072007

2008-
if (_hamt_dump_format(writer, "%d::\n", i)) {
2008+
if (_hamt_dump_format(writer, "%zd::\n", i)) {
20092009
goto error;
20102010
}
20112011

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)