Skip to contentSkip to conversation

tests: Fix unterminated string output visitor enum human string

PATCH In review Andreas Färber 4 participants 4 messages Jul 9, 2014 – Jul 10, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests: Fix unterminated string output visitor enum human string

The buffer was being allocated of size string length plus two. Around the string two quotes were being added, but no terminating NUL. It was then compared using g_assert_cmpstr(), resulting in fairly random assertion failures:

ERROR:tests/test-string-output-visitor.c:213:test_visitor_out_enum: assertion failed (str == str_human): ("\"value1\"" == "\"value1\"\001EEEEEEEEEEEEEE\0171")

There is no g_assert_cmpnstr() counterpart, so use g_strdup_printf() for safely assembling the string in the first place.

Cc Hu Tao <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Fixes b4900c0 tests: add human format test for string output visitor
---
 tests/test-string-output-visitor.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
tests/test-string-output-visitor.c +1 -6
index e89e43c..101fb27 100644
@@ -196,16 +196,11 @@ static void test_visitor_out_enum(TestOutputVisitorData *data,
 
     for (i = 0; i < ENUM_ONE_MAX; i++) {
         char *str_human;
-        int len;
 
         visit_type_EnumOne(data->ov, &i, "unused", &err);
         g_assert(!err);
 
-        len = strlen(EnumOne_lookup[i]) + 2;
-        str_human = g_malloc0(len);
-        str_human[0] = '"';
-        strncpy(str_human + 1, EnumOne_lookup[i], strlen(EnumOne_lookup[i]));
-        str_human[len - 1] = '"';
+        str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]);
 
         str = string_output_get_string(data->sov);
         g_assert(str != NULL);
Signature · 1 line
1.8.4.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Andreas Färber

The buffer was being allocated of size string length plus two. Around the string two quotes were being added, but no terminating NUL. It was then compared using g_assert_cmpstr(), resulting in fairly random assertion failures:

ERROR:tests/test-string-output-visitor.c:213:test_visitor_out_enum: assertion failed (str == str_human): ("\"value1\"" == "\"value1\"\001EEEEEEEEEEEEEE\0171")

There is no g_assert_cmpnstr() counterpart, so use g_strdup_printf() for safely assembling the string in the first place.

Quoted 8 lines
Cc Hu Tao <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Fixes b4900c0 tests: add human format test for string output visitor
---
 tests/test-string-output-visitor.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 1/1
#2 lore raw
In reply to Andreas Färber
Quoted 43 lines · Andreas Färber

The buffer was being allocated of size string length plus two. Around the string two quotes were being added, but no terminating NUL. It was then compared using g_assert_cmpstr(), resulting in fairly random assertion failures:

ERROR:tests/test-string-output-visitor.c:213:test_visitor_out_enum: assertion failed (str == str_human): ("\"value1\"" == "\"value1\"\001EEEEEEEEEEEEEE\0171")

There is no g_assert_cmpnstr() counterpart, so use g_strdup_printf() for safely assembling the string in the first place.

Cc Hu Tao <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Fixes b4900c0 tests: add human format test for string output visitor
---
 tests/test-string-output-visitor.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
tests/test-string-output-visitor.c +1 -6
index e89e43c..101fb27 100644
@@ -196,16 +196,11 @@ static void test_visitor_out_enum(TestOutputVisitorData *data,
 
     for (i = 0; i < ENUM_ONE_MAX; i++) {
         char *str_human;
-        int len;
 
         visit_type_EnumOne(data->ov, &i, "unused", &err);
         g_assert(!err);
 
-        len = strlen(EnumOne_lookup[i]) + 2;
-        str_human = g_malloc0(len);
-        str_human[0] = '"';
-        strncpy(str_human + 1, EnumOne_lookup[i], strlen(EnumOne_lookup[i]));
-        str_human[len - 1] = '"';
+        str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]);
 
         str = string_output_get_string(data->sov);
         g_assert(str != NULL);
Signature · 1 line
1.8.4.5
#3 lore raw
In reply to Andreas Färber
Quoted 9 lines · Andreas Färber

The buffer was being allocated of size string length plus two. Around the string two quotes were being added, but no terminating NUL. It was then compared using g_assert_cmpstr(), resulting in fairly random assertion failures:

ERROR:tests/test-string-output-visitor.c:213:test_visitor_out_enum: assertion failed (str == str_human): ("\"value1\"" == "\"value1\"\001EEEEEEEEEEEEEE\0171")

There is no g_assert_cmpnstr() counterpart, so use g_strdup_printf() for safely assembling the string in the first place.

Applied to master as a buildfix, thanks.

-- PMM