Skip to content

Commit

Permalink
Fix bug: failure to convert to json string
Browse files Browse the repository at this point in the history
The Binson class failed to convert data to a json string if the
data was \x40\x14\x01\x74\x10\x02\x41
A fix is added and testcase.
  • Loading branch information
AndreasHansson73 committed Aug 7, 2024
1 parent f97e8a2 commit 46c720a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/binson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ string Binson::toStr() const
if (!result)
{
str_vector.resize(size);
result = binson_parser_to_string(&p, str_vector.data(), &size, true);
}
result = binson_parser_to_string(&p, str_vector.data(), &size, true);
if (!result)
{
return string();
Expand Down
34 changes: 34 additions & 0 deletions utest/binson_class_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,38 @@ TEST(binson_class_test1)
ASSERT_TRUE(binson_writer_get_counter(&w) == binson_expected_size);
}

TEST(binson_class_test2)
{
uint8_t binson_data[] = "\x40\x14\x01\x74\x10\x02\x41";
size_t binson_data_size = sizeof(binson_data) - 1;
BINSON_PARSER_DEF(p);
ASSERT_TRUE(binson_parser_init(&p, binson_data, binson_data_size));
ASSERT_TRUE(binson_parser_verify(&p));

Binson b;
binson_parser_reset(&p);
b.deserialize(&p);

string json = b.toStr();
ASSERT_TRUE(json == string("{\"t\":2}"));
}

TEST(binson_class_test_empty_object)
{
uint8_t binson_data[] = "\x40\x41";
size_t binson_data_size = sizeof(binson_data) - 1;
BINSON_PARSER_DEF(p);
ASSERT_TRUE(binson_parser_init(&p, binson_data, binson_data_size));
ASSERT_TRUE(binson_parser_verify(&p));

Binson b;
binson_parser_reset(&p);
b.deserialize(&p);

string json = b.toStr();
ASSERT_TRUE(json == string("{}"));
}

TEST(unsorted_writing)
{
uint8_t buf[64];
Expand Down Expand Up @@ -244,6 +276,8 @@ TEST(serialize_vector)

int main(void) {
RUN_TEST(binson_class_test1);
RUN_TEST(binson_class_test2);
RUN_TEST(binson_class_test_empty_object);
RUN_TEST(unsorted_writing);
RUN_TEST(serialize_vector);
PRINT_RESULT();
Expand Down

0 comments on commit 46c720a

Please sign in to comment.