bpo-29568: Disable any characters between two percents for escaped percent "%%" in the format string for classic string formatting. (GH-513)
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index d3516fa..58899ad 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -14617,12 +14617,6 @@
if (ctx->fmtcnt == 0)
ctx->writer.overallocate = 0;
- if (arg->ch == '%') {
- if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0)
- return -1;
- return 1;
- }
-
v = unicode_format_getnextarg(ctx);
if (v == NULL)
return -1;
@@ -14882,6 +14876,13 @@
int ret;
arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos);
+ if (arg.ch == '%') {
+ ctx->fmtpos++;
+ ctx->fmtcnt--;
+ if (_PyUnicodeWriter_WriteCharInline(&ctx->writer, '%') < 0)
+ return -1;
+ return 0;
+ }
arg.flags = 0;
arg.width = -1;
arg.prec = -1;
@@ -14903,7 +14904,7 @@
return -1;
}
- if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') {
+ if (ctx->dict && (ctx->argidx < ctx->arglen)) {
PyErr_SetString(PyExc_TypeError,
"not all arguments converted during string formatting");
return -1;