Error reporting for GetNthTag functions
diff --git a/inc/qcbor/qcbor_decode.h b/inc/qcbor/qcbor_decode.h
index 90a1686..1033871 100644
--- a/inc/qcbor/qcbor_decode.h
+++ b/inc/qcbor/qcbor_decode.h
@@ -936,7 +936,7 @@
@param[in] pItem The CBOR item to get the tag for.
@param[in] uIndex The index of the tag to get.
- @returns The actual nth tag value.
+ @returns The actual nth tag value or CBOR_TAG_INVALID64.
Up to @ref QCBOR_MAX_TAGS_PER_ITEM are recorded for a decoded CBOR item. If there
are more than this, the @ref QCBOR_ERR_TOO_MANY_TAGS error is returned
@@ -945,20 +945,35 @@
The 0th tag (@c uIndex 0) is the one that occurs closest to the data item.
Tags nest, so the nth tag applies to what ever type
- is a result of applying the (n-1) tag.
+ is a result of applying the (n-1) tag. See also @ref Tag-Usage.
To reduce memory used by a QCBORItem, this implementation maps
all tags larger than UINT16_MAX. This function does the unmapping.
- This returns @ref CBOR_TAG_INVALID64 on all errors or if the nth tag is requested and
- there is no nth tag. If there are no tags on the item, then
- requesting the 0th tag will return @ref CBOR_TAG_INVALID64.
+ This returns @ref CBOR_TAG_INVALID64 if any error occurred getting
+ the item. This is also returned if there are no tags on the item or
+ there is no nth tag.
*/
uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pCtx, const QCBORItem *pItem, uint32_t uIndex);
+/**
+@brief Returns the tag value for last-fetched item.
+
+@param[in] pCtx The decoder context.
+@param[in] uIndex The index of the tag to get.
+
+@returns The actual nth tag value or CBOR_TAG_INVALID64.
+
+ This is similar to QCBORDecode_GetNthTag(), but works with spiffy decoding
+ functions. These generally do not return a QCBORItem with the tag set.
+ This gets the tags for the most recently decoded item.
+
+ If a decoding error set then this returns CBOR_TAG_INVALID64.
+*/
uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pCtx, uint32_t uIndex);
+
/**
@brief Check whether all the bytes have been decoded and maps and arrays closed.
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 3e36e5a..001a778 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -2193,6 +2193,9 @@
const QCBORItem *pItem,
uint32_t uIndex)
{
+ if(pItem->uDataType == QCBOR_TYPE_NONE) {
+ return CBOR_TAG_INVALID64;
+ }
if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) {
return CBOR_TAG_INVALID64;
} else {
@@ -2206,6 +2209,9 @@
uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe,
uint32_t uIndex)
{
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ return CBOR_TAG_INVALID64;
+ }
if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) {
return CBOR_TAG_INVALID64;
} else {
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index cfa1450..edcaad7 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -2890,6 +2890,11 @@
return -6;
}
+ if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
+ return -106;
+ }
+
+
/* tag 10489608748473423768(
2442302356(
21590(
@@ -5040,10 +5045,15 @@
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
- uErr = QCBORDecode_GetAndResetError(&DCtx);
+ uErr = QCBORDecode_GetError(&DCtx);
if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
return 2021;
}
+ if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
+ return 2121;
+ }
+ (void)QCBORDecode_GetAndResetError(&DCtx);
+
QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
uErr = QCBORDecode_GetAndResetError(&DCtx);