index checks fix

This commit is contained in:
ProgramSnail 2024-11-14 14:14:33 +03:00
parent b198e11416
commit b52a7fcb29
3 changed files with 27 additions and 23 deletions

View file

@ -14,9 +14,9 @@ typedef struct {
char *code_ptr; /* A pointer to the bytecode itself */ char *code_ptr; /* A pointer to the bytecode itself */
int *global_ptr; /* A pointer to the global area */ int *global_ptr; /* A pointer to the global area */
int code_size; /* The size (in bytes) of code */ int code_size; /* The size (in bytes) of code */
int stringtab_size; /* The size (in bytes) of the string table */ uint stringtab_size; /* The size (in bytes) of the string table */
int global_area_size; /* The size (in words) of global area */ uint global_area_size; /* The size (in words) of global area */
int public_symbols_number; /* The number of public symbols */ uint public_symbols_number; /* The number of public symbols */
char buffer[0]; char buffer[0];
} bytefile; } bytefile;
@ -36,7 +36,7 @@ static inline const char *get_string(const bytefile *f, size_t pos) {
/* Gets a name for a public symbol */ /* Gets a name for a public symbol */
static inline const char *get_public_name(const bytefile *f, size_t i) { static inline const char *get_public_name(const bytefile *f, size_t i) {
if (i * 2 >= f->public_symbols_number) { if (i >= f->public_symbols_number) {
failure("public number is out of range: %zu >= %i\n", i * 2, failure("public number is out of range: %zu >= %i\n", i * 2,
f->public_symbols_number); f->public_symbols_number);
} }
@ -45,7 +45,7 @@ static inline const char *get_public_name(const bytefile *f, size_t i) {
/* Gets an offset for a publie symbol */ /* Gets an offset for a publie symbol */
static inline size_t get_public_offset(const bytefile *f, size_t i) { static inline size_t get_public_offset(const bytefile *f, size_t i) {
if (i * 2 + 1 >= f->public_symbols_number) { if (i + 1 >= f->public_symbols_number) {
failure("public number is out of range: %zu >= %i\n", i * 2 + 1, failure("public number is out of range: %zu >= %i\n", i * 2 + 1,
f->public_symbols_number); f->public_symbols_number);
} }

View file

@ -424,19 +424,20 @@ void run(bytefile *bf, int argc, char **argv) {
case CMD_BUILTIN_Barray: { // CALL Barray %d case CMD_BUILTIN_Barray: { // CALL Barray %d
size_t elem_count = ip_read_int(&s.ip); size_t elem_count = ip_read_int(&s.ip);
// void **opr_buffer = elem_count > BUFFER_SIZE void **opr_buffer = elem_count > BUFFER_SIZE
// ? alloc(elem_count * sizeof(void *)) ? alloc(elem_count * sizeof(void *))
// : buffer; : buffer;
// for (size_t i = 0; i < elem_count; ++i) { for (size_t i = 0; i < elem_count; ++i) {
// opr_buffer[elem_count - i - 1] = s_pop(); opr_buffer[elem_count - i - 1] = s_pop();
// } }
s_rotate_n(elem_count); // s_rotate_n(elem_count);
// void *array = void *array =
// Barray((aint *)opr_buffer, Barray((aint *)opr_buffer,
// BOX(elem_count)); // NOTE: not shure if elems should be BOX(elem_count)); // NOTE: not shure if elems should be
// added // added
void *array = Barray((aint *)s_peek(), BOX(elem_count));
// void *array = Barray((aint *)s_peek(), BOX(elem_count));
s_push(array); s_push(array);
break; break;
} }

View file

@ -49,14 +49,17 @@ bytefile* read_file (char *fname) {
if (file->buffer + public_symbols_size >= file_end) { if (file->buffer + public_symbols_size >= file_end) {
failure ("public symbols are out of the file size\n"); failure ("public symbols are out of the file size\n");
} }
file->string_ptr = &file->buffer[public_symbols_size];
if (file->string_ptr + file->stringtab_size > file_end) { if (file->string_ptr + file->stringtab_size > file_end) {
failure ("strings table is out of the file size\n"); failure ("strings table is out of the file size\n");
} }
if (file->stringtab_size > 0 && file->string_ptr[file->stringtab_size - 1] != 0) {
failure ("strings table is not zero-ended\n");
}
if (file->code_size < 0 || public_symbols_size < 0 || file->stringtab_size < 0) { if (file->code_size < 0 || public_symbols_size < 0 || file->stringtab_size < 0) {
failure ("file zones sizes should be >= 0\n"); failure ("file zones sizes should be >= 0\n");
} }
file->string_ptr = &file->buffer [public_symbols_size];
file->public_ptr = (int*) file->buffer; file->public_ptr = (int*) file->buffer;
file->code_ptr = &file->string_ptr [file->stringtab_size]; file->code_ptr = &file->string_ptr [file->stringtab_size];
file->global_ptr = (int*) calloc (file->global_area_size, sizeof (int)); file->global_ptr = (int*) calloc (file->global_area_size, sizeof (int));