Skip to content

Commit ee66240

Browse files
vitaly-sinevclaude
andcommitted
tests: internal: pack: cover flb_msgpack_to_json buffer limits
Verify that a value whose JSON form does not fit returns a negative value (not 0), and that a zero-capacity buffer is rejected rather than wrapping the size computation. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: v.sinev <vit.phantom@gmail.com>
1 parent b402ae2 commit ee66240

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

tests/internal/pack.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,40 @@ void test_json_pack_nan()
928928
flb_pack_init(&config);
929929
}
930930

931+
/*
932+
* flb_msgpack_to_json() must report a buffer overflow as a negative value
933+
* (not 0), so fixed-buffer callers can tell truncation apart from a
934+
* successful zero-length write.
935+
*/
936+
void test_json_pack_truncation()
937+
{
938+
int ret;
939+
char small[4];
940+
msgpack_sbuffer mp_sbuf;
941+
msgpack_packer mp_pck;
942+
msgpack_object obj;
943+
msgpack_zone mempool;
944+
945+
/* a value whose JSON form does not fit in the destination buffer */
946+
msgpack_sbuffer_init(&mp_sbuf);
947+
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
948+
msgpack_pack_double(&mp_pck, 123456789.0);
949+
msgpack_zone_init(&mempool, 2048);
950+
msgpack_unpack(mp_sbuf.data, mp_sbuf.size, NULL, &mempool, &obj);
951+
952+
ret = flb_msgpack_to_json(small, sizeof(small), &obj, FLB_TRUE);
953+
if (!TEST_CHECK(ret < 0)) {
954+
TEST_MSG("truncation must return a negative value, got %d", ret);
955+
}
956+
957+
/* a zero-capacity buffer must be rejected, not wrapped to SIZE_MAX */
958+
ret = flb_msgpack_to_json(small, 0, &obj, FLB_TRUE);
959+
TEST_CHECK(ret < 0);
960+
961+
msgpack_zone_destroy(&mempool);
962+
msgpack_sbuffer_destroy(&mp_sbuf);
963+
}
964+
931965
static int check_msgpack_val(msgpack_object obj, int expected_type, char *expected_val)
932966
{
933967
int len;
@@ -1303,6 +1337,7 @@ TEST_LIST = {
13031337
{ "json_pack_bug342" , test_json_pack_bug342},
13041338
{ "json_pack_bug1278" , test_json_pack_bug1278},
13051339
{ "json_pack_nan" , test_json_pack_nan},
1340+
{ "json_pack_truncation", test_json_pack_truncation},
13061341
{ "json_pack_bug5336" , test_json_pack_bug5336},
13071342
{ "json_pack_empty_array", test_json_pack_empty_array},
13081343
{ "json_date_iso8601" , test_json_date_iso8601},

0 commit comments

Comments
 (0)