All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Thomas Huth <thuth@redhat.com>
Cc: AlexChen <alex.chen@huawei.com>,
	 lvivier@redhat.com,  Paolo Bonzini <pbonzini@redhat.com>,
	 QEMU Trivial <qemu-trivial@nongnu.org>,
	 QEMU <qemu-devel@nongnu.org>
Subject: Re: [PATCH] qtest: Fix bad printf format specifiers
Date: Fri, 06 Nov 2020 07:33:55 +0100	[thread overview]
Message-ID: <87wnyzouy4.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <18690aa2-3de9-70ad-477f-934724b284a0@redhat.com> (Thomas Huth's message of "Thu, 5 Nov 2020 06:58:31 +0100")

Thomas Huth <thuth@redhat.com> writes:

> On 05/11/2020 06.14, AlexChen wrote:
>> On 2020/11/4 18:44, Thomas Huth wrote:
>>> On 04/11/2020 11.23, AlexChen wrote:
>>>> We should use printf format specifier "%u" instead of "%d" for
>>>> argument of type "unsigned int".
>>>>
>>>> Reported-by: Euler Robot <euler.robot@huawei.com>
>>>> Signed-off-by: Alex Chen <alex.chen@huawei.com>
>>>> ---
>>>>  tests/qtest/arm-cpu-features.c | 8 ++++----
>>>>  1 file changed, 4 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/tests/qtest/arm-cpu-features.c b/tests/qtest/arm-cpu-features.c
>>>> index d20094d5a7..bc681a95d5 100644
>>>> --- a/tests/qtest/arm-cpu-features.c
>>>> +++ b/tests/qtest/arm-cpu-features.c
>>>> @@ -536,7 +536,7 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
>>>>          if (kvm_supports_sve) {
>>>>              g_assert(vls != 0);
>>>>              max_vq = 64 - __builtin_clzll(vls);
>>>> -            sprintf(max_name, "sve%d", max_vq * 128);
>>>> +            sprintf(max_name, "sve%u", max_vq * 128);
>>>>
>>>>              /* Enabling a supported length is of course fine. */
>>>>              assert_sve_vls(qts, "host", vls, "{ %s: true }", max_name);
>>>> @@ -556,7 +556,7 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
>>>>                   * unless all larger, supported vector lengths are also
>>>>                   * disabled.
>>>>                   */
>>>> -                sprintf(name, "sve%d", vq * 128);
>>>> +                sprintf(name, "sve%u", vq * 128);
>>>>                  error = g_strdup_printf("cannot disable %s", name);
>>>>                  assert_error(qts, "host", error,
>>>>                               "{ %s: true, %s: false }",
>>>> @@ -569,7 +569,7 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
>>>>               * we need at least one vector length enabled.
>>>>               */
>>>>              vq = __builtin_ffsll(vls);
>>>> -            sprintf(name, "sve%d", vq * 128);
>>>> +            sprintf(name, "sve%u", vq * 128);
>>>>              error = g_strdup_printf("cannot disable %s", name);
>>>>              assert_error(qts, "host", error, "{ %s: false }", name);
>>>>              g_free(error);
>>>> @@ -581,7 +581,7 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
>>>>                  }
>>>>              }
>>>>              if (vq <= SVE_MAX_VQ) {
>>>> -                sprintf(name, "sve%d", vq * 128);
>>>> +                sprintf(name, "sve%u", vq * 128);
>>>>                  error = g_strdup_printf("cannot enable %s", name);
>>>>                  assert_error(qts, "host", error, "{ %s: true }", name);
>>>>                  g_free(error);
>>>>
>>>
>>> max_vq and vq are both "uint32_t" and not "unsigned int" ... so if you want
>>> to fix this really really correctly, please use PRIu32 from inttypes.h instead.
>>>
>> 
>> Hi Thomas,
>> Thanks for your review.
>> According to the definition of the macro PRIu32(# define PRIu32         "u"),
>> using PRIu32 works the same as using %u to print, and using PRIu32 to print
>> is relatively rare in QEMU(%u 720, PRIu32 only 120). Can we continue to use %u to
>> print max_vq and vq in this patch.
>> Of course, this is just my small small suggestion. If you think it is better to use
>> PRIu32 for printing, I will send patch V2.
>
> Well, %u happens to work since "int" is 32-bit with all current compilers
> that we support.

Yes, it works.

>                  But if there is ever a compiler where the size of int is
> different, you'll get a compiler warning here again.

No, we won't.

If we ever use a compiler where int is narrower than 32 bits, then the
type of the argument is actually uint32_t[1].  We can forget about this
case, because "int narrower than 32 bits" is not going to fly with our
code base.

If we ever use a compiler where int is wider than 32 bits, then the type
of the argument is *not* uint32_t[2].  PRIu32 will work anyway, because
it will actually retrieve an unsigned int argument, *not* an uint32_t
argument[3].

In other words "%" PRIu32 is just a less legible alias for "%u" in all
cases that matter.

And that's why I would simply use "%u".

>                                                      So if we now fix this
> up, then let's do it really right and use PRIu32, please.
>
>  Thomas


[1] Because promotion does nothing either argument, and the usual
arithmetic conversions convert to uint32_t.  See my first reply.

[2] Because uint32_t gets promoted to unsigned int.  See my first reply.

[3] Because variable arguments undergo default argument promotion (§
6.5.2.2 Function calls), which promotes uint32_t to unsigned int.



WARNING: multiple messages have this Message-ID (diff)
From: Markus Armbruster <armbru@redhat.com>
To: Thomas Huth <thuth@redhat.com>
Cc: AlexChen <alex.chen@huawei.com>,
	lvivier@redhat.com, QEMU Trivial <qemu-trivial@nongnu.org>,
	QEMU <qemu-devel@nongnu.org>, Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [PATCH] qtest: Fix bad printf format specifiers
Date: Fri, 06 Nov 2020 07:33:55 +0100	[thread overview]
Message-ID: <87wnyzouy4.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <18690aa2-3de9-70ad-477f-934724b284a0@redhat.com> (Thomas Huth's message of "Thu, 5 Nov 2020 06:58:31 +0100")

Thomas Huth <thuth@redhat.com> writes:

> On 05/11/2020 06.14, AlexChen wrote:
>> On 2020/11/4 18:44, Thomas Huth wrote:
>>> On 04/11/2020 11.23, AlexChen wrote:
>>>> We should use printf format specifier "%u" instead of "%d" for
>>>> argument of type "unsigned int".
>>>>
>>>> Reported-by: Euler Robot <euler.robot@huawei.com>
>>>> Signed-off-by: Alex Chen <alex.chen@huawei.com>
>>>> ---
>>>>  tests/qtest/arm-cpu-features.c | 8 ++++----
>>>>  1 file changed, 4 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/tests/qtest/arm-cpu-features.c b/tests/qtest/arm-cpu-features.c
>>>> index d20094d5a7..bc681a95d5 100644
>>>> --- a/tests/qtest/arm-cpu-features.c
>>>> +++ b/tests/qtest/arm-cpu-features.c
>>>> @@ -536,7 +536,7 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
>>>>          if (kvm_supports_sve) {
>>>>              g_assert(vls != 0);
>>>>              max_vq = 64 - __builtin_clzll(vls);
>>>> -            sprintf(max_name, "sve%d", max_vq * 128);
>>>> +            sprintf(max_name, "sve%u", max_vq * 128);
>>>>
>>>>              /* Enabling a supported length is of course fine. */
>>>>              assert_sve_vls(qts, "host", vls, "{ %s: true }", max_name);
>>>> @@ -556,7 +556,7 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
>>>>                   * unless all larger, supported vector lengths are also
>>>>                   * disabled.
>>>>                   */
>>>> -                sprintf(name, "sve%d", vq * 128);
>>>> +                sprintf(name, "sve%u", vq * 128);
>>>>                  error = g_strdup_printf("cannot disable %s", name);
>>>>                  assert_error(qts, "host", error,
>>>>                               "{ %s: true, %s: false }",
>>>> @@ -569,7 +569,7 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
>>>>               * we need at least one vector length enabled.
>>>>               */
>>>>              vq = __builtin_ffsll(vls);
>>>> -            sprintf(name, "sve%d", vq * 128);
>>>> +            sprintf(name, "sve%u", vq * 128);
>>>>              error = g_strdup_printf("cannot disable %s", name);
>>>>              assert_error(qts, "host", error, "{ %s: false }", name);
>>>>              g_free(error);
>>>> @@ -581,7 +581,7 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
>>>>                  }
>>>>              }
>>>>              if (vq <= SVE_MAX_VQ) {
>>>> -                sprintf(name, "sve%d", vq * 128);
>>>> +                sprintf(name, "sve%u", vq * 128);
>>>>                  error = g_strdup_printf("cannot enable %s", name);
>>>>                  assert_error(qts, "host", error, "{ %s: true }", name);
>>>>                  g_free(error);
>>>>
>>>
>>> max_vq and vq are both "uint32_t" and not "unsigned int" ... so if you want
>>> to fix this really really correctly, please use PRIu32 from inttypes.h instead.
>>>
>> 
>> Hi Thomas,
>> Thanks for your review.
>> According to the definition of the macro PRIu32(# define PRIu32         "u"),
>> using PRIu32 works the same as using %u to print, and using PRIu32 to print
>> is relatively rare in QEMU(%u 720, PRIu32 only 120). Can we continue to use %u to
>> print max_vq and vq in this patch.
>> Of course, this is just my small small suggestion. If you think it is better to use
>> PRIu32 for printing, I will send patch V2.
>
> Well, %u happens to work since "int" is 32-bit with all current compilers
> that we support.

Yes, it works.

>                  But if there is ever a compiler where the size of int is
> different, you'll get a compiler warning here again.

No, we won't.

If we ever use a compiler where int is narrower than 32 bits, then the
type of the argument is actually uint32_t[1].  We can forget about this
case, because "int narrower than 32 bits" is not going to fly with our
code base.

If we ever use a compiler where int is wider than 32 bits, then the type
of the argument is *not* uint32_t[2].  PRIu32 will work anyway, because
it will actually retrieve an unsigned int argument, *not* an uint32_t
argument[3].

In other words "%" PRIu32 is just a less legible alias for "%u" in all
cases that matter.

And that's why I would simply use "%u".

>                                                      So if we now fix this
> up, then let's do it really right and use PRIu32, please.
>
>  Thomas


[1] Because promotion does nothing either argument, and the usual
arithmetic conversions convert to uint32_t.  See my first reply.

[2] Because uint32_t gets promoted to unsigned int.  See my first reply.

[3] Because variable arguments undergo default argument promotion (§
6.5.2.2 Function calls), which promotes uint32_t to unsigned int.



  reply	other threads:[~2020-11-06  6:34 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-04 10:23 [PATCH] qtest: Fix bad printf format specifiers AlexChen
2020-11-04 10:23 ` AlexChen
2020-11-04 10:44 ` Thomas Huth
2020-11-04 10:44   ` Thomas Huth
2020-11-05  5:14   ` AlexChen
2020-11-05  5:14     ` AlexChen
2020-11-05  5:58     ` Thomas Huth
2020-11-05  5:58       ` Thomas Huth
2020-11-06  6:33       ` Markus Armbruster [this message]
2020-11-06  6:33         ` Markus Armbruster
2020-11-06 14:18         ` Philippe Mathieu-Daudé
2020-11-06 15:36           ` Markus Armbruster
2020-11-06 15:36             ` Markus Armbruster
2020-11-08  7:51           ` Thomas Huth
2020-11-08  7:51             ` Thomas Huth
2020-11-09  7:57             ` Markus Armbruster
2020-11-09  7:57               ` Markus Armbruster
2020-11-09  9:56               ` Alex Chen
2020-11-09  9:56                 ` Alex Chen
2020-11-09 12:50                 ` Markus Armbruster
2020-11-10  8:09                   ` Thomas Huth
2020-11-10  8:09                     ` Thomas Huth
2020-11-11  9:53                     ` Markus Armbruster
2020-11-11  9:53                       ` Markus Armbruster
2020-11-05  8:19   ` Markus Armbruster
2020-11-05  8:19     ` Markus Armbruster
2020-11-08  7:42     ` Thomas Huth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wnyzouy4.fsf@dusky.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=alex.chen@huawei.com \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.