Skip to content

Commit

Permalink
str-format: Fixed format_hex_string_with_delimiter.
Browse files Browse the repository at this point in the history
g_snprintf adds a terminating NULL character, which was not considered.
We need more space for this extra character, so the result length
check should also be tweaked.

Signed-off-by: Viktor Tusa <tusa@balabit.hu>
  • Loading branch information
Viktor Tusa committed Feb 4, 2014
1 parent cdbe81d commit 58cf6a3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/str-format.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ format_hex_string_with_delimiter(gpointer data, gsize data_len, gchar *result, g
gint pos = 0;
guchar *str = (guchar *) data;

for (i = 0; i < data_len && result_len - pos >= 2; i++)
for (i = 0; i < data_len && result_len - pos >= 3; i++)
{
if ( (delimiter != 0) && (i < data_len - 1))
{
Expand Down
16 changes: 8 additions & 8 deletions lib/tests/test_str_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

void test_format_hex_string__single_byte__perfect()
{
gchar expected_output[2] = "40";
gchar output[2];
gchar expected_output[3] = "40";
gchar output[3];
gchar input[1] = "@";
//Act
format_hex_string(input, sizeof(input), output, sizeof(output));
Expand All @@ -14,8 +14,8 @@ void test_format_hex_string__single_byte__perfect()

void test_format_hex_string__two_bytes__perfect()
{
gchar expected_output[4] = "4041";
gchar output[4];
gchar expected_output[5] = "4041";
gchar output[5];
gchar input[2] = "@A";
//Act
format_hex_string(input, sizeof(input), output, sizeof(output));
Expand All @@ -25,8 +25,8 @@ void test_format_hex_string__two_bytes__perfect()

void test_format_hex_string_with_delimiter__single_byte__perfect()
{
gchar expected_output[2] = "40";
gchar output[2];
gchar expected_output[3] = "40";
gchar output[3];
gchar input[1] = "@";
//Act
format_hex_string_with_delimiter(input, sizeof(input), output, sizeof(output), ' ');
Expand All @@ -36,8 +36,8 @@ void test_format_hex_string_with_delimiter__single_byte__perfect()

void test_format_hex_string_with_delimiter__two_bytes__perfect()
{
gchar expected_output[5] = "40 41";
gchar output[5];
gchar expected_output[6] = "40 41";
gchar output[6];
gchar input[2] = "@A";
//Act
format_hex_string_with_delimiter(input, sizeof(input), output, sizeof(output), ' ');
Expand Down

0 comments on commit 58cf6a3

Please sign in to comment.