Improve boolean decoding error handling
Also more tests for boolean decoding
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 76540c5..d1be74d 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -3679,76 +3679,70 @@
-
-
-
-
-static QCBORError
-InterpretBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool)
+static inline void
+ProcessBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool)
{
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ /* Already in error state, do nothing */
+ return;
+ }
+
switch(pItem->uDataType) {
case QCBOR_TYPE_TRUE:
*pBool = true;
- return QCBOR_SUCCESS;
break;
case QCBOR_TYPE_FALSE:
*pBool = false;
- return QCBOR_SUCCESS;
break;
default:
- return QCBOR_ERR_UNEXPECTED_TYPE;
+ pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
break;
}
CopyTags(pMe, pItem);
}
-
/*
- Public function, see header qcbor/qcbor_decode.h file
-*/
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue)
{
if(pMe->uLastError != QCBOR_SUCCESS) {
- // Already in error state, do nothing
+ /* Already in error state, do nothing */
return;
}
- QCBORError nError;
QCBORItem Item;
- nError = QCBORDecode_GetNext(pMe, &Item);
- if(nError != QCBOR_SUCCESS) {
- pMe->uLastError = (uint8_t)nError;
- return;
- }
- pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue);
+ pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item);
+
+ ProcessBool(pMe, &Item, pValue);
}
/*
- Public function, see header qcbor/qcbor_decode.h file
-*/
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue)
{
QCBORItem Item;
QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
- pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue);
+ ProcessBool(pMe, &Item, pValue);
}
/*
- Public function, see header qcbor/qcbor_decode.h file
-*/
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue)
{
QCBORItem Item;
QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
- pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue);
+ ProcessBool(pMe, &Item, pValue);
}
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index 6e1cc3b..01a87e5 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -7766,3 +7766,70 @@
return 0;
}
+
+
+
+
+static const uint8_t spBooleansInMap[] =
+{
+ 0xa1, 0x08, 0xf5
+};
+
+static const uint8_t spBooleansInMapWrongType[] =
+{
+ 0xa1, 0x08, 0xf6
+};
+
+static const uint8_t spBooleansInMapNWF[] =
+{
+ 0xa1, 0x08, 0x1a
+};
+
+
+int32_t BoolTest(void)
+{
+ QCBORDecodeContext DCtx;
+ bool b;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), 0);
+
+ QCBORDecode_EnterMap(&DCtx, NULL);
+ QCBORDecode_GetBool(&DCtx, &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
+ return 1;
+ }
+
+ QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
+ return 2;
+ }
+
+ QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
+ return 3;
+ }
+
+
+ QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
+ return 4;
+ }
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), 0);
+
+ QCBORDecode_EnterMap(&DCtx, NULL);
+ QCBORDecode_GetBool(&DCtx, &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
+ return 5;
+ }
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), 0);
+
+ QCBORDecode_EnterMap(&DCtx, NULL);
+ QCBORDecode_GetBool(&DCtx, &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
+ return 6;
+ }
+
+ return 0;
+}
diff --git a/test/qcbor_decode_tests.h b/test/qcbor_decode_tests.h
index 53f6bd2..54e125a 100644
--- a/test/qcbor_decode_tests.h
+++ b/test/qcbor_decode_tests.h
@@ -308,5 +308,10 @@
int32_t PeekAndRewindTest(void);
+/*
+Test decoding of booleans
+*/
+int32_t BoolTest(void);
+
#endif /* defined(__QCBOR__qcbort_decode_tests__) */
diff --git a/test/run_tests.c b/test/run_tests.c
index cc9c70b..53d83f6 100644
--- a/test/run_tests.c
+++ b/test/run_tests.c
@@ -137,6 +137,7 @@
TEST_ENTRY(ExponentAndMantissaEncodeTests),
#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
TEST_ENTRY(ParseEmptyMapInMapTest),
+ TEST_ENTRY(BoolTest)
};