Skip to content

Commit

Permalink
Fix GetTextString() and GetByteString() error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurence Lundblade committed Jan 25, 2025
1 parent 2530482 commit 8fe9d11
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
35 changes: 12 additions & 23 deletions inc/qcbor/qcbor_spiffy_decode.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* ==========================================================================
* qcbor_spiffy_decode.h -- higher-level easier-to-use CBOR decoding.
*
* Copyright (c) 2020-2024, Laurence Lundblade. All rights reserved.
* Copyright (c) 2020-2025, Laurence Lundblade. All rights reserved.
* Copyright (c) 2021, Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
Expand Down Expand Up @@ -136,7 +136,7 @@ extern "C" {
*
* See also QCBORDecode_EnterBstrWrapped().
*/
static void
static inline void
QCBORDecode_GetByteString(QCBORDecodeContext *pCtx,
UsefulBufC *pBytes);

Expand Down Expand Up @@ -168,7 +168,7 @@ QCBORDecode_GetByteStringInMapSZ(QCBORDecodeContext *pCtx,
* This does no translation of line endings. See QCBOREncode_AddText()
* for a discussion of line endings in CBOR.
*/
static void
static inline void
QCBORDecode_GetTextString(QCBORDecodeContext *pCtx,
UsefulBufC *pText);

Expand Down Expand Up @@ -817,6 +817,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,
UsefulBufC *pText,
uint8_t uType);

/** @private Semi-private function. See qcbor_spiffy_decode.c */
void
QCBORDecode_Private_EnterBoundedMapOrArray(QCBORDecodeContext *pCtx,
Expand Down Expand Up @@ -1003,20 +1010,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, pBytes, QCBOR_TYPE_BYTE_STRING);
}

static inline void
Expand Down Expand Up @@ -1055,15 +1052,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, pText, QCBOR_TYPE_TEXT_STRING);
}

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


/* Public function, see qcbor/qcbor_spiffy_decode.h */
void
QCBORDecode_Private_GetString(QCBORDecodeContext *pMe, UsefulBufC *pText, uint8_t uType)
{
QCBORItem Item;

QCBORDecode_VGetNext(pMe, &Item);

*pText = NULLUsefulBufC;
if(pMe->uLastError == QCBOR_SUCCESS) {
if(Item.uDataType == uType) {
*pText = 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

0 comments on commit 8fe9d11

Please sign in to comment.