From 5bbd50785857c0c415469b9ae4848294b5318a29 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 5 Apr 2020 17:38:14 -0500 Subject: [PATCH] Address #336 case 3 Fix error condition for bad abstract types - don't return NULL, panic. --- src/core/marsh.c | 5 +++-- test/suite8.janet | 1 + tools/afl/generate_unmarshal_testcases.janet | 14 +++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/core/marsh.c b/src/core/marsh.c index cf87b187..f0728d98 100644 --- a/src/core/marsh.c +++ b/src/core/marsh.c @@ -1098,7 +1098,7 @@ static const uint8_t *unmarshal_one_abstract(UnmarshalState *st, const uint8_t * Janet key; data = unmarshal_one(st, data, &key, flags + 1); const JanetAbstractType *at = janet_get_abstract_type(key); - if (at == NULL) return NULL; + if (at == NULL) goto oops; if (at->unmarshal) { JanetMarshalContext context = {NULL, st, flags, data, at}; *out = janet_wrap_abstract(at->unmarshal(&context)); @@ -1107,7 +1107,8 @@ static const uint8_t *unmarshal_one_abstract(UnmarshalState *st, const uint8_t * } return context.data; } - return NULL; +oops: + janet_panic("invalid abstract type"); } static const uint8_t *unmarshal_one( diff --git a/test/suite8.janet b/test/suite8.janet index 0c573d91..8b9e0ea0 100644 --- a/test/suite8.janet +++ b/test/suite8.janet @@ -212,5 +212,6 @@ (assert-error "unmarshal errors 1" (unmarshal @"\xd6\xb9\xb9")) (assert-error "unmarshal errors 2" (unmarshal @"\xd7bc")) +(assert-error "unmarshal errors 3" (unmarshal "\xd3\x01\xd9\x01\x62\xcf\x03\x78\x79\x7a" load-image-dict)) (end-suite) diff --git a/tools/afl/generate_unmarshal_testcases.janet b/tools/afl/generate_unmarshal_testcases.janet index d04e1890..d1eef322 100644 --- a/tools/afl/generate_unmarshal_testcases.janet +++ b/tools/afl/generate_unmarshal_testcases.janet @@ -8,15 +8,15 @@ (def cases [ nil - + "abc" - + :def - + 'hij - + 123 - + (int/s64 123) "7" @@ -28,12 +28,12 @@ {:a 123} @{:b 'xyz} - + (peg/compile '{:a (* "a" :b "a") :b (* "b" (+ :a 0) "b") :main (* "(" :b ")")}) - + (fn f [a] (fn [] {:ab a})) (fn f [a] (print "hello world!"))