@@ -6271,9 +6271,10 @@ PyUnicode_AsUTF16String(PyObject *unicode)
62716271static _PyUnicode_Name_CAPI * ucnhash_CAPI = NULL ;
62726272
62736273PyObject *
6274- _PyUnicode_DecodeUnicodeEscape (const char * s ,
6274+ _PyUnicode_DecodeUnicodeEscapeInternal (const char * s ,
62756275 Py_ssize_t size ,
62766276 const char * errors ,
6277+ Py_ssize_t * consumed ,
62776278 const char * * first_invalid_escape )
62786279{
62796280 const char * starts = s ;
@@ -6286,6 +6287,9 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
62866287 * first_invalid_escape = NULL ;
62876288
62886289 if (size == 0 ) {
6290+ if (consumed ) {
6291+ * consumed = 0 ;
6292+ }
62896293 _Py_RETURN_UNICODE_EMPTY ();
62906294 }
62916295 /* Escaped strings will always be longer than the resulting
@@ -6336,7 +6340,7 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
63366340 /* \ - Escapes */
63376341 if (s >= end ) {
63386342 message = "\\ at end of string" ;
6339- goto error ;
6343+ goto incomplete ;
63406344 }
63416345 c = (unsigned char ) * s ++ ;
63426346
@@ -6390,7 +6394,10 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
63906394 count = 8 ;
63916395 message = "truncated \\UXXXXXXXX escape" ;
63926396 hexescape :
6393- for (ch = 0 ; count && s < end ; ++ s , -- count ) {
6397+ for (ch = 0 ; count ; ++ s , -- count ) {
6398+ if (s >= end ) {
6399+ goto incomplete ;
6400+ }
63946401 c = (unsigned char )* s ;
63956402 ch <<= 4 ;
63966403 if (c >= '0' && c <= '9' ) {
@@ -6403,12 +6410,9 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
64036410 ch += c - ('A' - 10 );
64046411 }
64056412 else {
6406- break ;
6413+ goto error ;
64076414 }
64086415 }
6409- if (count ) {
6410- goto error ;
6411- }
64126416
64136417 /* when we get here, ch is a 32-bit unicode character */
64146418 if (ch > MAX_UNICODE ) {
@@ -6435,14 +6439,20 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
64356439 }
64366440
64376441 message = "malformed \\N character escape" ;
6438- if (s < end && * s == '{' ) {
6442+ if (s >= end ) {
6443+ goto incomplete ;
6444+ }
6445+ if (* s == '{' ) {
64396446 const char * start = ++ s ;
64406447 size_t namelen ;
64416448 /* look for the closing brace */
64426449 while (s < end && * s != '}' )
64436450 s ++ ;
6451+ if (s >= end ) {
6452+ goto incomplete ;
6453+ }
64446454 namelen = s - start ;
6445- if (namelen && s < end ) {
6455+ if (namelen ) {
64466456 /* found a name. look it up in the unicode database */
64476457 s ++ ;
64486458 ch = 0xffffffff ; /* in case 'getcode' messes up */
@@ -6468,6 +6478,11 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
64686478 continue ;
64696479 }
64706480
6481+ incomplete :
6482+ if (consumed ) {
6483+ * consumed = startinpos ;
6484+ break ;
6485+ }
64716486 error :
64726487 endinpos = s - starts ;
64736488 writer .min_length = end - s + writer .pos ;
@@ -6496,12 +6511,14 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
64966511}
64976512
64986513PyObject *
6499- PyUnicode_DecodeUnicodeEscape (const char * s ,
6514+ _PyUnicode_DecodeUnicodeEscapeStateful (const char * s ,
65006515 Py_ssize_t size ,
6501- const char * errors )
6516+ const char * errors ,
6517+ Py_ssize_t * consumed )
65026518{
65036519 const char * first_invalid_escape ;
6504- PyObject * result = _PyUnicode_DecodeUnicodeEscape (s , size , errors ,
6520+ PyObject * result = _PyUnicode_DecodeUnicodeEscapeInternal (s , size , errors ,
6521+ consumed ,
65056522 & first_invalid_escape );
65066523 if (result == NULL )
65076524 return NULL ;
@@ -6516,6 +6533,14 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
65166533 return result ;
65176534}
65186535
6536+ PyObject *
6537+ PyUnicode_DecodeUnicodeEscape (const char * s ,
6538+ Py_ssize_t size ,
6539+ const char * errors )
6540+ {
6541+ return _PyUnicode_DecodeUnicodeEscapeStateful (s , size , errors , NULL );
6542+ }
6543+
65196544/* Return a Unicode-Escape string version of the Unicode object. */
65206545
65216546PyObject *
0 commit comments