Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QCBOR should have QCBORDecode_SetError() #210

Closed
osibert opened this issue Feb 29, 2024 · 4 comments
Closed

QCBOR should have QCBORDecode_SetError() #210

osibert opened this issue Feb 29, 2024 · 4 comments

Comments

@osibert
Copy link

osibert commented Feb 29, 2024

In writing decoders for some applications, it is often necessary to perform similar checks of decoded values in many different parts of the code. Such checks (e.g., integer range, valid values, text/byte string size checks, array size checks) can conveniently be implemented as thin wrappers on the underlying QCBORDecode_GetXXX and related functions, but there appears to be no way to indicate to the QCBOR decoder that a check has failed, stopping subsequent decoding and preserving the error value and context, unlike the regular spiffy decode functions which halt further decoding when they encounter a CBOR syntax error.

Would QCBORDecode_SetError(), perhaps in combination with some additional QCBORError values, be a reasonable way to facilitate this end?

Is there a straightforward (but of course undocumented and subject to change) way to do this by accessing the decoder structures directly? I have been reluctant to dive into the code without a sanity check on the concept.

I imagine defining QCBORError values like QCBOR_ERR_INVALID_VALUE, QCBOR_ERR_VALUE_OUT_OF_RANGE, QCBOR_ERR_INVALID_TYPE, QCBOR_ERR_STRING_TOO_LARGE, QCBOR_ERR_STRING_TOO_SHORT, QCBOR_ERR_INVALID_ARRAY_SIZE, etc.

My current solution wraps the whole decoding process with a thin layer of wrappers and maintains a separate copy of the error state in the wrapper object, but that seems a clumsy approach at best.

I see Issue #99 starts to address this objective, but it's a really specialized aspect of value checking (that said, I did end up writing those very functions in my set of wrappers).

Thank you for your consideration.

@laurencelundblade
Copy link
Owner

Sounds like a good idea to me.

Pretty sure you can just set QCBORDecodeContext.uLastError to a value other than QCBOR_SUCCESS and it will do what you want.

   if(pMe->uLastError != QCBOR_SUCCESS) {
      return;
   }

occurs about 50 times in the code, usually at the beginning of a decode method.

I will probably reserve error code 200-255 for end-user use.

I will probably implement this in the next month or so. Testing seems like the hardest part. :-)

Thanks for the suggestion!

@osibert
Copy link
Author

osibert commented Mar 1, 2024 via email

@laurencelundblade
Copy link
Owner

Yes, happy to review. I do have some materials on CBOR (not QCBOR). Let's switch to email for that discussion [email protected].

@laurencelundblade
Copy link
Owner

Fixed by #214. Eventually a QCBOR 1.3 will be released. For now this is just the tip in GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants