mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
fix: handle hex escapes in strings and reject invalid escapes
This commit is contained in:
parent
e4130ad12e
commit
394551c39c
@ -280,10 +280,22 @@ static Error* _eat_string(Lexer* self, c11_sbuf* buff, char quote, enum StringTy
|
|||||||
case 'x': {
|
case 'x': {
|
||||||
char hex[3] = {eatchar(self), eatchar(self), '\0'};
|
char hex[3] = {eatchar(self), eatchar(self), '\0'};
|
||||||
int code;
|
int code;
|
||||||
if(sscanf(hex, "%x", &code) != 1) {
|
if (sscanf(hex, "%x", &code) != 1 || code > 0xFF) {
|
||||||
return LexerError(self, "invalid hex char");
|
return LexerError(self, "invalid hex escape");
|
||||||
|
}
|
||||||
|
if (type == NORMAL_BYTES) {
|
||||||
|
// Bytes literals: write raw byte
|
||||||
|
c11_sbuf__write_char(buff, (char)code);
|
||||||
|
} else {
|
||||||
|
// Regular strings: encode as UTF-8
|
||||||
|
if (code <= 0x7F) {
|
||||||
|
c11_sbuf__write_char(buff, (char)code);
|
||||||
|
} else {
|
||||||
|
// Encode as 2-byte UTF-8 for code points 0x80-0xFF
|
||||||
|
c11_sbuf__write_char(buff, 0xC0 | (code >> 6)); // Leading byte
|
||||||
|
c11_sbuf__write_char(buff, 0x80 | (code & 0x3F)); // Continuation byte
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c11_sbuf__write_char(buff, (char)code);
|
|
||||||
} break;
|
} break;
|
||||||
default: return LexerError(self, "invalid escape char");
|
default: return LexerError(self, "invalid escape char");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user