Autotest: test the error conditions in the parser and the encoder
The encoder only supports two error conditions: out of memory (need more
buffer) and illegal simple types. It's possible for the encoder to
encode an array or map with a size the decoder can't parse (UINT32_MAX),
but that's not an error condition.
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
diff --git a/src/cbor.h b/src/cbor.h
index 6db2bac..4d1ae4b 100644
--- a/src/cbor.h
+++ b/src/cbor.h
@@ -102,7 +102,6 @@
/* parser errors streaming errors */
CborErrorGarbageAtEnd = 256,
CborErrorUnexpectedEOF,
- CborErrorBreakMissingAtEOF, /* special case of UnexpectedEOF */
CborErrorUnexpectedBreak,
CborErrorUnknownType, /* can only heppen in major type 7 */
CborErrorIllegalType, /* type not allowed here */
diff --git a/src/cborerrorstrings.c b/src/cborerrorstrings.c
index 95ff0aa..25e6a58 100644
--- a/src/cborerrorstrings.c
+++ b/src/cborerrorstrings.c
@@ -52,9 +52,6 @@
case CborErrorUnexpectedEOF:
return _("unexpected end of data");
- case CborErrorBreakMissingAtEOF:
- return _("'break' byte missing before end of document");
-
case CborErrorUnexpectedBreak:
return _("unexpected 'break' byte");
diff --git a/src/cborparser.c b/src/cborparser.c
index b7df8f1..03bb554 100644
--- a/src/cborparser.c
+++ b/src/cborparser.c
@@ -139,7 +139,7 @@
if (descriptor > Value64Bit) {
if (unlikely(descriptor != IndefiniteLength))
- return CborErrorIllegalNumber;
+ return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber;
if (likely(!is_fixed_type(type))) {
// special case
it->flags |= CborIteratorFlag_UnknownLength;
@@ -398,12 +398,18 @@
assert(err == CborNoError);
recursed->remaining = len;
- if (recursed->remaining != len || len == UINT32_MAX)
+ if (recursed->remaining != len || len == UINT32_MAX) {
+ // back track the pointer to indicate where the error occurred
+ recursed->ptr = it->ptr;
return CborErrorDataTooLarge;
+ }
if (recursed->type == CborMapType) {
// maps have keys and values, so we need to multiply by 2
- if (recursed->remaining > UINT32_MAX / 2)
+ if (recursed->remaining > UINT32_MAX / 2) {
+ // back track the pointer to indicate where the error occurred
+ recursed->ptr = it->ptr;
return CborErrorDataTooLarge;
+ }
recursed->remaining *= 2;
}
if (len != 0)