Skip to contentSkip to conversation

trace/simple: Fix warning and wrong trace file name for MinGW

PATCH In review Stefan Weil 3 participants 7 messages Sep 25, 2015 – Sep 28, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 trace/simple: Fix warning and wrong trace file name for MinGW

On Windows, getpid() always returns an int value, but pid_t (which is expected by the format string) is either a 32 bit or a 64 bit value.

Without a type cast (or a modified format string), the compiler prints a warning when building for 64 bit Windows and the resulting trace_file_name will include a wrong pid:

trace/simple.c:332:9: warning:
format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘int’ [-Wformat=]

---
 trace/simple.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
trace/simple.c +2 -1
index 11ad030..56a624c 100644
@@ -329,7 +329,8 @@ bool st_set_trace_file(const char *file)
     g_free(trace_file_name);
 
     if (!file) {
-        trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, getpid());
+        /* Type cast needed for Windows where getpid() returns an int. */
+        trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, (pid_t)getpid());
     } else {
         trace_file_name = g_strdup_printf("%s", file);
     }
Signature · 1 line
2.1.4
#1 lore raw
Quoted 21 lines · Stefan Weil

On Windows, getpid() always returns an int value, but pid_t (which is expected by the format string) is either a 32 bit or a 64 bit value.

Without a type cast (or a modified format string), the compiler prints a warning when building for 64 bit Windows and the resulting trace_file_name will include a wrong pid:

trace/simple.c:332:9: warning:
format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘int’ [-Wformat=]

---
 trace/simple.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
trace/simple.c +0 -0
index 11ad030..56a624c 100644

trace/simple.c bool st_set_trace_file(const char *file) · line 336

Quoted 9 lines
trace/simple.c +2 -1
@@ -329,7 +329,8 @@ bool st_set_trace_file(const char *file)
     g_free(trace_file_name);
 
     if (!file) {
-        trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, getpid());
+        /* Type cast needed for Windows where getpid() returns an int. */
+        trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, (pid_t)getpid());
     } else {
         trace_file_name = g_strdup_printf("%s", file);
     }

First we go to the trouble of defining a platform-dependent FMT_pid, and then we get to cast anyway. Meh.

Can you explain why osdep.h's

    #define FMT_pid "%" PRId64

is appropriate for Windows?

#2 lore raw

Am 28.09.2015 um 09:00 schrieb Markus Armbruster:

Quoted 35 lines
Quoted 21 lines · Stefan Weil

On Windows, getpid() always returns an int value, but pid_t (which is expected by the format string) is either a 32 bit or a 64 bit value.

Without a type cast (or a modified format string), the compiler prints a warning when building for 64 bit Windows and the resulting trace_file_name will include a wrong pid:

trace/simple.c:332:9: warning:
format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘int’ [-Wformat=]

---
 trace/simple.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
trace/simple.c +0 -0
index 11ad030..56a624c 100644

trace/simple.c bool st_set_trace_file(const char *file) · line 336

Quoted 9 lines
trace/simple.c +2 -1
@@ -329,7 +329,8 @@ bool st_set_trace_file(const char *file)
     g_free(trace_file_name);
 
     if (!file) {
-        trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, getpid());
+        /* Type cast needed for Windows where getpid() returns an int. */
+        trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, (pid_t)getpid());
     } else {
         trace_file_name = g_strdup_printf("%s", file);
     }

First we go to the trouble of defining a platform-dependent FMT_pid, and then we get to cast anyway. Meh.

Quoted 5 lines

Can you explain why osdep.h's

    #define FMT_pid "%" PRId64

is appropriate for Windows?

Don't blame me for any strangeness which you might find in Windows. :-)

Mingw-w64 sys/types.h defines pid_t to be either an int or an __int64. FMT_pid must match these definitions.

But getpid returns an int, not a pid_t...

#3 lore raw
Quoted 4 lines · Stefan Weil
Quoted 4 lines

Can you explain why osdep.h's

    #define FMT_pid "%" PRId64
Quoted 8 lines
Quoted 1 line

is appropriate for Windows?

Don't blame me for any strangeness which you might find in Windows. :-)

Mingw-w64 sys/types.h defines pid_t to be either an int or an __int64. FMT_pid must match these definitions.

But getpid returns an int, not a pid_t...

Can we 1) file a bug against mingw for their buggy getpid(), and 2) write a wrapper that makes getpid() always return pid_t in the meantime?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw

Am 28.09.2015 um 17:06 schrieb Eric Blake:

Quoted 7 lines · Stefan Weil
Quoted 6 lines
Quoted 6 lines

Can you explain why osdep.h's

    #define FMT_pid "%" PRId64

is appropriate for Windows?

Quoted 8 lines
Quoted 6 lines

Don't blame me for any strangeness which you might find in Windows. :-)

Mingw-w64 sys/types.h defines pid_t to be either an int or an __int64. FMT_pid must match these definitions.

But getpid returns an int, not a pid_t...

Can we 1) file a bug against mingw for their buggy getpid(), and 2) write a wrapper that makes getpid() always return pid_t in the meantime?

1) No, because MinGW and Mingw-w64 use the same declaration as MS. They cannot change that, of course. Nor will MS correct this after many years, for obvious reasons.

2) We could use a wrapper qemu_getpid or #define getpid() ((pid_t)getpid()). But that would require several other code changes.

#5 lore raw
Quoted 13 lines · Stefan Weil

Am 28.09.2015 um 17:06 schrieb Eric Blake:

Quoted 12 lines · Stefan Weil
Quoted 11 lines
Quoted 6 lines

Can you explain why osdep.h's

    #define FMT_pid "%" PRId64

is appropriate for Windows?

Don't blame me for any strangeness which you might find in Windows. :-)

Mingw-w64 sys/types.h defines pid_t to be either an int or an __int64. FMT_pid must match these definitions.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

But getpid returns an int, not a pid_t...

Can we 1) file a bug against mingw for their buggy getpid(), and 2) write a wrapper that makes getpid() always return pid_t in the meantime?

1) No, because MinGW and Mingw-w64 use the same declaration as MS. They cannot change that, of course. Nor will MS correct this after many years, for obvious reasons.

If getpid() always returns int, then defining pid_t to __int64 feels wrong. I still claim its a mingw bug to have a mismatch in types.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw

Am 28.09.2015 um 17:50 schrieb Eric Blake:

Quoted 18 lines · Stefan Weil
Quoted 17 lines

Am 28.09.2015 um 17:06 schrieb Eric Blake:

Quoted 15 lines · Stefan Weil
Quoted 5 lines
Quoted 5 lines

Can you explain why osdep.h's

    #define FMT_pid "%" PRId64
Quoted 7 lines
Quoted 1 line

is appropriate for Windows?

Don't blame me for any strangeness which you might find in Windows. :-)

Mingw-w64 sys/types.h defines pid_t to be either an int or an __int64. FMT_pid must match these definitions.

But getpid returns an int, not a pid_t...

Can we 1) file a bug against mingw for their buggy getpid(), and 2) write a wrapper that makes getpid() always return pid_t in the meantime?

Quoted 7 lines
Quoted 3 lines

1) No, because MinGW and Mingw-w64 use the same declaration as MS. They cannot change that, of course. Nor will MS correct this after many years, for obvious reasons.

If getpid() always returns int, then defining pid_t to __int64 feels wrong. I still claim its a mingw bug to have a mismatch in types.

I have sent this question to [email protected].

Stefan