Skip to contentSkip to conversation

printf() fixes

PATCH In review Alejandro Colomar 3 participants 44 messages Sep 11, 2020 – Sep 15, 2020 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

cover printf() fixes

Hello Michael,

Here are some patches related to printf().

Cheers,

Alex

Alejandro Colomar (12):
dl_iterate_phdr.3: Cast 'ElfN_Word' to 'uintmax_t' for printf() malloc_hook.3: Remove unneeded cast, and print 'size_t' with "%zu" pthread_getcpuclockid.3: Cast 'time_t' to 'intmax_t' for printf() getgrent_r.3: Cast 'gid_t' to 'intmax_t' for printf() getpwent_r.3: Cast 'uid_t' to 'intmax_t' for printf() rtld-audit.7: Use "%u" rather than "%d" when printing 'unsigned int'
values
userfaultfd.2: Use 'PRIx64' rather than "%llx" when printing 64-bit
fixed-width types
clock_getres.2: Cast 'time_t' to 'intmax_t' for printf() and fix the
length modifiers
open_by_handle_at.2: Use "%u" rather than "%d" when printing 'unsigned
int' values
spu_run.2: Cast 'int' to 'unsigned int' when printing with "%x" mprotect.2: Use "%p" rather than casting to 'long' when printing
adresses (pointer values)
pidfd_open.2: Cast 'short' to 'unsigned int' when printing with "%x"

 man2/clock_getres.2          | 7 +++++--
 man2/mprotect.2              | 5 ++---
 man2/open_by_handle_at.2     | 2 +-
 man2/pidfd_open.2            | 3 ++-
 man2/spu_run.2               | 2 +-
 man2/userfaultfd.2           | 7 ++++---
 man3/dl_iterate_phdr.3       | 7 ++++---
 man3/getgrent_r.3            | 3 ++-
 man3/getpwent_r.3            | 5 +++--
 man3/malloc_hook.3           | 4 ++--
 man3/pthread_getcpuclockid.3 | 3 ++-
 man7/rtld-audit.7            | 6 +++---
 12 files changed, 31 insertions(+), 23 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/12 dl_iterate_phdr.3: Cast 'ElfN_Word' to 'uintmax_t' for printf()

---
 man3/dl_iterate_phdr.3 | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
man3/dl_iterate_phdr.3 +4 -3
index 70206a0ba..a8a85d8e6 100644
@@ -302,6 +302,7 @@ Name: "/lib64/ld-linux-x86-64.so.2" (7 segments)
 #include <link.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdint.h>
 
 static int
 callback(struct dl_phdr_info *info, size_t size, void *data)
@@ -325,10 +326,10 @@ callback(struct dl_phdr_info *info, size_t size, void *data)
                 (p_type == PT_GNU_STACK) ? "PT_GNU_STACK" :
                 (p_type == PT_GNU_RELRO) ? "PT_GNU_RELRO" : NULL;
 
-        printf("    %2d: [%14p; memsz:%7lx] flags: %#x; ", j,
+        printf("    %2d: [%14p; memsz:%7jx] flags: %#jx; ", j,
                 (void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr),
-                info\->dlpi_phdr[j].p_memsz,
-                info\->dlpi_phdr[j].p_flags);
+                (uintmax_t) info\->dlpi_phdr[j].p_memsz,
+                (uintmax_t) info\->dlpi_phdr[j].p_flags);
         if (type != NULL)
             printf("%s\en", type);
         else
Signature · 1 line
2.28.0
#2 lore raw

Re: [PATCH 01/12] dl_iterate_phdr.3: Cast 'ElfN_Word' to 'uintmax_t' for printf()

* Alejandro Colomar <[email protected]>, 2020-09-12, 01:14:

man3/dl_iterate_phdr.3 callback(struct dl_phdr_info *info, size_t size, void *data) · line 332

Quoted 7 lines
man3/dl_iterate_phdr.3 +3 -3
-        printf("    %2d: [%14p; memsz:%7lx] flags: %#x; ", j,
+        printf("    %2d: [%14p; memsz:%7jx] flags: %#jx; ", j,
                (void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr),
-                info\->dlpi_phdr[j].p_memsz,
-                info\->dlpi_phdr[j].p_flags);
+                (uintmax_t) info\->dlpi_phdr[j].p_memsz,
+                (uintmax_t) info\->dlpi_phdr[j].p_flags);

The commit message is misleading. ElfN_Word is always uint32_t, regardless of N. On the other hand, on 64-bit architectures the type of p_memsz is Elf64_Xword (i.e., uint64_t), not Elf64_Word.

Signature · 1 line
Jakub Wilk
#3 lore raw

Re: [PATCH 01/12] dl_iterate_phdr.3: Cast 'ElfN_Word' to 'uintmax_t' for printf()

Hi Jakub,

Quoted 10 lines · Jakub Wilk

* Alejandro Colomar <[email protected]>, 2020-09-12, 01:14:

man3/dl_iterate_phdr.3 callback(struct dl_phdr_info *info, size_t size, void *data) · line 332

Quoted 8 lines
man3/dl_iterate_phdr.3 +1 -1
-        printf("    %2d: [%14p; memsz:%7lx] flags: %#x; ", j,
+        printf("    %2d: [%14p; memsz:%7jx] flags: %#jx; ", j,
                (void *) (info\->dlpi_addr +

info\->dlpi_phdr[j].p_vaddr),

man3/dl_iterate_phdr.3 +2 -2
-                info\->dlpi_phdr[j].p_memsz,
-                info\->dlpi_phdr[j].p_flags);
+                (uintmax_t) info\->dlpi_phdr[j].p_memsz,
+                (uintmax_t) info\->dlpi_phdr[j].p_flags);
Quoted 4 lines

The commit message is misleading. ElfN_Word is always uint32_t, regardless of N. On the other hand, on 64-bit architectures the type of p_memsz is Elf64_Xword (i.e., uint64_t), not Elf64_Word.

Thanks for noticing that. I had doubts while reading the man page. I think it could be a bit more clear, but maybe it's me that I don't speak elfic :p

Maybe 'ElfW(Word)' would be more appropriate?

Thanks,

Alex

#4 lore raw

Re: [PATCH 01/12] dl_iterate_phdr.3: Cast 'ElfN_Word' to 'uintmax_t' for printf()

* Alejandro Colomar <[email protected]>, 2020-09-12, 10:31:

Quoted 10 lines
Quoted 10 lines

* Alejandro Colomar <[email protected]>, 2020-09-12, 01:14:

Quoted 8 lines
man3/dl_iterate_phdr.3 +1 -1
-        printf("    %2d: [%14p; memsz:%7lx] flags: %#x; ", j,
+        printf("    %2d: [%14p; memsz:%7jx] flags: %#jx; ", j,
                (void *) (info\->dlpi_addr +

info\->dlpi_phdr[j].p_vaddr),

man3/dl_iterate_phdr.3 +2 -2
-                info\->dlpi_phdr[j].p_memsz,
-                info\->dlpi_phdr[j].p_flags);
+                (uintmax_t) info\->dlpi_phdr[j].p_memsz,
+                (uintmax_t) info\->dlpi_phdr[j].p_flags);
Quoted 6 lines
Quoted 1 line

The commit message is misleading. ElfN_Word is always uint32_t, regardless of N. On the other hand, on 64-bit architectures the type of p_memsz is Elf64_Xword (i.e., uint64_t), not Elf64_Word.

Thanks for noticing that. I had doubts while reading the man page.
I think it could be a bit more clear, but maybe it's me that I don't speak elfic :p

Maybe 'ElfW(Word)' would be more appropriate?

I wouldn't know what "ElfW(Word)" means.

I'd write "Cast ELF types to uintmax_t" or something.

Signature · 1 line
Jakub Wilk
#5 lore raw

Re: [PATCH 01/12] dl_iterate_phdr.3: Cast 'ElfN_Word' to 'uintmax_t' for printf()

Quoted 6 lines · Jakub Wilk
Quoted 1 line

Maybe 'ElfW(Word)' would be more appropriate?

I wouldn't know what "ElfW(Word)" means.

I'd write "Cast ELF types to uintmax_t" or something.

Much better, yes. However, it doesn't matter anymore: fixed by this patch:

https://lore.kernel.org/linux-man/[email protected]/T/#u

Thanks,

Alex

#6 lore raw

PATCH 2/12 malloc_hook.3: Remove unneeded cast, and print 'size_t' with "%zu"

In reply to Alejandro Colomar
---
 man3/malloc_hook.3 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
man3/malloc_hook.3 +2 -2
index a509dc72f..fb45e30f2 100644
@@ -130,8 +130,8 @@ my_malloc_hook(size_t size, const void *caller)
     old_malloc_hook = __malloc_hook;
 
     /* printf() might call malloc(), so protect it too. */
-    printf("malloc(%u) called from %p returns %p\en",
-            (unsigned int) size, caller, result);
+    printf("malloc(%zu) called from %p returns %p\en",
+            size, caller, result);
 
     /* Restore our own hooks */
     __malloc_hook = my_malloc_hook;
Signature · 1 line
2.28.0
#7 lore raw

Re: [PATCH 02/12] malloc_hook.3: Remove unneeded cast, and print 'size_t' with "%zu"

Quoted 1 line · Alejandro Colomar

Thanks, Alex. Patch applied.

Cheers,

Michael

Quoted 20 lines
---
 man3/malloc_hook.3 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
man3/malloc_hook.3 +2 -2
index a509dc72f..fb45e30f2 100644
@@ -130,8 +130,8 @@ my_malloc_hook(size_t size, const void *caller)
     old_malloc_hook = __malloc_hook;
 
     /* printf() might call malloc(), so protect it too. */
-    printf("malloc(%u) called from %p returns %p\en",
-            (unsigned int) size, caller, result);
+    printf("malloc(%zu) called from %p returns %p\en",
+            size, caller, result);
 
     /* Restore our own hooks */
     __malloc_hook = my_malloc_hook;
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#8 lore raw

PATCH 3/12 pthread_getcpuclockid.3: Cast 'time_t' to 'intmax_t' for printf()

In reply to Alejandro Colomar
---
 man3/pthread_getcpuclockid.3 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
man3/pthread_getcpuclockid.3 +2 -1
index 703735b91..a457a4cee 100644
@@ -115,6 +115,7 @@ Subthread CPU time:        0.992
 
 #include <time.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <pthread.h>
@@ -143,7 +144,7 @@ pclock(char *msg, clockid_t cid)
     printf("%s", msg);
     if (clock_gettime(cid, &ts) == \-1)
         handle_error("clock_gettime");
-    printf("%4ld.%03ld\en", ts.tv_sec, ts.tv_nsec / 1000000);
+    printf("%4jd.%03ld\en", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
 }
 
 int
Signature · 1 line
2.28.0
#9 lore raw

Re: [PATCH 03/12] pthread_getcpuclockid.3: Cast 'time_t' to 'intmax_t' for printf()

Hi Alex,

Quoted 4 lines · Alejandro Colomar
---
 man3/pthread_getcpuclockid.3 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Casting to long is the historical practive here, and should be sufficient, don't you think?

Thanks,

Michael

Quoted 22 lines
man3/pthread_getcpuclockid.3 +2 -1
index 703735b91..a457a4cee 100644
@@ -115,6 +115,7 @@ Subthread CPU time:        0.992
 
 #include <time.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <pthread.h>
@@ -143,7 +144,7 @@ pclock(char *msg, clockid_t cid)
     printf("%s", msg);
     if (clock_gettime(cid, &ts) == \-1)
         handle_error("clock_gettime");
-    printf("%4ld.%03ld\en", ts.tv_sec, ts.tv_nsec / 1000000);
+    printf("%4jd.%03ld\en", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
 }
 
 int
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#10 lore raw

Re: [PATCH 03/12] pthread_getcpuclockid.3: Cast 'time_t' to 'intmax_t' for printf()

Hi Alex,

Quoted 10 lines · Michael Kerrisk (man-pages)

Hi Alex,

Quoted 4 lines · Alejandro Colomar
---
 man3/pthread_getcpuclockid.3 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Casting to long is the historical practive here, and should be sufficient, don't you think?

Oh -- I see Jakub made a similar comment for patch 08.

Do you want to redraft this patch too?

Thanks,

Michael

Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#11 lore raw

Re: [PATCH 03/12] pthread_getcpuclockid.3: Cast 'time_t' to 'intmax_t' for printf()

Hi Michael,

Quoted 12 lines · Michael Kerrisk (man-pages)

Hi Alex,

Quoted 9 lines · Michael Kerrisk (man-pages)

Hi Alex,

Quoted 4 lines · Alejandro Colomar
---
 man3/pthread_getcpuclockid.3 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Casting to long is the historical practive here, and should be

sufficient,

Quoted 10 lines
Quoted 1 line

don't you think?

Oh -- I see Jakub made a similar comment for patch 08.

Do you want to redraft this patch too?

Thanks,

Michael

The thing in patch 08 is that the numbers were modulo very small numbers, and therefore very small numbers:

Review

Quoted 6 lines
(quoted diff) +4 -2
-    printf("%2ldh %2ldm %2lds", (ts.tv_sec % SECS_IN_DAY) / 3600,
-            (ts.tv_sec % 3600) / 60, ts.tv_sec % 60);
+    printf("%2dh %2dm %2ds",
+            (int) (ts.tv_sec % SECS_IN_DAY) / 3600,
+            (int) (ts.tv_sec % 3600) / 60,
+            (int) ts.tv_sec % 60);

In this case it doesn't apply.

Cheers,

Alex

#12 lore raw

Re: [PATCH 03/12] pthread_getcpuclockid.3: Cast 'time_t' to 'intmax_t' for printf()

Hi Alex,

Quoted 31 lines · Alejandro Colomar

Hi Michael,

Quoted 12 lines · Michael Kerrisk (man-pages)

Hi Alex,

Quoted 9 lines · Michael Kerrisk (man-pages)

Hi Alex,

Quoted 4 lines · Alejandro Colomar
---
 man3/pthread_getcpuclockid.3 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Casting to long is the historical practive here, and should be

sufficient,

Quoted 10 lines
Quoted 1 line

don't you think?

Oh -- I see Jakub made a similar comment for patch 08.

Do you want to redraft this patch too?

Thanks,

Michael

The thing in patch 08 is that the numbers were modulo very small numbers, and therefore very small numbers:

Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +4 -2
-    printf("%2ldh %2ldm %2lds", (ts.tv_sec % SECS_IN_DAY) / 3600,
-            (ts.tv_sec % 3600) / 60, ts.tv_sec % 60);
+    printf("%2dh %2dm %2ds",
+            (int) (ts.tv_sec % SECS_IN_DAY) / 3600,
+            (int) (ts.tv_sec % 3600) / 60,
+            (int) ts.tv_sec % 60);

In this case it doesn't apply.

See my reply to patch 05, in a moment.

Thanks,

Michael

Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#13 lore raw

Re: [PATCH 03/12] pthread_getcpuclockid.3: Cast 'time_t' to 'intmax_t' for printf()

In reply to Michael Kerrisk (man-pages)

* Michael Kerrisk <[email protected]>, 2020-09-13, 07:58:

Quoted 1 line

Casting to long is the historical practive here, and should be sufficient, don't you think?

It general, the practice of casting time_t to long is wrong. There are existing systems that have 64-bit time_t but 32-bit long.

In this particular case, we're measuring CPU time, so there's practically no danger of overflow.

I'd either cast it to int (to make the code shortest and simplest) or to intmax_t (to be pedantically correct).

Signature · 1 line
Jakub Wilk
#14 lore raw

Re: [PATCH 03/12] pthread_getcpuclockid.3: Cast 'time_t' to 'intmax_t' for printf()

Hi Jakub,

Quoted 10 lines · Jakub Wilk

* Michael Kerrisk <[email protected]>, 2020-09-13, 07:58:

Quoted 2 lines

Casting to long is the historical practive here, and should be sufficient, don't you think?

It general, the practice of casting time_t to long is wrong. There are existing systems that have 64-bit time_t but 32-bit long.

In this particular case, we're measuring CPU time, so there's practically no danger of overflow.

Yes, what you say makes sense.

Quoted 2 lines

I'd either cast it to int (to make the code shortest and simplest) or to intmax_t (to be pedantically correct).

As per the patch from Alex, we'll universally move to intmax_t/uintmax_t casts.

Thanks,

Michael

Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#15 lore raw

PATCH 4/12 getgrent_r.3: Cast 'gid_t' to 'intmax_t' for printf()

In reply to Alejandro Colomar
---
 man3/getgrent_r.3 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
man3/getgrent_r.3 +2 -1
index 76deec370..8170cd417 100644
@@ -174,6 +174,7 @@ in the stream with all other threads.
 #define _GNU_SOURCE
 #include <grp.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #define BUFLEN 4096
 
@@ -189,7 +190,7 @@ main(void)
         i = getgrent_r(&grp, buf, sizeof(buf), &grpp);
         if (i)
             break;
