summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-03-11 11:10:36 +0100
committerJo-Philipp Wich <jo@mein.io>2021-03-11 11:33:21 +0100
commit40fed16a0beb0eb4af251f6375bd9002cf05e8c5 (patch)
tree7713a42ea1e176be0687221a48a9d60ad97250a9
parentab8090d39f3c9c750b093621813292bd5bf7c372 (diff)
vm: gracefully handle property setting on non-array, non-object values
Before this fix, the VM aborted due to an assert in libjson-c when an attempt was made to set a property on a non-object value. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--tests/03_bugs/04_property_set_abort76
-rw-r--r--vm.c20
2 files changed, 95 insertions, 1 deletions
diff --git a/tests/03_bugs/04_property_set_abort b/tests/03_bugs/04_property_set_abort
new file mode 100644
index 0000000..8af477f
--- /dev/null
+++ b/tests/03_bugs/04_property_set_abort
@@ -0,0 +1,76 @@
+When attempting to set a property on a non-array, non-object value the
+VM aborted due to an assert triggered by libjson-c.
+
+-- Testcase --
+{% (null).x = 1 %}
+-- End --
+
+-- Expect stderr --
+Type error: attempt to set property on null value
+In line 1, byte 15:
+
+ `{% (null).x = 1 %}`
+ Near here ----^
+
+
+-- End --
+
+
+-- Testcase --
+{% (1).x = 1 %}
+-- End --
+
+-- Expect stderr --
+Type error: attempt to set property on integer value
+In line 1, byte 12:
+
+ `{% (1).x = 1 %}`
+ Near here -^
+
+
+-- End --
+
+
+-- Testcase --
+{% (1.2).x = 1 %}
+-- End --
+
+-- Expect stderr --
+Type error: attempt to set property on double value
+In line 1, byte 14:
+
+ `{% (1.2).x = 1 %}`
+ Near here ---^
+
+
+-- End --
+
+
+-- Testcase --
+{% (true).x = 1 %}
+-- End --
+
+-- Expect stderr --
+Type error: attempt to set property on boolean value
+In line 1, byte 15:
+
+ `{% (true).x = 1 %}`
+ Near here ----^
+
+
+-- End --
+
+
+-- Testcase --
+{% ("test").x = 1 %}
+-- End --
+
+-- Expect stderr --
+Type error: attempt to set property on string value
+In line 1, byte 17:
+
+ `{% ("test").x = 1 %}`
+ Near here ------^
+
+
+-- End --
diff --git a/vm.c b/vm.c
index ca0435a..7fb7078 100644
--- a/vm.c
+++ b/vm.c
@@ -1096,7 +1096,25 @@ uc_vm_insn_store_val(uc_vm *vm, enum insn_type insn)
json_object *k = uc_vm_stack_pop(vm);
json_object *o = uc_vm_stack_pop(vm);
- uc_vm_stack_push(vm, uc_setval(o, k, v));
+ const char *typenames[] = {
+ [json_type_string] = "string",
+ [json_type_int] = "integer",
+ [json_type_double] = "double",
+ [json_type_boolean] = "boolean",
+ [json_type_null] = "null"
+ };
+
+ switch (json_object_get_type(o)) {
+ case json_type_object:
+ case json_type_array:
+ uc_vm_stack_push(vm, uc_setval(o, k, v));
+ break;
+
+ default:
+ uc_vm_raise_exception(vm, EXCEPTION_TYPE,
+ "attempt to set property on %s value",
+ typenames[json_object_get_type(o)]);
+ }
uc_value_put(o);
uc_value_put(k);