Always init CborEncoder::data in cbor_encoder_init_writer()

Coverity complains that, when CBOR_ENCODER_WRITE_FUNCTION is defined,
enc.data is read in cbor_encoder_create_map() when
cbor_encoder_init_writer() didn't write to it.

While 'data' is merely copied in cbor_encoder_create_map(), Coverity
is right, though: reading an uninitialized value is UB.

Fix by setting data.writer to nullptr (abstracting the difference
between C and C++ behind a new macro).
This commit is contained in:
Marc Mutz
2025-03-10 13:00:39 -07:00
committed by Thiago Macieira
parent 53ff130af9
commit 5bdc6ea3fe
2 changed files with 3 additions and 0 deletions
+1
View File
@@ -213,6 +213,7 @@ void cbor_encoder_init_writer(CborEncoder *encoder, CborEncoderWriteFunction wri
{
#ifdef CBOR_ENCODER_WRITE_FUNCTION
(void) writer;
encoder->data.writer = CBOR_NULLPTR;
#else
encoder->data.writer = writer;
#endif
+2
View File
@@ -198,9 +198,11 @@
#ifdef __cplusplus
# define CONST_CAST(t, v) const_cast<t>(v)
# define CBOR_NULLPTR nullptr
#else
/* C-style const_cast without triggering a warning with -Wcast-qual */
# define CONST_CAST(t, v) (t)(uintptr_t)(v)
# define CBOR_NULLPTR NULL
#endif
#ifdef __GNUC__