-        printf("%s (%d):", grpp\->gr_name, grpp\->gr_gid);
+        printf("%s (%jd):", grpp\->gr_name, (intmax_t) grpp\->gr_gid);
         for (int j = 0; ; j++) {
             if (grpp\->gr_mem[j] == NULL)
                 break;
Signature · 1 line
2.28.0
#16 lore raw

Re: [PATCH 04/12] getgrent_r.3: Cast 'gid_t' to 'intmax_t' for printf()

Hi Alex,

See my reply to patch 05.

Thanks,

Michael

Quoted 27 lines · Alejandro Colomar
---
 man3/getgrent_r.3 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
man3/getgrent_r.3 +2 -1
index 76deec370..8170cd417 100644
@@ -174,6 +174,7 @@ in the stream with all other threads.
 #define _GNU_SOURCE
 #include <grp.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #define BUFLEN 4096
 
@@ -189,7 +190,7 @@ main(void)
         i = getgrent_r(&grp, buf, sizeof(buf), &grpp);
         if (i)
             break;
-        printf("%s (%d):", grpp\->gr_name, grpp\->gr_gid);
+        printf("%s (%jd):", grpp\->gr_name, (intmax_t) grpp\->gr_gid);
         for (int j = 0; ; j++) {
             if (grpp\->gr_mem[j] == NULL)
                 break;
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#17 lore raw

PATCH 5/12 getpwent_r.3: Cast 'uid_t' to 'intmax_t' for printf()

In reply to Alejandro Colomar
---
 man3/getpwent_r.3 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
man3/getpwent_r.3 +3 -2
index b6c1c281f..ef4c01c21 100644
@@ -179,6 +179,7 @@ in the stream with all other threads.
 #define _GNU_SOURCE
 #include <pwd.h>
 #include <stdio.h>
+#include <stdint.h>
 #define BUFLEN 4096
 
 int
@@ -194,8 +195,8 @@ main(void)
         i = getpwent_r(&pw, buf, sizeof(buf), &pwp);
         if (i)
             break;
-        printf("%s (%d)\etHOME %s\etSHELL %s\en", pwp\->pw_name,
-               pwp\->pw_uid, pwp\->pw_dir, pwp\->pw_shell);
+        printf("%s (%jd)\etHOME %s\etSHELL %s\en", pwp\->pw_name,
+               (intmax_t) pwp\->pw_uid, pwp\->pw_dir, pwp\->pw_shell);
     }
     endpwent();
     exit(EXIT_SUCCESS);
Signature · 1 line
2.28.0
#18 lore raw

Re: [PATCH 05/12] getpwent_r.3: Cast 'uid_t' to 'intmax_t' for printf()

Hi Alex,

Quoted 18 lines · Alejandro Colomar
---
 man3/getpwent_r.3 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
man3/getpwent_r.3 +1 -0
index b6c1c281f..ef4c01c21 100644
@@ -179,6 +179,7 @@ in the stream with all other threads.
 #define _GNU_SOURCE
 #include <pwd.h>
 #include <stdio.h>
+#include <stdint.h>
 #define BUFLEN 4096
 
 int

man3/getpwent_r.3 main(void) · line 202

Quoted 10 lines
man3/getpwent_r.3 +2 -2
@@ -194,8 +195,8 @@ main(void)
         i = getpwent_r(&pw, buf, sizeof(buf), &pwp);
         if (i)
             break;
-        printf("%s (%d)\etHOME %s\etSHELL %s\en", pwp\->pw_name,
-               pwp\->pw_uid, pwp\->pw_dir, pwp\->pw_shell);
+        printf("%s (%jd)\etHOME %s\etSHELL %s\en", pwp\->pw_name,
+               (intmax_t) pwp\->pw_uid, pwp\->pw_dir, pwp\->pw_shell);
     }
     endpwent();
     exit(EXIT_SUCCESS);

Casting to long is the historical practice here, and should be sufficient, don't you think? I realize there's an argument for making all of these system data type casts intmax_t / uintmax_t. But the counterarguments (admittedly not compelling), are:

* Some people might still be confined to a pre C-99 world
* Churn (lots of changes)
* (long) is a sufficient cast for all of these types (except off_t).

Your thoughts?

Thanks,

Michael

Thanks,

Michael

Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#19 lore raw

Re: [PATCH 05/12] getpwent_r.3: Cast 'uid_t' to 'intmax_t' for printf()

Hi Michael,

Quoted 4 lines · Michael Kerrisk (man-pages)

Casting to long is the historical practice here, and should be sufficient, don't you think? I realize there's an argument for making all of these system data type casts intmax_t / uintmax_t. But the counterarguments (admittedly not compelling), are:

I was casting to (long) until I accidentally met with a cast to (intmax_t) in ftw.3 (now I realize it was the only one in all of the pages; lucke me).

I saw there a good point in using those types and tried it to see what you think about them. I still haven't sent you a big patchset with s/(long)/(intmax_t)/ because I was waiting for this discussion :)

Quoted 2 lines

* Some people might still be confined to a pre C-99 world

I guess that people are dealing with very special hardware/software and can be trusted to understand what the C99 types are and which pre-C99 do the job for them; intmax_t.3 might also help ;).

If someone doesn't live in such a special world and just doesn't know the types, it's a good moment to learn them.

Quoted 1 line

* Churn (lots of changes)

True ... But if changes are trivial enough, maybe a single big patch can do it.

Quoted 2 lines

* (long) is a sufficient cast for all of these types (except off_t).

I don't like exceptions very much. Maybe someone sees (long) all over the place and thinks 'long' is enough everywhere and misuses it.

Quoted 2 lines

Your thoughts?

Your thoughts?

Thanks,

Alex

#20 lore raw

Re: [PATCH 05/12] getpwent_r.3: Cast 'uid_t' to 'intmax_t' for printf()

Hello Alex,

Quoted 4 lines · Alejandro Colomar

Hi Michael,

On 9/13/20 8:01 AM, Michael Kerrisk (man-pages) wrote:

Quoted 8 lines
Quoted 4 lines

Casting to long is the historical practice here, and should be sufficient, don't you think? I realize there's an argument for making all of these system data type casts intmax_t / uintmax_t. But the counterarguments (admittedly not compelling), are:

I was casting to (long) until I accidentally met with a cast to (intmax_t) in ftw.3 (now I realize it was the only one in all of the pages; lucke me).

Yep, that was a bit of a coincidence!

Quoted 3 lines

I saw there a good point in using those types and tried it to see what you think about them. I still haven't sent you a big patchset with s/(long)/(intmax_t)/ because I was waiting for this discussion :)

:-).

Quoted 6 lines
Quoted 1 line

* Some people might still be confined to a pre C-99 world

I guess that people are dealing with very special hardware/software and can be trusted to understand what the C99 types are and which pre-C99 do the job for them; intmax_t.3 might also help ;).

Quoted 7 lines

If someone doesn't live in such a special world and just doesn't know the types, it's a good moment to learn them.

Quoted 1 line

* Churn (lots of changes)

True ... But if changes are trivial enough, maybe a single big patch can do it.

Yes.

Quoted 9 lines
Quoted 2 lines

* (long) is a sufficient cast for all of these types (except off_t).

I don't like exceptions very much. Maybe someone sees (long) all over the place and thinks 'long' is enough everywhere and misuses it.

Quoted 1 line

Your thoughts?

Your thoughts?

Well, I said none of my arguments was too compelling... I think that I'd take one big patch. Perhaps first, could you send me an estimate of how many pages you think might be changed in the patch.

Thanks,

Michael

--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

#21 lore raw

Re: [PATCH 05/12] getpwent_r.3: Cast 'uid_t' to 'intmax_t' for printf()

On 9/13/20 2:04 PM, Michael Kerrisk (man-pages) wrote:> Well, I said none of my arguments was too compelling... I think that> I'd take one big patch. Perhaps first, could you send me an estimate> of how many pages you think might be changed in the patch.

$ grep -rn "(long)" |wc -l
56

I'd say around 20~30 pages.

Cheers

Alex

#22 lore raw

Re: [PATCH 05/12] getpwent_r.3: Cast 'uid_t' to 'intmax_t' for printf()

In reply to Michael Kerrisk (man-pages)
Quoted 3 lines · Michael Kerrisk (man-pages)

Well, I said none of my arguments was too compelling... I think that I'd take one big patch. Perhaps first, could you send me an estimate of how many pages you think might be changed in the patch.

BTW, please don't apply the patches about [u]intmax_t from this patchset:
I included them in the big patch so they are all together.

Thanks,

Alex

#23 lore raw

PATCH 6/12 rtld-audit.7: Use "%u" rather than "%d" when printing 'unsigned int' values

In reply to Alejandro Colomar
---
 man7/rtld-audit.7 | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
man7/rtld-audit.7 +3 -3
index f90731261..b1b7dfebc 100644
@@ -508,7 +508,7 @@ This is reportedly fixed in glibc 2.10.
 unsigned int
 la_version(unsigned int version)
 {
-    printf("la_version(): %d\en", version);
+    printf("la_version(): %u\en", version);
 
     return version;
 }
@@ -572,7 +572,7 @@ la_symbind32(Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
 {
     printf("la_symbind32(): symname = %s; sym\->st_value = %p\en",
             symname, sym\->st_value);
-    printf("        ndx = %d; flags = %#x", ndx, *flags);
+    printf("        ndx = %u; flags = %#x", ndx, *flags);
     printf("; refcook = %p; defcook = %p\en", refcook, defcook);
 
     return sym\->st_value;
@@ -584,7 +584,7 @@ la_symbind64(Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
 {
     printf("la_symbind64(): symname = %s; sym\->st_value = %p\en",
             symname, sym\->st_value);
-    printf("        ndx = %d; flags = %#x", ndx, *flags);
+    printf("        ndx = %u; flags = %#x", ndx, *flags);
     printf("; refcook = %p; defcook = %p\en", refcook, defcook);
 
     return sym\->st_value;
Signature · 1 line
2.28.0
#24 lore raw

Re: [PATCH 06/12] rtld-audit.7: Use "%u" rather than "%d" when printing 'unsigned int' values

Quoted 1 line · Alejandro Colomar

Thanks, Alex. Patch applied.

Cheers,

Michael

Quoted 36 lines
---
 man7/rtld-audit.7 | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
man7/rtld-audit.7 +3 -3
index f90731261..b1b7dfebc 100644
@@ -508,7 +508,7 @@ This is reportedly fixed in glibc 2.10.
 unsigned int
 la_version(unsigned int version)
 {
-    printf("la_version(): %d\en", version);
+    printf("la_version(): %u\en", version);
 
     return version;
 }
@@ -572,7 +572,7 @@ la_symbind32(Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
 {
     printf("la_symbind32(): symname = %s; sym\->st_value = %p\en",
             symname, sym\->st_value);
-    printf("        ndx = %d; flags = %#x", ndx, *flags);
+    printf("        ndx = %u; flags = %#x", ndx, *flags);
     printf("; refcook = %p; defcook = %p\en", refcook, defcook);
 
     return sym\->st_value;
@@ -584,7 +584,7 @@ la_symbind64(Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
 {
     printf("la_symbind64(): symname = %s; sym\->st_value = %p\en",
             symname, sym\->st_value);
-    printf("        ndx = %d; flags = %#x", ndx, *flags);
+    printf("        ndx = %u; flags = %#x", ndx, *flags);
     printf("; refcook = %p; defcook = %p\en", refcook, defcook);
 
     return sym\->st_value;
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#25 lore raw

PATCH 7/12 userfaultfd.2: Use 'PRIx64' rather than "%llx" when printing 64-bit fixed-width types

In reply to Alejandro Colomar
---
 man2/userfaultfd.2 | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
man2/userfaultfd.2 +4 -3
index 126d4a854..c7e42804a 100644
@@ -566,6 +566,7 @@ Read address 0x7fd30106ec0f in main(): C
    Licensed under the GNU General Public License version 2 or later.
 */
 #define _GNU_SOURCE
+#include <inttypes.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <linux/userfaultfd.h>
@@ -650,8 +651,8 @@ fault_handler_thread(void *arg)
         /* Display info about the page\-fault event */
 
         printf("    UFFD_EVENT_PAGEFAULT event: ");
-        printf("flags = %llx; ", msg.arg.pagefault.flags);
-        printf("address = %llx\en", msg.arg.pagefault.address);
+        printf("flags = %"PRIx64"; ", msg.arg.pagefault.flags);
+        printf("address = %"PRIx64"\en", msg.arg.pagefault.address);
 
         /* Copy the page pointed to by \(aqpage\(aq into the faulting
            region. Vary the contents that are copied in, so that it
@@ -673,7 +674,7 @@ fault_handler_thread(void *arg)
         if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == \-1)
             errExit("ioctl\-UFFDIO_COPY");
 
-        printf("        (uffdio_copy.copy returned %lld)\en",
+        printf("        (uffdio_copy.copy returned %"PRId64")\en",
                 uffdio_copy.copy);
     }
 }
Signature · 1 line
2.28.0
#26 lore raw

Re: [PATCH 07/12] userfaultfd.2: Use 'PRIx64' rather than "%llx" when printing 64-bit fixed-width types

Quoted 1 line · Alejandro Colomar

Thanks, Alex. Patch applied.

Cheers,

Michael

Quoted 37 lines
---
 man2/userfaultfd.2 | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
man2/userfaultfd.2 +4 -3
index 126d4a854..c7e42804a 100644
@@ -566,6 +566,7 @@ Read address 0x7fd30106ec0f in main(): C
    Licensed under the GNU General Public License version 2 or later.
 */
 #define _GNU_SOURCE
+#include <inttypes.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <linux/userfaultfd.h>
@@ -650,8 +651,8 @@ fault_handler_thread(void *arg)
         /* Display info about the page\-fault event */
 
         printf("    UFFD_EVENT_PAGEFAULT event: ");
-        printf("flags = %llx; ", msg.arg.pagefault.flags);
-        printf("address = %llx\en", msg.arg.pagefault.address);
+        printf("flags = %"PRIx64"; ", msg.arg.pagefault.flags);
+        printf("address = %"PRIx64"\en", msg.arg.pagefault.address);
 
         /* Copy the page pointed to by \(aqpage\(aq into the faulting
            region. Vary the contents that are copied in, so that it
@@ -673,7 +674,7 @@ fault_handler_thread(void *arg)
         if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == \-1)
             errExit("ioctl\-UFFDIO_COPY");
 
-        printf("        (uffdio_copy.copy returned %lld)\en",
+        printf("        (uffdio_copy.copy returned %"PRId64")\en",
                 uffdio_copy.copy);
     }
 }
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#27 lore raw

PATCH 8/12 clock_getres.2: Cast 'time_t' to 'intmax_t' for printf() and fix the length modifiers

In reply to Alejandro Colomar
---
 man2/clock_getres.2 | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
man2/clock_getres.2 +5 -2
index 8fc7c6fef..d95d88bb2 100644
@@ -468,6 +468,7 @@ CLOCK_BOOTTIME :      72691.019 (20h 11m 31s)
 #define _XOPEN_SOURCE 600
 #include <time.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <unistd.h>
@@ -491,8 +492,10 @@ displayClock(clockid_t clock, char *name, bool showRes)
     if (days > 0)
         printf("%ld days + ", days);
 
-    printf("%2ldh %2ldm %2lds", (ts.tv_sec % SECS_IN_DAY) / 3600,
-            (ts.tv_sec % 3600) / 60, ts.tv_sec % 60);
+    printf("%2jdh %2jdm %2jds",
+            (intmax_t) (ts.tv_sec % SECS_IN_DAY) / 3600,
+            (intmax_t) (ts.tv_sec % 3600) / 60,
+            (intmax_t) ts.tv_sec % 60);
     printf(")\en");
 
     if (clock_getres(clock, &ts) == \-1) {
Signature · 1 line
2.28.0
#28 lore raw

Re: [PATCH 08/12] clock_getres.2: Cast 'time_t' to 'intmax_t' for printf() and fix the length modifiers

* Alejandro Colomar <[email protected]>, 2020-09-12, 01:14:

man2/clock_getres.2 displayClock(clockid_t clock, char *name, bool showRes) · line 498

Quoted 6 lines
man2/clock_getres.2 +4 -2
-    printf("%2ldh %2ldm %2lds", (ts.tv_sec % SECS_IN_DAY) / 3600,
-            (ts.tv_sec % 3600) / 60, ts.tv_sec % 60);
+    printf("%2jdh %2jdm %2jds",
+            (intmax_t) (ts.tv_sec % SECS_IN_DAY) / 3600,
+            (intmax_t) (ts.tv_sec % 3600) / 60,
+            (intmax_t) ts.tv_sec % 60);

All these numbers are small, so %d and int casts would do the trick.

Signature · 1 line
Jakub Wilk
#29 lore raw

Re: [PATCH 08/12] clock_getres.2: Cast 'time_t' to 'intmax_t' for printf() and fix the length modifiers

Quoted 10 lines · Jakub Wilk

* Alejandro Colomar <[email protected]>, 2020-09-12, 01:14:

Quoted 6 lines

-    printf("%2ldh %2ldm %2lds", (ts.tv_sec % SECS_IN_DAY) / 3600, -            (ts.tv_sec % 3600) / 60, ts.tv_sec % 60); +    printf("%2jdh %2jdm %2jds",
+            (intmax_t) (ts.tv_sec % SECS_IN_DAY) / 3600, +            (intmax_t) (ts.tv_sec % 3600) / 60,
+            (intmax_t) ts.tv_sec % 60);

All these numbers are small, so %d and int casts would do the trick.

True. In this specific example '(int)' would be better.

Thanks,

Alex

#30 lore raw

PATCH v2 8/12 clock_getres.2: Cast 'time_t' very small,values to 'int' for printf() and fix the length modifiers

Michael, here is the improved patch with Jakub's review applied.

Jakub, I added a line acknowledging your review.

Thanks,

Alex

----------------------------------------------------
From cd0e8df57be9ebee47be5c5988a980e462c89085 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Sat, 12 Sep 2020 11:53:01 +0200
Subject: [PATCH v2 08/12] clock_getres.2: Cast 'time_t' very small values to 'int' for printf() and fix the length modifiers

---
 man2/clock_getres.2 | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
man2/clock_getres.2 +4 -2
index 8fc7c6fef..0c14203ee 100644
@@ -491,8 +491,10 @@ displayClock(clockid_t clock, char *name, bool showRes)
     if (days > 0)
         printf("%ld days + ", days);
 
-    printf("%2ldh %2ldm %2lds", (ts.tv_sec % SECS_IN_DAY) / 3600,
-            (ts.tv_sec % 3600) / 60, ts.tv_sec % 60);
+    printf("%2dh %2dm %2ds",
+            (int) (ts.tv_sec % SECS_IN_DAY) / 3600,
+            (int) (ts.tv_sec % 3600) / 60,
+            (int) ts.tv_sec % 60);
     printf(")\en");
 
     if (clock_getres(clock, &ts) == \-1) {
Signature · 1 line
2.28.0
#31 lore raw

Re: [PATCH v2 08/12] clock_getres.2: Cast 'time_t' very small,values to 'int' for printf() and fix the length modifiers

Quoted 3 lines · Alejandro Colomar

Michael, here is the improved patch with Jakub's review applied.

Jakub, I added a line acknowledging your review.

Thanks, Alex. Patch applied.

Cheers,

Michael

Quoted 31 lines

----------------------------------------------------
From cd0e8df57be9ebee47be5c5988a980e462c89085 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Sat, 12 Sep 2020 11:53:01 +0200
Subject: [PATCH v2 08/12] clock_getres.2: Cast 'time_t' very small values to 'int' for printf() and fix the length modifiers

---
  man2/clock_getres.2 | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
man2/clock_getres.2 +4 -2
index 8fc7c6fef..0c14203ee 100644
@@ -491,8 +491,10 @@ displayClock(clockid_t clock, char *name, bool showRes)
      if (days > 0)
          printf("%ld days + ", days);
 
-    printf("%2ldh %2ldm %2lds", (ts.tv_sec % SECS_IN_DAY) / 3600,
-            (ts.tv_sec % 3600) / 60, ts.tv_sec % 60);
+    printf("%2dh %2dm %2ds",
+            (int) (ts.tv_sec % SECS_IN_DAY) / 3600,
+            (int) (ts.tv_sec % 3600) / 60,
+            (int) ts.tv_sec % 60);
      printf(")\en");
 
      if (clock_getres(clock, &ts) == \-1) {
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#32 lore raw

Re: [PATCH v2 08/12] clock_getres.2: Cast 'time_t' very small,values to 'int' for printf() and fix the length modifiers

In reply to Alejandro Colomar

* Alejandro Colomar <[email protected]>, 2020-09-12, 12:06:

Quoted 1 line

Jakub, I added a line acknowledging your review.

[...]

Quoted 1 line

I don't know about linux-man policies, but for the kernel development, Reviewed-by is not just an acknowledgment. It's a strong statement that is supposed to be added by the reviewer, not by the patch author:
https://www.kernel.org/doc/html/v5.8/process/submitting-patches.html#using-reported-by-tested-by-reviewed-by-suggested-by-and-fixes

Signature · 1 line
Jakub Wilk
#33 lore raw

Re: [PATCH v2 08/12] clock_getres.2: Cast 'time_t' very small,values to 'int' for printf() and fix the length modifiers

Hi Jakub,

Quoted 11 lines · Jakub Wilk

* Alejandro Colomar <[email protected]>, 2020-09-12, 12:06:

Quoted 1 line

Jakub, I added a line acknowledging your review.

[...]

Quoted 1 line

I don't know about linux-man policies, but for the kernel development, Reviewed-by is not just an acknowledgment. It's a strong statement that is supposed to be added by the reviewer, not by the patch author:
https://www.kernel.org/doc/html/v5.8/process/submitting-patches.html#using-reported-by-tested-by-reviewed-by-suggested-by-and-fixes

Sorry, I didn't know.

Thanks,

Alex

#34 lore raw

PATCH 9/12 open_by_handle_at.2: Use "%u" rather than "%d" when printing 'unsigned int' values

In reply to Alejandro Colomar
---
 man2/open_by_handle_at.2 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
man2/open_by_handle_at.2 +1 -1
index fb8940696..53c8c7968 100644
@@ -600,7 +600,7 @@ main(int argc, char *argv[])
        for later reuse by t_open_by_handle_at.c */
 
     printf("%d\en", mount_id);
-    printf("%d %d   ", fhp\->handle_bytes, fhp\->handle_type);
+    printf("%u %d   ", fhp\->handle_bytes, fhp\->handle_type);
     for (int j = 0; j < fhp\->handle_bytes; j++)
         printf(" %02x", fhp\->f_handle[j]);
     printf("\en");
Signature · 1 line
2.28.0
#35 lore raw

Re: [PATCH 09/12] open_by_handle_at.2: Use "%u" rather than "%d" when printing 'unsigned int' values

Quoted 1 line · Alejandro Colomar

Thanks, Alex. Patch applied.

Cheers,

Michael

Quoted 18 lines
---
 man2/open_by_handle_at.2 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
man2/open_by_handle_at.2 +1 -1
index fb8940696..53c8c7968 100644
@@ -600,7 +600,7 @@ main(int argc, char *argv[])
        for later reuse by t_open_by_handle_at.c */
 
     printf("%d\en", mount_id);
-    printf("%d %d   ", fhp\->handle_bytes, fhp\->handle_type);
+    printf("%u %d   ", fhp\->handle_bytes, fhp\->handle_type);
     for (int j = 0; j < fhp\->handle_bytes; j++)
         printf(" %02x", fhp\->f_handle[j]);
     printf("\en");
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#36 lore raw

PATCH 10/12 spu_run.2: Cast 'int' to 'unsigned int' when printing with "%x"

In reply to Alejandro Colomar
---
 man2/spu_run.2 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
man2/spu_run.2 +1 -1
index b6bc2c131..ddd03ffd3 100644
@@ -254,7 +254,7 @@ int main(void)
      *   0x00000002 (spu was stopped due to stop\-and\-signal)
      * | 0x12340000 (the stop\-and\-signal code)
      */
-    printf("SPU Status: %#08x\en", spu_status);
+    printf("SPU Status: %#08x\en", (unsigned) spu_status);
 
     exit(EXIT_SUCCESS);
 }
Signature · 1 line
2.28.0
#37 lore raw

Re: [PATCH 10/12] spu_run.2: Cast 'int' to 'unsigned int' when printing with "%x"

Hi Michael,

I think this patch of mine should not be applied.

printf() will internally reinterpret the 'int' as 'unsigned int' anyway, and the behaviour is completely defined AFAIK.

Relevant standard: C18 §6.5.2.2 6

And in the case you do want this patch, I should have written 'unsigned int' in the cast, for consistency.

Thanks,

Alex

Quoted 19 lines · Alejandro Colomar
---
  man2/spu_run.2 | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
man2/spu_run.2 +1 -1
index b6bc2c131..ddd03ffd3 100644
@@ -254,7 +254,7 @@ int main(void)
       *   0x00000002 (spu was stopped due to stop\-and\-signal)
       * | 0x12340000 (the stop\-and\-signal code)
       */
-    printf("SPU Status: %#08x\en", spu_status);
+    printf("SPU Status: %#08x\en", (unsigned) spu_status);
 
      exit(EXIT_SUCCESS);
  }
#38 lore raw

Re: [PATCH 10/12] spu_run.2: Cast 'int' to 'unsigned int' when printing with "%x"

Hi Alex,

Quoted 11 lines · Alejandro Colomar

Hi Michael,

I think this patch of mine should not be applied.

printf() will internally reinterpret the 'int' as 'unsigned int' anyway, and the behaviour is completely defined AFAIK.

Relevant standard: C18 §6.5.2.2 6

And in the case you do want this patch, I should have written 'unsigned int' in the cast, for consistency.

Okay.

Thanks,

Michael

Quoted 21 lines · Alejandro Colomar
Quoted 19 lines
---
  man2/spu_run.2 | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
man2/spu_run.2 +1 -1
index b6bc2c131..ddd03ffd3 100644
@@ -254,7 +254,7 @@ int main(void)
       *   0x00000002 (spu was stopped due to stop\-and\-signal)
       * | 0x12340000 (the stop\-and\-signal code)
       */
-    printf("SPU Status: %#08x\en", spu_status);
+    printf("SPU Status: %#08x\en", (unsigned) spu_status);
 
      exit(EXIT_SUCCESS);
  }
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#39 lore raw

PATCH 11/12 mprotect.2: Use "%p" rather than casting to 'long' when printing adresses (pointer values)

In reply to Alejandro Colomar
---
 man2/mprotect.2 | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
man2/mprotect.2 +2 -3
index 47d250303..885238a8d 100644
@@ -326,8 +326,7 @@ handler(int sig, siginfo_t *si, void *unused)
        Nevertheless, we use printf() here as a simple way of
        showing that the handler was called. */
 
-    printf("Got SIGSEGV at address: %#lx\en",
-            (long) si\->si_addr);
+    printf("Got SIGSEGV at address: %p\en", si\->si_addr);
     exit(EXIT_FAILURE);
 }
 
@@ -354,7 +353,7 @@ main(int argc, char *argv[])
     if (buffer == NULL)
         handle_error("memalign");
 
-    printf("Start of region:        %#lx\en", (long) buffer);
+    printf("Start of region:        %p\en", buffer);
 
     if (mprotect(buffer + pagesize * 2, pagesize,
                 PROT_READ) == \-1)
Signature · 1 line
2.28.0
#40 lore raw

Re: [PATCH 11/12] mprotect.2: Use "%p" rather than casting to 'long' when printing adresses (pointer values)

Quoted 1 line · Alejandro Colomar

Thanks, Alex. Patch applied.

Cheers,

Michael

Quoted 28 lines
---
 man2/mprotect.2 | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
man2/mprotect.2 +2 -3
index 47d250303..885238a8d 100644
@@ -326,8 +326,7 @@ handler(int sig, siginfo_t *si, void *unused)
        Nevertheless, we use printf() here as a simple way of
        showing that the handler was called. */
 
-    printf("Got SIGSEGV at address: %#lx\en",
-            (long) si\->si_addr);
+    printf("Got SIGSEGV at address: %p\en", si\->si_addr);
     exit(EXIT_FAILURE);
 }
 
@@ -354,7 +353,7 @@ main(int argc, char *argv[])
     if (buffer == NULL)
         handle_error("memalign");
 
-    printf("Start of region:        %#lx\en", (long) buffer);
+    printf("Start of region:        %p\en", buffer);
 
     if (mprotect(buffer + pagesize * 2, pagesize,
                 PROT_READ) == \-1)
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#41 lore raw

PATCH 12/12 pidfd_open.2: Cast 'short' to 'unsigned int' when printing with "%x"

In reply to Alejandro Colomar
---
 man2/pidfd_open.2 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
man2/pidfd_open.2 +2 -1
index 0256d6d8c..c51e9fd67 100644
@@ -253,7 +253,8 @@ main(int argc, char *argv[])
         exit(EXIT_FAILURE);
     }
 
-    printf("Events (%#x): POLLIN is %sset\en", pollfd.revents,
+    printf("Events (%#x): POLLIN is %sset\en",
+            (unsigned int) pollfd.revents,
             (pollfd.revents & POLLIN) ? "" : "not ");
 
     close(pidfd);
Signature · 1 line
2.28.0
#42 lore raw

Re: [PATCH 12/12] pidfd_open.2: Cast 'short' to 'unsigned int' when printing with "%x"

Hi Alex,

Quoted 10 lines · Alejandro Colomar
---
 man2/pidfd_open.2 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
man2/pidfd_open.2 +0 -0
index 0256d6d8c..c51e9fd67 100644

man2/pidfd_open.2 main(int argc, char *argv[]) · line 260

Quoted 9 lines
man2/pidfd_open.2 +2 -1
@@ -253,7 +253,8 @@ main(int argc, char *argv[])
         exit(EXIT_FAILURE);
     }
 
-    printf("Events (%#x): POLLIN is %sset\en", pollfd.revents,
+    printf("Events (%#x): POLLIN is %sset\en",
+            (unsigned int) pollfd.revents,
             (pollfd.revents & POLLIN) ? "" : "not ");
 
     close(pidfd);

Does something similar not also apply as for patch 11, since the short will be promoted to an int in a variadic argument list?

Thanks,

Michael

Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#43 lore raw

Re: [PATCH 12/12] pidfd_open.2: Cast 'short' to 'unsigned int' when printing with "%x"

Quoted 9 lines · Michael Kerrisk (man-pages)

man2/pidfd_open.2 main(int argc, char *argv[]) · line 258

Quoted 4 lines
man2/pidfd_open.2 +2 -1
-    printf("Events (%#x): POLLIN is %sset\en", pollfd.revents,
+    printf("Events (%#x): POLLIN is %sset\en",
+            (unsigned int) pollfd.revents,
             (pollfd.revents & POLLIN) ? "" : "not ");

Does something similar not also apply as for patch 11, since the short will be promoted to an int in a variadic argument list?

Good catch :) Cancel this patch.

Thanks,

Alex