mirror of
https://github.com/pocketpy/pocketpy
synced 2026-06-23 00:47:11 +08:00
fix lexer EOF OOB read and list.sort comparator signature
This commit is contained in:
parent
db8d52cab0
commit
c18322444e
@ -37,16 +37,24 @@ static void Lexer__dtor(Lexer* self) {
|
|||||||
c11_vector__dtor(&self->indents);
|
c11_vector__dtor(&self->indents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char* lexer_source_end(Lexer* self) {
|
||||||
|
return self->src->source->data + self->src->source->size;
|
||||||
|
}
|
||||||
|
|
||||||
static char eatchar(Lexer* self) {
|
static char eatchar(Lexer* self) {
|
||||||
|
const char* end = lexer_source_end(self);
|
||||||
|
if(self->curr_char > end) return '\0';
|
||||||
char c = *self->curr_char;
|
char c = *self->curr_char;
|
||||||
assert(c != '\n'); // eatchar() cannot consume a newline
|
assert(c != '\n'); // eatchar() cannot consume a newline
|
||||||
self->curr_char++;
|
self->curr_char = (self->curr_char < end) ? self->curr_char + 1 : end + 1;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char eatchar_include_newline(Lexer* self) {
|
static char eatchar_include_newline(Lexer* self) {
|
||||||
|
const char* end = lexer_source_end(self);
|
||||||
|
if(self->curr_char > end) return '\0';
|
||||||
char c = *self->curr_char;
|
char c = *self->curr_char;
|
||||||
self->curr_char++;
|
self->curr_char = (self->curr_char < end) ? self->curr_char + 1 : end + 1;
|
||||||
if(c == '\n') {
|
if(c == '\n') {
|
||||||
self->current_line++;
|
self->current_line++;
|
||||||
c11_vector__push(const char*, &self->src->line_starts, self->curr_char);
|
c11_vector__push(const char*, &self->src->line_starts, self->curr_char);
|
||||||
@ -189,7 +197,8 @@ static Error* LexerError(Lexer* self, const char* fmt, ...) {
|
|||||||
err->src = self->src;
|
err->src = self->src;
|
||||||
PK_INCREF(self->src);
|
PK_INCREF(self->src);
|
||||||
err->lineno = self->current_line;
|
err->lineno = self->current_line;
|
||||||
if(*self->curr_char == '\n') { err->lineno--; }
|
const char* end = lexer_source_end(self);
|
||||||
|
if(self->curr_char <= end && *self->curr_char == '\n') { err->lineno--; }
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
vsnprintf(err->msg, sizeof(err->msg), fmt, args);
|
vsnprintf(err->msg, sizeof(err->msg), fmt, args);
|
||||||
|
|||||||
@ -384,7 +384,10 @@ static bool list_insert(int argc, py_Ref argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lt_with_key(py_TValue* a, py_TValue* b, py_TValue* key) {
|
static int lt_with_key(const void* a_, const void* b_, void* extra) {
|
||||||
|
py_TValue* a = (py_TValue*)a_;
|
||||||
|
py_TValue* b = (py_TValue*)b_;
|
||||||
|
py_TValue* key = (py_TValue*)extra;
|
||||||
if(!key) return py_less(a, b);
|
if(!key) return py_less(a, b);
|
||||||
VM* vm = pk_current_vm;
|
VM* vm = pk_current_vm;
|
||||||
// project a
|
// project a
|
||||||
@ -416,7 +419,7 @@ static bool list_sort(int argc, py_Ref argv) {
|
|||||||
bool ok = c11__stable_sort(self->data,
|
bool ok = c11__stable_sort(self->data,
|
||||||
self->length,
|
self->length,
|
||||||
sizeof(py_TValue),
|
sizeof(py_TValue),
|
||||||
(int (*)(const void*, const void*, void*))lt_with_key,
|
lt_with_key,
|
||||||
key);
|
key);
|
||||||
if(!ok) return false;
|
if(!ok) return false;
|
||||||
|
|
||||||
|
|||||||
@ -76,3 +76,10 @@ assert x == 33
|
|||||||
|
|
||||||
# test removing trailing newlines
|
# test removing trailing newlines
|
||||||
assert eval('[1, 2, 3]\n \n') == [1, 2, 3]
|
assert eval('[1, 2, 3]\n \n') == [1, 2, 3]
|
||||||
|
|
||||||
|
# lexer doesn't read past NUL on error at end of input
|
||||||
|
try:
|
||||||
|
eval('"\\x4')
|
||||||
|
exit(1)
|
||||||
|
except SyntaxError:
|
||||||
|
pass
|
||||||
Loading…
x
Reference in New Issue
Block a user