Skip to content

Commit 5bdc6ea

Browse files
marcmutzthiagomacieira
authored andcommitted
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).
1 parent 53ff130 commit 5bdc6ea

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

src/cborencoder.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ void cbor_encoder_init_writer(CborEncoder *encoder, CborEncoderWriteFunction wri
213213
{
214214
#ifdef CBOR_ENCODER_WRITE_FUNCTION
215215
(void) writer;
216+
encoder->data.writer = CBOR_NULLPTR;
216217
#else
217218
encoder->data.writer = writer;
218219
#endif

src/compilersupport_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,11 @@
198198

199199
#ifdef __cplusplus
200200
# define CONST_CAST(t, v) const_cast<t>(v)
201+
# define CBOR_NULLPTR nullptr
201202
#else
202203
/* C-style const_cast without triggering a warning with -Wcast-qual */
203204
# define CONST_CAST(t, v) (t)(uintptr_t)(v)
205+
# define CBOR_NULLPTR NULL
204206
#endif
205207

206208
#ifdef __GNUC__

0 commit comments

Comments
 (0)