NULL pointers everywhere

This commit is contained in:
Giulio De Pasquale 2017-05-14 23:01:11 +02:00
parent f818b5548e
commit f1060ff86c
3 changed files with 16 additions and 2 deletions

View File

@ -8,7 +8,8 @@
using namespace std;
int main() {
VM vm((uint8_t*)"\x00\x00\x50\x50\x03\x10\x00\x00\x02\x04\x10\x00\x04\x04\x01\x00\x13", 107);
uint8_t bytecode[] = "\x00\x00\x00";
VM vm(bytecode, sizeof(bytecode));
vm.run();
printf("\n\n");
vm.status();

View File

@ -177,7 +177,7 @@ void VM::run(void) {
break;
default:
DBG_INFO(("WAT: 0x%x\n", opcode));
return;
finished = true;
break;
}
}

View File

@ -5,6 +5,9 @@
#include <string.h>
VMAddrSpace::VMAddrSpace() {
stack = NULL;
code = NULL;
data = NULL;
stacksize = DEFAULT_STACKSIZE;
codesize = DEFAULT_CODESIZE;
datasize = DEFAULT_DATASIZE;
@ -12,6 +15,9 @@ VMAddrSpace::VMAddrSpace() {
}
VMAddrSpace::VMAddrSpace(uint32_t ss, uint32_t cs, uint32_t ds) {
stack = NULL;
code = NULL;
data = NULL;
stacksize = ss;
codesize = cs;
datasize = ds;
@ -21,12 +27,15 @@ VMAddrSpace::VMAddrSpace(uint32_t ss, uint32_t cs, uint32_t ds) {
bool VMAddrSpace::allocate(void) {
DBG_INFO(("Allocating sections...\n"));
if (code == NULL) {
DBG_INFO(("\tcode...\n"));
code = (uint8_t *)malloc(codesize);
}
if (data == NULL) {
DBG_INFO(("\tdata...\n"));
data = (uint8_t *)malloc(datasize);
}
if (stack == NULL) {
DBG_INFO(("\tstack...\n"));
stack = (uint8_t *)malloc(stacksize);
}
if (code == NULL) {
@ -43,6 +52,10 @@ bool VMAddrSpace::allocate(void) {
DBG_ERROR(("Couldn't allocate stack section.\n"));
return false;
}
memset(code, 0xff,
stacksize); // auto halt in case the assembly is not correct
memset(stack, 0x0, stacksize);
memset(data, 0x0, stacksize);
DBG_SUCC(("Done!\n"));
return true;
}