Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fa0b89

Browse files
author
Fredrik Lundh
committed
changed \x to consume exactly two hex digits. implements PEP-223
for 8-bit strings.
1 parent 510c97b commit 1fa0b89

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

Python/compile.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -873,10 +873,11 @@ parsestr(char *s)
873873
return PyUnicode_DecodeUnicodeEscape(
874874
s, len, NULL);
875875
}
876-
else if (rawmode || strchr(s, '\\') == NULL) {
876+
if (rawmode || strchr(s, '\\') == NULL)
877877
return PyString_FromStringAndSize(s, len);
878-
}
879878
v = PyString_FromStringAndSize((char *)NULL, len);
879+
if (v == NULL)
880+
return NULL;
880881
p = buf = PyString_AsString(v);
881882
end = s + len;
882883
while (s < end) {
@@ -909,24 +910,35 @@ parsestr(char *s)
909910
*p++ = c;
910911
break;
911912
case 'x':
912-
if (isxdigit(Py_CHARMASK(*s))) {
913+
if (isxdigit(Py_CHARMASK(s[0])) && isxdigit(Py_CHARMASK(s[1]))) {
913914
unsigned int x = 0;
914-
do {
915-
c = Py_CHARMASK(*s);
916-
s++;
917-
x = (x<<4) & ~0xF;
918-
if (isdigit(c))
919-
x += c - '0';
920-
else if (islower(c))
921-
x += 10 + c - 'a';
922-
else
923-
x += 10 + c - 'A';
924-
} while (isxdigit(Py_CHARMASK(*s)));
915+
c = Py_CHARMASK(*s);
916+
s++;
917+
if (isdigit(c))
918+
x = c - '0';
919+
else if (islower(c))
920+
x = 10 + c - 'a';
921+
else
922+
x = 10 + c - 'A';
923+
x = x << 4;
924+
c = Py_CHARMASK(*s);
925+
s++;
926+
if (isdigit(c))
927+
x += c - '0';
928+
else if (islower(c))
929+
x += 10 + c - 'a';
930+
else
931+
x += 10 + c - 'A';
925932
*p++ = x;
926933
break;
927934
}
928-
/* FALLTHROUGH */
929-
default: *p++ = '\\'; *p++ = s[-1]; break;
935+
PyErr_SetString(PyExc_ValueError, "invalid \\x escape");
936+
Py_DECREF(v);
937+
return NULL;
938+
default:
939+
*p++ = '\\';
940+
*p++ = s[-1];
941+
break;
930942
}
931943
}
932944
_PyString_Resize(&v, (int)(p - buf));

0 commit comments

Comments
 (0)