From d7d74e6999f5fac5757e16d67d8dc9ae4f8ce15d Mon Sep 17 00:00:00 2001 From: Adygzhy Ondar Date: Fri, 27 Oct 2017 00:05:14 +0300 Subject: [PATCH 1/2] tvm_vm_create(): filename parameter is removed. --- libtvm/tvm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libtvm/tvm.c b/libtvm/tvm.c index 46c7a63..1bb69f3 100644 --- a/libtvm/tvm.c +++ b/libtvm/tvm.c @@ -3,7 +3,7 @@ #include #include -struct tvm_ctx *tvm_vm_create(char *filename) +struct tvm_ctx *tvm_vm_create() { struct tvm_ctx *vm = (struct tvm_ctx *)calloc(1, sizeof(struct tvm_ctx)); From 1911acb84f83cb17603be346326cf0094a2f942a Mon Sep 17 00:00:00 2001 From: Adygzhy Ondar Date: Fri, 27 Oct 2017 00:22:36 +0300 Subject: [PATCH 2/2] tvm_vm_create(): fix possible NULL pointer dereferencing. if calloc() doesn't allocate memory for struct tvm_ctx, there may be NULL pointer dereferencing, i.e. vm->mem, vm->prog. So the vm pointer verification must be before it is used. Also, there is the same situation with vm->mem and stack creation. --- libtvm/tvm.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libtvm/tvm.c b/libtvm/tvm.c index 1bb69f3..5839f87 100644 --- a/libtvm/tvm.c +++ b/libtvm/tvm.c @@ -8,14 +8,17 @@ struct tvm_ctx *tvm_vm_create() struct tvm_ctx *vm = (struct tvm_ctx *)calloc(1, sizeof(struct tvm_ctx)); + if (!vm) + return NULL; vm->mem = tvm_mem_create(MIN_MEMORY_SIZE); vm->prog = tvm_prog_create(); - tvm_stack_create(vm->mem, MIN_STACK_SIZE); - - if (!vm || !vm->mem || !vm->prog) + if (!vm->mem || !vm->prog) { + tvm_vm_destroy(vm); return NULL; + } + tvm_stack_create(vm->mem, MIN_STACK_SIZE); return vm; }