* [PATCH] tests/unit: fix a -Wformat-trunction warning
@ 2022-08-10 12:15 marcandre.lureau
2022-08-10 12:24 ` Markus Armbruster
2022-08-12 10:23 ` Peter Maydell
0 siblings, 2 replies; 4+ messages in thread
From: marcandre.lureau @ 2022-08-10 12:15 UTC (permalink / raw)
To: qemu-devel; +Cc: Marc-André Lureau, Markus Armbruster, Michael Roth
From: Marc-André Lureau <marcandre.lureau@redhat.com>
../tests/test-qobject-input-visitor.c: In function ‘test_visitor_in_list’:
../tests/test-qobject-input-visitor.c:454:49: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 6 [-Wformat-truncation=]
454 | snprintf(string, sizeof(string), "string%d", i);
| ^~
../tests/test-qobject-input-visitor.c:454:42: note: directive argument in the range [0, 2147483606]
454 | snprintf(string, sizeof(string), "string%d", i);
| ^~~~~~~~~~
../tests/test-qobject-input-visitor.c:454:9: note: ‘snprintf’ output between 8 and 17 bytes into a destination of size 12
454 | snprintf(string, sizeof(string), "string%d", i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Not trying to be clever, this is called 3 times during tests,
let simply use g_strdup_printf().
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
tests/unit/test-qobject-input-visitor.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/tests/unit/test-qobject-input-visitor.c b/tests/unit/test-qobject-input-visitor.c
index 14329dabcf..5f614afdbf 100644
--- a/tests/unit/test-qobject-input-visitor.c
+++ b/tests/unit/test-qobject-input-visitor.c
@@ -447,9 +447,8 @@ static void test_visitor_in_list(TestInputVisitorData *data,
g_assert(head != NULL);
for (i = 0, item = head; item; item = item->next, i++) {
- char string[12];
+ g_autofree char *string = g_strdup_printf("string%d", i);
- snprintf(string, sizeof(string), "string%d", i);
g_assert_cmpstr(item->value->string, ==, string);
g_assert_cmpint(item->value->integer, ==, 42 + i);
}
--
2.37.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] tests/unit: fix a -Wformat-trunction warning
2022-08-10 12:15 [PATCH] tests/unit: fix a -Wformat-trunction warning marcandre.lureau
@ 2022-08-10 12:24 ` Markus Armbruster
2022-08-10 13:54 ` Philippe Mathieu-Daudé via
2022-08-12 10:23 ` Peter Maydell
1 sibling, 1 reply; 4+ messages in thread
From: Markus Armbruster @ 2022-08-10 12:24 UTC (permalink / raw)
To: marcandre.lureau; +Cc: qemu-devel, Michael Roth
Typo in subject, it's -Wformat-truncation
marcandre.lureau@redhat.com writes:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> ../tests/test-qobject-input-visitor.c: In function ‘test_visitor_in_list’:
> ../tests/test-qobject-input-visitor.c:454:49: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 6 [-Wformat-truncation=]
> 454 | snprintf(string, sizeof(string), "string%d", i);
> | ^~
> ../tests/test-qobject-input-visitor.c:454:42: note: directive argument in the range [0, 2147483606]
> 454 | snprintf(string, sizeof(string), "string%d", i);
> | ^~~~~~~~~~
> ../tests/test-qobject-input-visitor.c:454:9: note: ‘snprintf’ output between 8 and 17 bytes into a destination of size 12
> 454 | snprintf(string, sizeof(string), "string%d", i);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Not trying to be clever, this is called 3 times during tests,
> let simply use g_strdup_printf().
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
> tests/unit/test-qobject-input-visitor.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/tests/unit/test-qobject-input-visitor.c b/tests/unit/test-qobject-input-visitor.c
> index 14329dabcf..5f614afdbf 100644
> --- a/tests/unit/test-qobject-input-visitor.c
> +++ b/tests/unit/test-qobject-input-visitor.c
> @@ -447,9 +447,8 @@ static void test_visitor_in_list(TestInputVisitorData *data,
> g_assert(head != NULL);
>
> for (i = 0, item = head; item; item = item->next, i++) {
> - char string[12];
> + g_autofree char *string = g_strdup_printf("string%d", i);
>
> - snprintf(string, sizeof(string), "string%d", i);
> g_assert_cmpstr(item->value->string, ==, string);
> g_assert_cmpint(item->value->integer, ==, 42 + i);
> }
Even less clever would be char string[32].
Anyway, with the typo corrected:
Reviewed-by: Markus Armbruster <armbru@redhat.com>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] tests/unit: fix a -Wformat-trunction warning
2022-08-10 12:24 ` Markus Armbruster
@ 2022-08-10 13:54 ` Philippe Mathieu-Daudé via
0 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-08-10 13:54 UTC (permalink / raw)
To: Markus Armbruster, marcandre.lureau; +Cc: qemu-devel, Michael Roth
On 10/8/22 14:24, Markus Armbruster wrote:
> Typo in subject, it's -Wformat-truncation
>
> marcandre.lureau@redhat.com writes:
>
>> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>>
>> ../tests/test-qobject-input-visitor.c: In function ‘test_visitor_in_list’:
>> ../tests/test-qobject-input-visitor.c:454:49: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 6 [-Wformat-truncation=]
>> 454 | snprintf(string, sizeof(string), "string%d", i);
>> | ^~
>> ../tests/test-qobject-input-visitor.c:454:42: note: directive argument in the range [0, 2147483606]
>> 454 | snprintf(string, sizeof(string), "string%d", i);
>> | ^~~~~~~~~~
>> ../tests/test-qobject-input-visitor.c:454:9: note: ‘snprintf’ output between 8 and 17 bytes into a destination of size 12
>> 454 | snprintf(string, sizeof(string), "string%d", i);
>> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> Not trying to be clever, this is called 3 times during tests,
>> let simply use g_strdup_printf().
>>
>> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
>> ---
>> tests/unit/test-qobject-input-visitor.c | 3 +--
>> 1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/tests/unit/test-qobject-input-visitor.c b/tests/unit/test-qobject-input-visitor.c
>> index 14329dabcf..5f614afdbf 100644
>> --- a/tests/unit/test-qobject-input-visitor.c
>> +++ b/tests/unit/test-qobject-input-visitor.c
>> @@ -447,9 +447,8 @@ static void test_visitor_in_list(TestInputVisitorData *data,
>> g_assert(head != NULL);
>>
>> for (i = 0, item = head; item; item = item->next, i++) {
>> - char string[12];
>> + g_autofree char *string = g_strdup_printf("string%d", i);
>>
>> - snprintf(string, sizeof(string), "string%d", i);
>> g_assert_cmpstr(item->value->string, ==, string);
>> g_assert_cmpint(item->value->integer, ==, 42 + i);
>> }
>
> Even less clever would be char string[32].
Or assert(i < 10) before calling snprintf (the test data only contains 3
strings).
>
> Anyway, with the typo corrected:
> Reviewed-by: Markus Armbruster <armbru@redhat.com>
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] tests/unit: fix a -Wformat-trunction warning
2022-08-10 12:15 [PATCH] tests/unit: fix a -Wformat-trunction warning marcandre.lureau
2022-08-10 12:24 ` Markus Armbruster
@ 2022-08-12 10:23 ` Peter Maydell
1 sibling, 0 replies; 4+ messages in thread
From: Peter Maydell @ 2022-08-12 10:23 UTC (permalink / raw)
To: marcandre.lureau; +Cc: qemu-devel, Markus Armbruster, Michael Roth
On Wed, 10 Aug 2022 at 13:20, <marcandre.lureau@redhat.com> wrote:
>
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> ../tests/test-qobject-input-visitor.c: In function ‘test_visitor_in_list’:
> ../tests/test-qobject-input-visitor.c:454:49: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 6 [-Wformat-truncation=]
> 454 | snprintf(string, sizeof(string), "string%d", i);
> | ^~
> ../tests/test-qobject-input-visitor.c:454:42: note: directive argument in the range [0, 2147483606]
> 454 | snprintf(string, sizeof(string), "string%d", i);
> | ^~~~~~~~~~
> ../tests/test-qobject-input-visitor.c:454:9: note: ‘snprintf’ output between 8 and 17 bytes into a destination of size 12
> 454 | snprintf(string, sizeof(string), "string%d", i);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Not trying to be clever, this is called 3 times during tests,
> let simply use g_strdup_printf().
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This is a pretty safe fix and compiler warnings seem like they're
worth fixing for rc3. Since I need to do a target-arm pullreq
anyway I'll take this via that, unless anybody objects.
(I've fixed up the commit message typo in my tree.)
thanks
-- PMM
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-08-12 10:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-10 12:15 [PATCH] tests/unit: fix a -Wformat-trunction warning marcandre.lureau
2022-08-10 12:24 ` Markus Armbruster
2022-08-10 13:54 ` Philippe Mathieu-Daudé via
2022-08-12 10:23 ` Peter Maydell
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.