QCBOREncode_Finish sets internal error state
diff --git a/inc/qcbor/qcbor_encode.h b/inc/qcbor/qcbor_encode.h
index 0385af4..3981e9b 100644
--- a/inc/qcbor/qcbor_encode.h
+++ b/inc/qcbor/qcbor_encode.h
@@ -3892,15 +3892,17 @@
QCBOREncode_GetErrorState(QCBOREncodeContext *pMe)
{
if(UsefulOutBuf_GetError(&(pMe->OutBuf))) {
- // Items didn't fit in the buffer.
- // This check catches this condition for all the appends and inserts
- // so checks aren't needed when the appends and inserts are performed.
- // And of course UsefulBuf will never overrun the input buffer given
- // to it. No complex analysis of the error handling in this file is
- // needed to know that is true. Just read the UsefulBuf code.
+ /* Items didn't fit in the buffer. This check catches this
+ * condition for all the appends and inserts so checks aren't
+ * needed when the appends and inserts are performed. And of
+ * course UsefulBuf will never overrun the input buffer given to
+ * it. No complex analysis of the error handling in this file is
+ * needed to know that is true. Just read the UsefulBuf code.
+ */
pMe->uError = QCBOR_ERR_BUFFER_TOO_SMALL;
- // QCBOR_ERR_BUFFER_TOO_SMALL masks other errors, but that is
- // OK. Once the caller fixes this, they'll be unmasked.
+ /* QCBOR_ERR_BUFFER_TOO_SMALL masks other errors, but that is
+ * OK. Once the caller fixes this, they'll be unmasked.
+ */
}
return (QCBORError)pMe->uError;
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 72e6800..fa3486e 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -1140,15 +1140,13 @@
QCBORError
QCBOREncode_Finish(QCBOREncodeContext *pMe, UsefulBufC *pEncodedCBOR)
{
- QCBORError uReturn = QCBOREncode_GetErrorState(pMe);
-
- if(uReturn != QCBOR_SUCCESS) {
+ if(QCBOREncode_GetErrorState(pMe) != QCBOR_SUCCESS) {
goto Done;
}
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(Nesting_IsInNest(&(pMe->nesting))) {
- uReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN;
+ pMe->uError = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN;
goto Done;
}
#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
@@ -1156,7 +1154,7 @@
*pEncodedCBOR = UsefulOutBuf_OutUBuf(&(pMe->OutBuf));
Done:
- return uReturn;
+ return pMe->uError;
}
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index 267b439..dcc1445 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -2483,6 +2483,7 @@
{
QCBOREncodeContext EC;
QCBORError uErr;
+ UsefulBufC yy;
// ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
@@ -2678,6 +2679,13 @@
}
#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+ // TODO: make sure error sticks after Finish.
+ QCBOREncode_Init(&EC, Large);
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_Finish(&EC, &yy);
+ if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -120;
+ }
return 0;
}