Skip to content

QCBOREncode_FinishGetSize not usable if the original buffer is too small #329

@BrianSipos

Description

@BrianSipos

I would like to use a simple pattern to encode variable-length PDUs with an optimistic initial size estimate to avoid double-encoding under good conditions.

The pattern is:

  1. Allocate a small fixed-size buffer
  2. Encode to that buffer as normal
  3. Use QCBOREncode_FinishGetSize() to determine if the buffer was large enough or not
  4. If it was not, allocate a properly sized (larger) buffer
  5. Encode to the dynamically sized buffer
  6. Use QCBOREncode_Finish() to finalize

My problem is currently that QCBOREncode_FinishGetSize() only sets the encoded size if the error state is QCBOR_SUCCESS but not if it is QCBOR_ERR_BUFFER_TOO_SMALL.

QCBOR/src/qcbor_encode.c

Lines 1081 to 1083 in 9f09f0c

if(nReturn == QCBOR_SUCCESS) {
*puEncodedLen = Enc.len;
}

Is there a reason for that condition to be present in FinishGetSize at all?
Let the caller check the error code to see if the size is meaningful in its context.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions