more spiffy decoded fixes for empty maps and arrays and other error conditions
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 3d41238..57b9bce 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -2475,15 +2475,6 @@
memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags));
}
-/*
-static inline void CopyAllButOneTags(QCBORDecodeContext *pMe, const QCBORItem *pItem)
-{
- const size_t uSizeLessOne = (QCBOR_MAX_TAGS_PER_ITEM - 1) * sizeof(pItem->uTags[0]);
-
- memcpy(pMe->uLastTags, &(pItem->uTags[1]), uSizeLessOne);
- pMe->uLastTags[QCBOR_MAX_TAGS_PER_ITEM - 1] = CBOR_TAG_INVALID16;
-} */
-
/*
Consume an entire map or array (and do next to
@@ -2499,8 +2490,11 @@
DecodeNesting_Print(&(pMe->nesting), &(pMe->InBuf), "ConsumeItem");
- if(QCBORItem_IsMapOrArray(pItemToConsume)) {
- /* There is only real work to do for maps and arrays */
+ // If it is a map or array, this will tell if it is empty.
+ const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel);
+
+ if(QCBORItem_IsMapOrArray(pItemToConsume) && !bIsEmpty) {
+ /* There is only real work to do for non-empty maps and arrays */
/* This works for definite and indefinite length
* maps and arrays by using the nesting level
@@ -2670,6 +2664,10 @@
// TODO: also bail out on implementation limits like array too big
goto Done;
}
+ if(uReturn == QCBOR_ERR_NO_MORE_ITEMS) {
+ // Unexpected end of map or array.
+ goto Done;
+ }
/* See if item has one of the labels that are of interest */
bool bMatched = false;
@@ -3124,8 +3122,6 @@
return;
}
- CopyTags(pMe, pSearch);
-
/* Need to get the current pre-order nesting level and cursor to be
at the map/array about to be entered.
@@ -3219,7 +3215,7 @@
return;
}
- /* Get the data item that is the map that is being searched */
+ /* Get the data item that is the map or array being entered. */
QCBORItem Item;
uErr = QCBORDecode_GetNext(pMe, &Item);
if(uErr != QCBOR_SUCCESS) {
@@ -3230,6 +3226,9 @@
goto Done;
}
+ CopyTags(pMe, &Item);
+
+
const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel);
if(bIsEmpty) {
if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) {