Fix enum conversion warnings for LLVM/XCode 11 (#44)
* Fix enum conversion warnings for LLVM/XCode 11
* Add int conversion warning back in to optional CFLAGS
* type conversion and integer overflow fix when decoding maps as arrays
* add test for map that is too large to handle as an array
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index fd3e57b..09a220d 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -365,7 +365,7 @@
UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR);
// Don't bother with error check on decode mode. If a bad value is
// passed it will just act as if the default normal mode of 0 was set.
- me->uDecodeMode = nDecodeMode;
+ me->uDecodeMode = (uint8_t)nDecodeMode;
DecodeNesting_Init(&(me->nesting));
}
@@ -1012,9 +1012,15 @@
}
} else {
if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) {
+ if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) {
+ nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
+ goto Done;
+ }
// Decoding a map as an array
pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY;
- pDecodedItem->val.uCount *= 2;
+ // Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2
+ // Cast is needed because of integer promotion
+ pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2);
}
}
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index be72cd5..b17698d 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -72,22 +72,20 @@
pNesting->pCurrentNesting->uMajorType = CBOR_MAJOR_TYPE_ARRAY;
}
-inline static QCBORError Nesting_Increase(QCBORTrackNesting *pNesting,
+inline static uint8_t Nesting_Increase(QCBORTrackNesting *pNesting,
uint8_t uMajorType,
uint32_t uPos)
{
- QCBORError nReturn = QCBOR_SUCCESS;
-
if(pNesting->pCurrentNesting == &pNesting->pArrays[QCBOR_MAX_ARRAY_NESTING]) {
// Trying to open one too many
- nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
+ return QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
} else {
pNesting->pCurrentNesting++;
pNesting->pCurrentNesting->uCount = 0;
pNesting->pCurrentNesting->uStart = uPos;
pNesting->pCurrentNesting->uMajorType = uMajorType;
+ return QCBOR_SUCCESS;
}
- return nReturn;
}
inline static void Nesting_Decrease(QCBORTrackNesting *pNesting)
@@ -95,13 +93,13 @@
pNesting->pCurrentNesting--;
}
-inline static QCBORError Nesting_Increment(QCBORTrackNesting *pNesting)
+inline static uint8_t Nesting_Increment(QCBORTrackNesting *pNesting)
{
if(1 >= QCBOR_MAX_ITEMS_IN_ARRAY - pNesting->pCurrentNesting->uCount) {
return QCBOR_ERR_ARRAY_TOO_LONG;
}
- pNesting->pCurrentNesting->uCount += 1;
+ pNesting->pCurrentNesting->uCount++;
return QCBOR_SUCCESS;
}