disable encode guards mostly working
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 992fa87..f800cb4 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -95,9 +95,11 @@
inline static uint8_t Nesting_Increment(QCBORTrackNesting *pNesting)
{
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(1 >= QCBOR_MAX_ITEMS_IN_ARRAY - pNesting->pCurrentNesting->uCount) {
return QCBOR_ERR_ARRAY_TOO_LONG;
}
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
pNesting->pCurrentNesting->uCount++;
@@ -127,6 +129,7 @@
return pNesting->pCurrentNesting->uStart;
}
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
inline static uint8_t Nesting_GetMajorType(QCBORTrackNesting *pNesting)
{
return pNesting->pCurrentNesting->uMajorType;
@@ -136,6 +139,7 @@
{
return pNesting->pCurrentNesting == &pNesting->pArrays[0] ? false : true;
}
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
@@ -440,31 +444,37 @@
*/
static void InsertCBORHead(QCBOREncodeContext *me, uint8_t uMajorType, size_t uLen)
{
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(me->uError == QCBOR_SUCCESS) {
if(!Nesting_IsInNest(&(me->nesting))) {
me->uError = QCBOR_ERR_TOO_MANY_CLOSES;
+ return;
} else if(Nesting_GetMajorType(&(me->nesting)) != uMajorType) {
me->uError = QCBOR_ERR_CLOSE_MISMATCH;
- } else {
- // A stack buffer large enough for a CBOR head
- UsefulBuf_MAKE_STACK_UB (pBufferForEncodedHead,QCBOR_HEAD_BUFFER_SIZE);
-
- UsefulBufC EncodedHead = QCBOREncode_EncodeHead(pBufferForEncodedHead,
- uMajorType,
- 0,
- uLen);
-
- /* No check for EncodedHead == NULLUsefulBufC is performed here to
- * save object code. It is very clear that pBufferForEncodedHead
- * is the correct size. If EncodedHead == NULLUsefulBufC then
- * UsefulOutBuf_InsertUsefulBuf() will do nothing so there is
- * no security whole introduced.
- */
- UsefulOutBuf_InsertUsefulBuf(&(me->OutBuf), EncodedHead, Nesting_GetStartPos(&(me->nesting)) );
-
- Nesting_Decrease(&(me->nesting));
+ return;
}
}
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+
+ // A stack buffer large enough for a CBOR head
+ UsefulBuf_MAKE_STACK_UB(pBufferForEncodedHead, QCBOR_HEAD_BUFFER_SIZE);
+
+ UsefulBufC EncodedHead = QCBOREncode_EncodeHead(pBufferForEncodedHead,
+ uMajorType,
+ 0,
+ uLen);
+
+ /* No check for EncodedHead == NULLUsefulBufC is performed here to
+ * save object code. It is very clear that pBufferForEncodedHead
+ * is the correct size. If EncodedHead == NULLUsefulBufC then
+ * UsefulOutBuf_InsertUsefulBuf() will do nothing so there is
+ * no security whole introduced.
+ */
+ UsefulOutBuf_InsertUsefulBuf(&(me->OutBuf),
+ EncodedHead,
+ Nesting_GetStartPos(&(me->nesting)));
+
+ Nesting_Decrease(&(me->nesting));
}
@@ -473,10 +483,15 @@
*/
void QCBOREncode_AddUInt64(QCBOREncodeContext *me, uint64_t uValue)
{
+ AppendCBORHead(me, CBOR_MAJOR_TYPE_POSITIVE_INT, uValue, 0);
+
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(me->uError == QCBOR_SUCCESS) {
- AppendCBORHead(me, CBOR_MAJOR_TYPE_POSITIVE_INT, uValue, 0);
me->uError = Nesting_Increment(&(me->nesting));
}
+#else
+ (void)Nesting_Increment(&(me->nesting));
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
}
@@ -485,22 +500,26 @@
*/
void QCBOREncode_AddInt64(QCBOREncodeContext *me, int64_t nNum)
{
+ uint8_t uMajorType;
+ uint64_t uValue;
+
+ if(nNum < 0) {
+ // In CBOR -1 encodes as 0x00 with major type negative int.
+ uValue = (uint64_t)(-nNum - 1);
+ uMajorType = CBOR_MAJOR_TYPE_NEGATIVE_INT;
+ } else {
+ uValue = (uint64_t)nNum;
+ uMajorType = CBOR_MAJOR_TYPE_POSITIVE_INT;
+ }
+ AppendCBORHead(me, uMajorType, uValue, 0);
+
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(me->uError == QCBOR_SUCCESS) {
- uint8_t uMajorType;
- uint64_t uValue;
-
- if(nNum < 0) {
- // In CBOR -1 encodes as 0x00 with major type negative int.
- uValue = (uint64_t)(-nNum - 1);
- uMajorType = CBOR_MAJOR_TYPE_NEGATIVE_INT;
- } else {
- uValue = (uint64_t)nNum;
- uMajorType = CBOR_MAJOR_TYPE_POSITIVE_INT;
- }
- AppendCBORHead(me, uMajorType, uValue, 0);
-
me->uError = Nesting_Increment(&(me->nesting));
}
+#else
+ (void)Nesting_Increment(&(me->nesting));
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
}
@@ -526,24 +545,28 @@
*/
void QCBOREncode_AddBuffer(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC Bytes)
{
+ // If it is not Raw CBOR, add the type and the length
+ if(uMajorType != CBOR_MAJOR_NONE_TYPE_RAW) {
+ uint8_t uRealMajorType = uMajorType;
+ if(uRealMajorType == CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) {
+ uRealMajorType = CBOR_MAJOR_TYPE_BYTE_STRING;
+ }
+ AppendCBORHead(me, uRealMajorType, Bytes.len, 0);
+ }
+
+ if(uMajorType != CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) {
+ // Actually add the bytes
+ UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), Bytes);
+ }
+
+ // Update the array counting if there is any nesting at all
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(me->uError == QCBOR_SUCCESS) {
- // If it is not Raw CBOR, add the type and the length
- if(uMajorType != CBOR_MAJOR_NONE_TYPE_RAW) {
- uint8_t uRealMajorType = uMajorType;
- if(uRealMajorType == CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) {
- uRealMajorType = CBOR_MAJOR_TYPE_BYTE_STRING;
- }
- AppendCBORHead(me, uRealMajorType, Bytes.len, 0);
- }
-
- if(uMajorType != CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) {
- // Actually add the bytes
- UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), Bytes);
- }
-
- // Update the array counting if there is any nesting at all
me->uError = Nesting_Increment(&(me->nesting));
}
+#else
+ (void)Nesting_Increment(&(me->nesting));
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
}
@@ -564,15 +587,25 @@
*/
void QCBOREncode_AddType7(QCBOREncodeContext *me, uint8_t uMinLen, uint64_t uNum)
{
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(me->uError == QCBOR_SUCCESS) {
if(uNum >= CBOR_SIMPLEV_RESERVED_START && uNum <= CBOR_SIMPLEV_RESERVED_END) {
me->uError = QCBOR_ERR_ENCODE_UNSUPPORTED;
- } else {
- // AppendHead() does endian swapping for the float / double
- AppendCBORHead(me, CBOR_MAJOR_TYPE_SIMPLE, uNum, uMinLen);
- me->uError = Nesting_Increment(&(me->nesting));
+ return;
}
}
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+
+ // AppendHead() does endian swapping for the float / double
+ AppendCBORHead(me, CBOR_MAJOR_TYPE_SIMPLE, uNum, uMinLen);
+
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
+ if(me->uError == QCBOR_SUCCESS) {
+ me->uError = Nesting_Increment(&(me->nesting));
+ }
+#else
+ (void)Nesting_Increment(&(me->nesting));
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
}
@@ -781,18 +814,23 @@
*/
void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType)
{
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(me->uError == QCBOR_SUCCESS) {
if(!Nesting_IsInNest(&(me->nesting))) {
me->uError = QCBOR_ERR_TOO_MANY_CLOSES;
+ return;
} else if(Nesting_GetMajorType(&(me->nesting)) != uMajorType) {
me->uError = QCBOR_ERR_CLOSE_MISMATCH;
- } else {
- // Append the break marker (0xff for both arrays and maps)
- AppendCBORHead(me, CBOR_MAJOR_NONE_TYPE_SIMPLE_BREAK, CBOR_SIMPLE_BREAK, 0);
-
- Nesting_Decrease(&(me->nesting));
+ return;
}
}
+#else
+ (void) uMajorType;
+#endif
+
+ // Append the break marker (0xff for both arrays and maps)
+ AppendCBORHead(me, CBOR_MAJOR_NONE_TYPE_SIMPLE_BREAK, CBOR_SIMPLE_BREAK, 0);
+ Nesting_Decrease(&(me->nesting));
}
@@ -807,10 +845,12 @@
goto Done;
}
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if (Nesting_IsInNest(&(me->nesting))) {
uReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN;
goto Done;
}
+#endif
*pEncodedCBOR = UsefulOutBuf_OutUBuf(&(me->OutBuf));
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index c573a4b..57c4ff8 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -924,6 +924,8 @@
static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
+// 9F F5 F4 F6 F7 BF 65 55 4E 44 65 66 F7 F8 1F F8 1F 69 73 20 74 68 65 20
+
int32_t SimpleValuesIndefiniteLengthTest1()
{
QCBOREncodeContext ECtx;
@@ -1780,8 +1782,12 @@
int32_t BstrWrapErrorTest()
{
- // ---- Test closing a bstrwrap when it is an array that is open ---------
QCBOREncodeContext EC;
+ UsefulBufC Wrapped;
+ UsefulBufC Encoded2;
+
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
+ // ---- Test closing a bstrwrap when it is an array that is open ---------
QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
@@ -1792,12 +1798,10 @@
QCBOREncode_AddUInt64(&EC, 466);
QCBOREncode_OpenArray(&EC);
- UsefulBufC Wrapped;
QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
QCBOREncode_CloseArray(&EC);
- UsefulBufC Encoded2;
if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
return -1;
}
@@ -1808,6 +1812,7 @@
if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
return -2;
}
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
// --------------- test nesting too deep ----------------------------------
QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
@@ -2358,13 +2363,13 @@
// Second verify error from an array in encoded output too large
// Also test fetching the error code before finish
- QCBOREncode_Init(&EC, Buffer);
+ QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
QCBOREncode_OpenArray(&EC);
- QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
+ QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
// Error fetch failed.
- return -12;
+ return -122;
}
QCBOREncode_CloseArray(&EC);
QCBOREncode_CloseArray(&EC);
@@ -2435,6 +2440,7 @@
}
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
// ------ QCBOR_ERR_TOO_MANY_CLOSES --------
QCBOREncode_Init(&EC, Large);
for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
@@ -2471,6 +2477,7 @@
// One more level to cause error
return -9;
}
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
/* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
it would require a 64KB of RAM to test */
@@ -2482,6 +2489,7 @@
return -11;
}
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
// ------ QCBOR_ERR_UNSUPPORTED --------
QCBOREncode_Init(&EC, Large);
QCBOREncode_OpenArray(&EC);
@@ -2496,6 +2504,8 @@
if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ENCODE_UNSUPPORTED) {
return -13;
}
+#endif /* #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+
return 0;
}
diff --git a/test/run_tests.c b/test/run_tests.c
index 0609736..0383b21 100644
--- a/test/run_tests.c
+++ b/test/run_tests.c
@@ -73,7 +73,9 @@
TEST_ENTRY(MapEncodeTest),
TEST_ENTRY(ArrayNestingTest1),
TEST_ENTRY(ArrayNestingTest2),
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
TEST_ENTRY(ArrayNestingTest3),
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
TEST_ENTRY(EncodeDateTest),
TEST_ENTRY(SimpleValuesTest1),
TEST_ENTRY(IntegerValuesTest1),