diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-04-30 10:16:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-30 10:16:27 (GMT) |
commit | 3483299a24e41a7f2e958369cb3573d7c2253e33 (patch) | |
tree | f51b90e97184249f71c6df08579a2f4a8899d43a /Objects | |
parent | a055dac0b45031878a8196a8735522de018491e3 (diff) | |
download | cpython-3483299a24e41a7f2e958369cb3573d7c2253e33.zip cpython-3483299a24e41a7f2e958369cb3573d7c2253e33.tar.gz cpython-3483299a24e41a7f2e958369cb3573d7c2253e33.tar.bz2 |
gh-81548: Deprecate octal escape sequences with value larger than 0o377 (GH-91668)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/bytesobject.c | 29 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 29 |
2 files changed, 48 insertions, 10 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 510a836..b5066d0 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -1113,6 +1113,12 @@ PyObject *_PyBytes_DecodeEscape(const char *s, if (s < end && '0' <= *s && *s <= '7') c = (c<<3) + *s++ - '0'; } + if (c > 0377) { + if (*first_invalid_escape == NULL) { + *first_invalid_escape = s-3; /* Back up 3 chars, since we've + already incremented s. */ + } + } *p++ = c; break; case 'x': @@ -1179,11 +1185,24 @@ PyObject *PyBytes_DecodeEscape(const char *s, if (result == NULL) return NULL; if (first_invalid_escape != NULL) { - if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "invalid escape sequence '\\%c'", - (unsigned char)*first_invalid_escape) < 0) { - Py_DECREF(result); - return NULL; + unsigned char c = *first_invalid_escape; + if ('4' <= c && c <= '7') { + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "invalid octal escape sequence '\\%.3s'", + first_invalid_escape) < 0) + { + Py_DECREF(result); + return NULL; + } + } + else { + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "invalid escape sequence '\\%c'", + c) < 0) + { + Py_DECREF(result); + return NULL; + } } } return result; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 7768f66..4933075 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -6404,6 +6404,12 @@ _PyUnicode_DecodeUnicodeEscapeInternal(const char *s, ch = (ch<<3) + *s++ - '0'; } } + if (ch > 0377) { + if (*first_invalid_escape == NULL) { + *first_invalid_escape = s-3; /* Back up 3 chars, since we've + already incremented s. */ + } + } WRITE_CHAR(ch); continue; @@ -6554,11 +6560,24 @@ _PyUnicode_DecodeUnicodeEscapeStateful(const char *s, if (result == NULL) return NULL; if (first_invalid_escape != NULL) { - if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "invalid escape sequence '\\%c'", - (unsigned char)*first_invalid_escape) < 0) { - Py_DECREF(result); - return NULL; + unsigned char c = *first_invalid_escape; + if ('4' <= c && c <= '7') { + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "invalid octal escape sequence '\\%.3s'", + first_invalid_escape) < 0) + { + Py_DECREF(result); + return NULL; + } + } + else { + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "invalid escape sequence '\\%c'", + c) < 0) + { + Py_DECREF(result); + return NULL; + } } } return result; |