From 83172df776f6203d73cd2ae70151b3c5c9a30427 Mon Sep 17 00:00:00 2001 From: Timerix Date: Sat, 16 Nov 2024 19:36:17 +0500 Subject: [PATCH] sprintf_malloc --- src/VM/VM.c | 14 +++++++++++++- src/VM/VM.h | 10 +++------- src/instructions/impl/SYS.c | 2 +- src/instructions/instructions.c | 2 +- src/instructions/instructions.h | 2 +- src/main.c | 18 ++++++++++++++++++ src/std.h | 5 ++++- 7 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/VM/VM.c b/src/VM/VM.c index a6de2fa..a14b751 100644 --- a/src/VM/VM.c +++ b/src/VM/VM.c @@ -6,6 +6,18 @@ void VM_init(VM* vm){ vm->state = VMState_Initialized; } +void VM_setErrorMessage(VM* vm, const char* format, ...){ + va_list argv; + va_start(argv, format); + char* NULLABLE(buf) = vsprintf_malloc(256, format, argv); + va_end(argv); + if(buf == NULL){ + buf = malloc(16); + strcpy(buf, "SPRINTF FAILED"); + } + vm->error_message = buf; +} + bool VM_loadProgram(VM* vm, u8* data, size_t size){ if(data == NULL){ VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because data == NULL"); @@ -33,7 +45,7 @@ i32 VM_executeProgram(VM* vm){ const Instruction* instr = Instruction_getFromOpcode(opcode); if(instr == NULL){ - + VM_setErrorMessage(vm, "[%p] unknown opcode %02x", (void*)pos, opcode); return -1; } diff --git a/src/VM/VM.h b/src/VM/VM.h index 5b54297..149a6c9 100644 --- a/src/VM/VM.h +++ b/src/VM/VM.h @@ -45,7 +45,7 @@ typedef struct VM { }; VMState state; - NULLABLE(char* error_message); + char* NULLABLE(error_message); u8* data; size_t data_size; @@ -62,10 +62,6 @@ bool VM_loadProgram(VM* vm, u8* data, size_t size); /// @return program exit code or -1 on error (check vm.error_message) i32 VM_executeProgram(VM* vm); -#define VM_setErrorMessage(V, FORMAT, ...) {\ - char* buf = malloc(256);\ - sprintf(buf, FORMAT, ##__VA_ARGS__);\ - vm->error_message = buf;\ -} - bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size); + +void VM_setErrorMessage(VM* vm, const char* format, ...) __attribute__((__format__ (__printf__, 2, 3))); diff --git a/src/instructions/impl/SYS.c b/src/instructions/impl/SYS.c index 825b81e..44813ff 100644 --- a/src/instructions/impl/SYS.c +++ b/src/instructions/impl/SYS.c @@ -1,6 +1,6 @@ #include "impl_macros.h" -NULLABLE(FILE* fileFromN(VM* vm, size_t pos, u32 file_n)){ +FILE* NULLABLE(fileFromN)(VM* vm, size_t pos, u32 file_n){ FILE* f = NULL; switch(file_n){ case 0: f = stdin; break; diff --git a/src/instructions/instructions.c b/src/instructions/instructions.c index 871f09a..9b96dbc 100644 --- a/src/instructions/instructions.c +++ b/src/instructions/instructions.c @@ -16,7 +16,7 @@ const Instruction instructions[] = { }; const size_t instructions_count = sizeof(instructions)/sizeof(instructions[0]); -NULLABLE(const Instruction* Instruction_getFromOpcode(u8 opcode)){ +const Instruction* NULLABLE(Instruction_getFromOpcode)(u8 opcode){ if(opcode >= instructions_count) return NULL; diff --git a/src/instructions/instructions.h b/src/instructions/instructions.h index 332ae40..67a3760 100644 --- a/src/instructions/instructions.h +++ b/src/instructions/instructions.h @@ -18,7 +18,7 @@ typedef struct Instruction { /// @brief get instruction info from table /// @param opcode any byte /// @return ptr to struct or NULL -NULLABLE(const Instruction* Instruction_getFromOpcode(u8 opcode)); +const Instruction* NULLABLE(Instruction_getFromOpcode)(u8 opcode); i32 NOP_impl(VM* vm, size_t pos); i32 PUSH_impl(VM* vm, size_t pos); diff --git a/src/main.c b/src/main.c index b83f590..d916227 100644 --- a/src/main.c +++ b/src/main.c @@ -75,3 +75,21 @@ i32 main(const i32 argc, const char** argv){ free(buffer); return exit_code; } + +char* NULLABLE(sprintf_malloc)(size_t buffer_size, const char* format, ...){ + va_list argv; + va_start(argv, format); + char* NULLABLE(heap_ptr) = vsprintf_malloc(buffer_size, format, argv); + va_end(argv); + return heap_ptr; +} + +char* NULLABLE(vsprintf_malloc)(size_t buffer_size, const char* format, va_list argv){ + char* buf = malloc(buffer_size); + int r = vsprintf_s(buf, buffer_size, format, argv); + if(r < 0){ + free(buf); + return NULL; + } + return buf; +} diff --git a/src/std.h b/src/std.h index 2ac9d64..8296c0b 100644 --- a/src/std.h +++ b/src/std.h @@ -27,4 +27,7 @@ typedef u8 bool; #define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__) /// @warning pointer can be null -#define NULLABLE(DECLARATION) DECLARATION +#define NULLABLE(NAME) NAME + +char* NULLABLE(sprintf_malloc)(size_t buffer_size, const char* format, ...) __attribute__((__format__ (__printf__, 2, 3))); +char* NULLABLE(vsprintf_malloc)(size_t buffer_size, const char* format, va_list argv);