sprintf_malloc

This commit is contained in:
2024-11-16 19:36:17 +05:00
parent b8296a45a7
commit 83172df776
7 changed files with 41 additions and 12 deletions

View File

@@ -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;
}