Both cbor_encode_bytes_prefix_bin() and cbor_encode_text_bin() document
that "if <bytes> is NULL, then only the <prefix> (with length) will be
emitted", and the hex variants do implement it, their copy loop being
guarded with "while (bytes && it < len)". The bin one does not: it calls
memcpy() unconditionally, so passing NULL dereferences it and crashes,
and it advances <start> by <len> for bytes it never wrote.
No caller passed NULL so far, which is why this remained unnoticed, but
better fix it before it gets used, as it's trivial.
No backport needed.
return NULL;
/* write actual bytes if provided */
- if (start + len >= stop)
- return NULL;
- memcpy(start, bytes, len);
- start += len;
+ if (bytes) {
+ if (start + len >= stop)
+ return NULL;
+ memcpy(start, bytes, len);
+ start += len;
+ }
return start;
}