jkuhlmann / cgltf

:diamond_shape_with_a_dot_inside: Single-file glTF 2.0 loader and writer written in C99
MIT License
1.44k stars 136 forks source link

Fix format-truncation warning for gcc #156

Closed Black-Cat closed 3 years ago

Black-Cat commented 3 years ago

In newer gcc versions (tested on gcc 11.1.0) compiling with -O2 -Wall -Werror flags produces warnings like

In file included from /cgltf/test/test_write.cpp:3:
In function ‘void cgltf_write_node(cgltf_write_context*, const cgltf_node*)’,
    inlined from ‘cgltf_size cgltf_write(const cgltf_options*, char*, cgltf_size, const cgltf_data*)’ at /cgltf/test/../cgltf_write.h:1183:20:
/cgltf/test/../cgltf_write.h:112:41: error: null destination pointer [-Werror=format-truncation=]
  112 |                 context->tmp = snprintf ( context->cursor, context->remaining, __VA_ARGS__ ); \
      |                                ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/cgltf/test/../cgltf_write.h:138:25: note: in expansion of macro ‘CGLTF_SPRINTF’
  138 |                         CGLTF_SPRINTF(" %d", idx); \
      |                         ^~~~~~~~~~~~~
/cgltf/test/../cgltf_write.h:857:9: note: in expansion of macro ‘CGLTF_WRITE_IDXARRPROP’
  857 |         CGLTF_WRITE_IDXARRPROP("children", node->children_count, node->children, context->data->nodes);
      |         ^~~~~~~~~~~~~~~~~~~~~~

This patch adds additional check for non zero buffer size with null buffer in snprintf, which solves the warning.