Patchwork [5/6] Remove warning in printf due to type mismatch

login
register
mail settings
Submitter Alexandre Raymond
Date May 27, 2011, 5:22 p.m.
Message ID <1306516951-31105-6-git-send-email-cerbere@gmail.com>
Download mbox | patch
Permalink /patch/97724/
State New
Headers show

Comments

Alexandre Raymond - May 27, 2011, 5:22 p.m.
----8<----
qemu/target-lm32/translate.c: In function ‘gen_intermediate_code_internal’:
qemu/target-lm32/translate.c:1135: warning: format ‘%zd’ expects type ‘signed size_t’, but argument 4 has type ‘int’
----8<----

Both gen_opc_ptr and gen_opc_buf are "uint16_t *", so a simple '%d' should
be able to describe their relative difference.

Signed-off-by: Alexandre Raymond <cerbere@gmail.com>
---
 target-lm32/translate.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Stefan Weil - May 27, 2011, 5:35 p.m.
Am 27.05.2011 19:22, schrieb Alexandre Raymond:
> ----8<----
> qemu/target-lm32/translate.c: In function 
> ‘gen_intermediate_code_internal’:
> qemu/target-lm32/translate.c:1135: warning: format ‘%zd’ expects type 
> ‘signed size_t’, but argument 4 has type ‘int’
> ----8<----
>
> Both gen_opc_ptr and gen_opc_buf are "uint16_t *", so a simple '%d' should
> be able to describe their relative difference.
>
> Signed-off-by: Alexandre Raymond <cerbere@gmail.com>
> ---
> target-lm32/translate.c | 2 +-
> 1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/target-lm32/translate.c b/target-lm32/translate.c
> index eb21158..0f69f27 100644
> --- a/target-lm32/translate.c
> +++ b/target-lm32/translate.c
> @@ -1132,7 +1132,7 @@ static void 
> gen_intermediate_code_internal(CPUState *env,
> if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
> qemu_log("\n");
> log_target_disas(pc_start, dc->pc - pc_start, 0);
> - qemu_log("\nisize=%d osize=%zd\n",
> + qemu_log("\nisize=%d osize=%d\n",
> dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
> }
> #endif

Nack.

The original code is correct, because the difference of two pointers
is always of type ssize_t (well, obviously not with your compiler,
but then I assume that your compiler is broken).

This pattern is quite common in QEMU, so it looks like there is
another problem here (otherwise you would get more errors of this kind).

Cheers,
Stefan W.
Markus Armbruster - May 27, 2011, 7:11 p.m.
Stefan Weil <weil@mail.berlios.de> writes:

> Am 27.05.2011 19:22, schrieb Alexandre Raymond:
>> ----8<----
>> qemu/target-lm32/translate.c: In function
>> ‘gen_intermediate_code_internal’:
>> qemu/target-lm32/translate.c:1135: warning: format ‘%zd’ expects
>> type ‘signed size_t’, but argument 4 has type ‘int’
>> ----8<----
>>
>> Both gen_opc_ptr and gen_opc_buf are "uint16_t *", so a simple '%d' should
>> be able to describe their relative difference.
>>
>> Signed-off-by: Alexandre Raymond <cerbere@gmail.com>
>> ---
>> target-lm32/translate.c | 2 +-
>> 1 files changed, 1 insertions(+), 1 deletions(-)
>>
>> diff --git a/target-lm32/translate.c b/target-lm32/translate.c
>> index eb21158..0f69f27 100644
>> --- a/target-lm32/translate.c
>> +++ b/target-lm32/translate.c
>> @@ -1132,7 +1132,7 @@ static void
>> gen_intermediate_code_internal(CPUState *env,
>> if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
>> qemu_log("\n");
>> log_target_disas(pc_start, dc->pc - pc_start, 0);
>> - qemu_log("\nisize=%d osize=%zd\n",
>> + qemu_log("\nisize=%d osize=%d\n",
>> dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
>> }
>> #endif
>
> Nack.
>
> The original code is correct, because the difference of two pointers
> is always of type ssize_t (well, obviously not with your compiler,
> but then I assume that your compiler is broken).

ISO/IEC 9899:1999 §6.5.6 on pointer subtraction:

    The size of the result is implementation-defined, and its type (a
    signed integer type) is ptrdiff_t defined in the <stddef.h> header.

The pedantically correct way to print a pointer difference is the 't'
type modifier.  Ibid. §7.19.6.1 on fprintf():

    t   Specifies  that a following d, i, o, u, x, or X conversion
        specifier applies to a ptrdiff_t or the corresponding unsigned
        integer type argument; or that a following n conversion
        specifier applies to a pointer to a ptrdiff_t argument.

ssize_t is POSIX, not ISO C.  It can differ from ptrdiff_t only if
ptrdiff_t has a different size than size_t, which would be kind of sick.
ISO C permits all kinds of sickness.  Whether tolerating a particular
sickness is worth our while is another question.

[...]
Stefan Weil - May 27, 2011, 8:44 p.m.
Am 27.05.2011 21:11, schrieb Markus Armbruster:
> Stefan Weil <weil@mail.berlios.de> writes:
>
>> Am 27.05.2011 19:22, schrieb Alexandre Raymond:
>>> ----8<----
>>> qemu/target-lm32/translate.c: In function
>>> ‘gen_intermediate_code_internal’:
>>> qemu/target-lm32/translate.c:1135: warning: format ‘%zd’ expects
>>> type ‘signed size_t’, but argument 4 has type ‘int’
>>> ----8<----
>>>
>>> Both gen_opc_ptr and gen_opc_buf are "uint16_t *", so a simple '%d' 
>>> should
>>> be able to describe their relative difference.
>>>
>>> Signed-off-by: Alexandre Raymond <cerbere@gmail.com>
>>> ---
>>> target-lm32/translate.c | 2 +-
>>> 1 files changed, 1 insertions(+), 1 deletions(-)
>>>
>>> diff --git a/target-lm32/translate.c b/target-lm32/translate.c
>>> index eb21158..0f69f27 100644
>>> --- a/target-lm32/translate.c
>>> +++ b/target-lm32/translate.c
>>> @@ -1132,7 +1132,7 @@ static void
>>> gen_intermediate_code_internal(CPUState *env,
>>> if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
>>> qemu_log("\n");
>>> log_target_disas(pc_start, dc->pc - pc_start, 0);
>>> - qemu_log("\nisize=%d osize=%zd\n",
>>> + qemu_log("\nisize=%d osize=%d\n",
>>> dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
>>> }
>>> #endif
>>
>> Nack.
>>
>> The original code is correct, because the difference of two pointers
>> is always of type ssize_t (well, obviously not with your compiler,
>> but then I assume that your compiler is broken).
>
> ISO/IEC 9899:1999 §6.5.6 on pointer subtraction:
>
> The size of the result is implementation-defined, and its type (a
> signed integer type) is ptrdiff_t defined in the <stddef.h> header.
>
> The pedantically correct way to print a pointer difference is the 't'
> type modifier. Ibid. §7.19.6.1 on fprintf():
>
> t Specifies that a following d, i, o, u, x, or X conversion
> specifier applies to a ptrdiff_t or the corresponding unsigned
> integer type argument; or that a following n conversion
> specifier applies to a pointer to a ptrdiff_t argument.
>
> ssize_t is POSIX, not ISO C. It can differ from ptrdiff_t only if
> ptrdiff_t has a different size than size_t, which would be kind of sick.
> ISO C permits all kinds of sickness. Whether tolerating a particular
> sickness is worth our while is another question.
>
> [...]

That's correct. And ptrdiff_t needs %td.

Alexandre, could you please try %td instead of %zd?

Cheers,
Stefan W.
Alexandre Raymond - May 27, 2011, 10:10 p.m.
Hi Stefan and Markus,

Thanks for your feedback :)

"%td" doesn't generate warnings on Linux nor on OSX.

Alexandre

On Fri, May 27, 2011 at 4:44 PM, Stefan Weil <weil@mail.berlios.de> wrote:
> Am 27.05.2011 21:11, schrieb Markus Armbruster:
>>
>> Stefan Weil <weil@mail.berlios.de> writes:
>>
>>> Am 27.05.2011 19:22, schrieb Alexandre Raymond:
>>>>
>>>> ----8<----
>>>> qemu/target-lm32/translate.c: In function
>>>> ‘gen_intermediate_code_internal’:
>>>> qemu/target-lm32/translate.c:1135: warning: format ‘%zd’ expects
>>>> type ‘signed size_t’, but argument 4 has type ‘int’
>>>> ----8<----
>>>>
>>>> Both gen_opc_ptr and gen_opc_buf are "uint16_t *", so a simple '%d'
>>>> should
>>>> be able to describe their relative difference.
>>>>
>>>> Signed-off-by: Alexandre Raymond <cerbere@gmail.com>
>>>> ---
>>>> target-lm32/translate.c | 2 +-
>>>> 1 files changed, 1 insertions(+), 1 deletions(-)
>>>>
>>>> diff --git a/target-lm32/translate.c b/target-lm32/translate.c
>>>> index eb21158..0f69f27 100644
>>>> --- a/target-lm32/translate.c
>>>> +++ b/target-lm32/translate.c
>>>> @@ -1132,7 +1132,7 @@ static void
>>>> gen_intermediate_code_internal(CPUState *env,
>>>> if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
>>>> qemu_log("\n");
>>>> log_target_disas(pc_start, dc->pc - pc_start, 0);
>>>> - qemu_log("\nisize=%d osize=%zd\n",
>>>> + qemu_log("\nisize=%d osize=%d\n",
>>>> dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
>>>> }
>>>> #endif
>>>
>>> Nack.
>>>
>>> The original code is correct, because the difference of two pointers
>>> is always of type ssize_t (well, obviously not with your compiler,
>>> but then I assume that your compiler is broken).
>>
>> ISO/IEC 9899:1999 §6.5.6 on pointer subtraction:
>>
>> The size of the result is implementation-defined, and its type (a
>> signed integer type) is ptrdiff_t defined in the <stddef.h> header.
>>
>> The pedantically correct way to print a pointer difference is the 't'
>> type modifier. Ibid. §7.19.6.1 on fprintf():
>>
>> t Specifies that a following d, i, o, u, x, or X conversion
>> specifier applies to a ptrdiff_t or the corresponding unsigned
>> integer type argument; or that a following n conversion
>> specifier applies to a pointer to a ptrdiff_t argument.
>>
>> ssize_t is POSIX, not ISO C. It can differ from ptrdiff_t only if
>> ptrdiff_t has a different size than size_t, which would be kind of sick.
>> ISO C permits all kinds of sickness. Whether tolerating a particular
>> sickness is worth our while is another question.
>>
>> [...]
>
> That's correct. And ptrdiff_t needs %td.
>
> Alexandre, could you please try %td instead of %zd?
>
> Cheers,
> Stefan W.
>
>
Paolo Bonzini - May 28, 2011, 6:55 a.m.
On 05/28/2011 12:10 AM, Alexandre Raymond wrote:
> Hi Stefan and Markus,
>
> Thanks for your feedback :)
>
> "%td" doesn't generate warnings on Linux nor on OSX.

Stefan, what about Windows?

Paolo
Stefan Weil - May 28, 2011, 8:19 a.m.
Am 28.05.2011 08:55, schrieb Paolo Bonzini:
> On 05/28/2011 12:10 AM, Alexandre Raymond wrote:
>> Hi Stefan and Markus,
>>
>> Thanks for your feedback :)
>>
>> "%td" doesn't generate warnings on Linux nor on OSX.
>
> Stefan, what about Windows?
>
> Paolo

%td is ok for Windows, too.
Alexandre, please send an update of your patch using %td.

Stefan W.

Patch

diff --git a/target-lm32/translate.c b/target-lm32/translate.c
index eb21158..0f69f27 100644
--- a/target-lm32/translate.c
+++ b/target-lm32/translate.c
@@ -1132,7 +1132,7 @@  static void gen_intermediate_code_internal(CPUState *env,
     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
         qemu_log("\n");
         log_target_disas(pc_start, dc->pc - pc_start, 0);
-        qemu_log("\nisize=%d osize=%zd\n",
+        qemu_log("\nisize=%d osize=%d\n",
             dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
     }
 #endif