Skip to content

Commit

Permalink
Fix GetTextString() and GetByteString() error handling (#303)
Browse files Browse the repository at this point in the history
GetTextString() and GetByteString() where returning the wrong error code when the input CBOR was the wrong type

* Fix GetTextString() and GetByteString() error handling

* Improve test and doc for GetString

* Miniscule header fix

---------

Co-authored-by: Laurence Lundblade <[email protected]>
  • Loading branch information
laurencelundblade and Laurence Lundblade authored Feb 12, 2025
1 parent 1e13a4f commit 294316b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 23 deletions.
29 changes: 9 additions & 20 deletions inc/qcbor/qcbor_spiffy_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,13 @@ QCBORDecode_GetSimpleInMapSZ(QCBORDecodeContext *pCtx,
* BEGINNING OF PRIVATE INLINE IMPLEMENTATION *
* ========================================================================= */


/** @private Semi-private function. See qcbor_spiffy_decode.c */
void
QCBORDecode_Private_GetString(QCBORDecodeContext *pMe,
uint8_t uType,
UsefulBufC *pText);

/** @private Semi-private function. See qcbor_spiffy_decode.c */
void
QCBORDecode_Private_EnterBoundedMapOrArray(QCBORDecodeContext *pCtx,
Expand Down Expand Up @@ -1009,20 +1016,10 @@ QCBORDecode_GetMapFromMapSZ(QCBORDecodeContext *pMe,
}




static inline void
QCBORDecode_GetByteString(QCBORDecodeContext *pMe, UsefulBufC *pBytes)
{
QCBORItem Item;

QCBORDecode_VGetNext(pMe, &Item);

if(pMe->uLastError == QCBOR_SUCCESS && Item.uDataType == QCBOR_TYPE_BYTE_STRING) {
*pBytes = Item.val.string;
} else {
*pBytes = NULLUsefulBufC;
}
QCBORDecode_Private_GetString(pMe, QCBOR_TYPE_BYTE_STRING, pBytes);
}

static inline void
Expand Down Expand Up @@ -1061,15 +1058,7 @@ QCBORDecode_GetByteStringInMapSZ(QCBORDecodeContext *pMe,
static inline void
QCBORDecode_GetTextString(QCBORDecodeContext *pMe, UsefulBufC *pText)
{
QCBORItem Item;

QCBORDecode_VGetNext(pMe, &Item);

if(pMe->uLastError == QCBOR_SUCCESS && Item.uDataType == QCBOR_TYPE_TEXT_STRING) {
*pText = Item.val.string;
} else {
*pText = NULLUsefulBufC;
}
QCBORDecode_Private_GetString(pMe, QCBOR_TYPE_TEXT_STRING, pText);
}

static inline void
Expand Down
28 changes: 28 additions & 0 deletions src/qcbor_spiffy_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@
#endif


/**
* @brief Spiffy decode get a byte string.
*
* @param[in] pMe The decode context.
* @param[in] uType The CBOR qcbor type requested.
* @param[out] pString The returned string.
*
* This sets the spiffy decode last error if there is a problem
* deocing or the string is not of the requested type.
*/
void
QCBORDecode_Private_GetString(QCBORDecodeContext *pMe, const uint8_t uType, UsefulBufC *pString)
{
QCBORItem Item;

QCBORDecode_VGetNext(pMe, &Item);

*pString = NULLUsefulBufC;
if(pMe->uLastError == QCBOR_SUCCESS) {
if(Item.uDataType == uType) {
*pString = Item.val.string;
} else {
pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
}
}
}


/* Return true if the labels in Item1 and Item2 are the same.
Works only for integer and string labels. Returns false
for any other type. */
Expand Down
54 changes: 53 additions & 1 deletion test/qcbor_decode_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -9323,7 +9323,7 @@ int32_t TooLargeInputTest(void)

/*
An array of three map entries
1) Indefinite length string label for indefinite lenght byte string
1) Indefinite length string label for indefinite length byte string
2) Indefinite length string label for an integer
3) Indefinite length string label for an indefinite-length negative big num
*/
Expand Down Expand Up @@ -9416,6 +9416,58 @@ int32_t SpiffyIndefiniteLengthStringsTests(void)
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */


static const uint8_t spStringsTest[] = {
0x63, 'a', 'b', 'c',
0x43, 'd', 'e', 'f'
};

static const uint8_t spNotWellFormed[] = {
0xff
};

int32_t SpiffyStringTest(void)
{
QCBORDecodeContext DC;
UsefulBufC String;

QCBORDecode_Init(&DC, ByteArrayLiteralToUsefulBufC(spStringsTest), 0);

QCBORDecode_GetTextString(&DC, &String);
if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS &&
UsefulBuf_Compare(String, SZLiteralToUsefulBufC("abc"))) {
return 1;
}

QCBORDecode_GetByteString(&DC, &String);
if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS &&
UsefulBuf_Compare(String, SZLiteralToUsefulBufC("def"))) {
return 2;
}

QCBORDecode_Init(&DC, ByteArrayLiteralToUsefulBufC(spStringsTest), 0);
QCBORDecode_GetByteString(&DC, &String);
if(QCBORDecode_GetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE &&
!UsefulBuf_IsNULLC(String)) {
return 3;
}

QCBORDecode_Init(&DC, ByteArrayLiteralToUsefulBufC(spNotWellFormed), 0);
QCBORDecode_GetByteString(&DC, &String);
if(QCBORDecode_GetError(&DC) != QCBOR_ERR_BAD_BREAK &&
!UsefulBuf_IsNULLC(String)) {
return 4;
}

#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
return SpiffyIndefiniteLengthStringsTests();
#else
return 0;
#endif

}



#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
/*
* An array of an integer and an array. The second array contains
Expand Down
2 changes: 1 addition & 1 deletion test/qcbor_decode_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ int32_t TooLargeInputTest(void);
/*
Test spiffy decoding of indefinite length strings.
*/
int32_t SpiffyIndefiniteLengthStringsTests(void);
int32_t SpiffyStringTest(void);


/*
Expand Down
2 changes: 1 addition & 1 deletion test/run_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static test_entry s_tests[] = {
TEST_ENTRY(MemPoolTest),
TEST_ENTRY(IndefiniteLengthStringTest),
#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
TEST_ENTRY(SpiffyIndefiniteLengthStringsTests),
TEST_ENTRY(SpiffyStringTest),
#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
TEST_ENTRY(SetUpAllocatorTest),
TEST_ENTRY(CBORTestIssue134),
Expand Down

0 comments on commit 294316b

Please sign in to comment.