Skip to contentSkip to conversation

ioctl_ns.2, stat.2: Fix signedness of printf specifiers

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ioctl_ns.2, stat.2: Fix signedness of printf specifiers

These variables are either of an unsigned integer type per POSIX; or of an integer type per POSIX, that Linux defines as an unsigned integer type.

Print them with 'uintmax_t' instead of 'intmax_t' to avoid big positive numbers being printed as negative numbers.

Bug report:
From: Konstantin Bukin @ 2020-09-13 15:04 UTC
To: mtk.manpages; +Cc: Konstantin Bukin, linux-man

inode numbers are expected to be positive. Casting them to a signed type may result in printing negative values. E.g. running example program on the following file:

$ ls -li test.txt
9280843260537405888 -r--r--r-- 1 kbukin hardware 300 Jul 21 06:36 test.txt

resutls in the following output:

$ ./example test.txt
ID of containing device:  [0,480]
File type:                regular file
I-node number:            -9165900813172145728
Mode:                     100444 (octal)
Link count:               1
Ownership:                UID=2743   GID=30
Preferred I/O block size: 32768 bytes
File size:                300 bytes
Blocks allocated:         8
Last status change:       Tue Jul 21 06:36:50 2020
Last file access:         Sat Sep 12 14:13:38 2020
Last file modification:   Tue Jul 21 06:36:50 2020

Such erroneous reporting happens for inode values greater than maximum value which can be stored in signed long. Casting does not seem to be necessary here. Printing inode as unsigned long fixes the issue.

---

Hi Michael,

I wrote a patch similar to Konstantin's patch, but using uintmax_t as we discussed.
I further fixed a few more cases of incorrect casts. I found all of those related to struct stat's members, but there may be more incorrect casts out there.

Cheers,

Alex

 man2/ioctl_ns.2 | 8 ++++----
 man2/stat.2     | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
man2/ioctl_ns.2 +4 -4
index b83db74e0..87b5168a7 100644
@@ -317,10 +317,10 @@ main(int argc, char *argv[])
             exit(EXIT_FAILURE);
         }
         printf("Device/Inode of owning user namespace is: "
-                "[%jx,%jx] / %jd\en",
+                "[%jx,%jx] / %ju\en",
                 (uintmax_t) major(sb.st_dev),
                 (uintmax_t) minor(sb.st_dev),
-                (intmax_t) sb.st_ino);
+                (uintmax_t) sb.st_ino);
 
         close(userns_fd);
     }
@@ -347,10 +347,10 @@ main(int argc, char *argv[])
             perror("fstat\-parentns");
             exit(EXIT_FAILURE);
         }
-        printf("Device/Inode of parent namespace is: [%jx,%jx] / %jd\en",
+        printf("Device/Inode of parent namespace is: [%jx,%jx] / %ju\en",
                 (uintmax_t) major(sb.st_dev),
                 (uintmax_t) minor(sb.st_dev),
-                (intmax_t) sb.st_ino);
+                (uintmax_t) sb.st_ino);
 
         close(parent_fd);
     }
man2/stat.2 +4 -4
index f71cc3831..13a1f37f7 100644
@@ -682,14 +682,14 @@ main(int argc, char *argv[])
     default:       printf("unknown?\en");                break;
     }
 
-    printf("I\-node number:            %jd\en", (intmax_t) sb.st_ino);
+    printf("I\-node number:            %ju\en", (uintmax_t) sb.st_ino);
 
     printf("Mode:                     %jo (octal)\en",
             (uintmax_t) sb.st_mode);
 
-    printf("Link count:               %jd\en", (intmax_t) sb.st_nlink);
-    printf("Ownership:                UID=%jd   GID=%jd\en",
-            (intmax_t) sb.st_uid, (intmax_t) sb.st_gid);
+    printf("Link count:               %ju\en", (uintmax_t) sb.st_nlink);
+    printf("Ownership:                UID=%ju   GID=%ju\en",
+            (uintmax_t) sb.st_uid, (uintmax_t) sb.st_gid);
 
     printf("Preferred I/O block size: %jd bytes\en",
             (intmax_t) sb.st_blksize);
Signature · 1 line
2.28.0
#1 lore raw

Hi Alex,

Quoted 33 lines · Alejandro Colomar

These variables are either of an unsigned integer type per POSIX; or of an integer type per POSIX, that Linux defines as an unsigned integer type.

Print them with 'uintmax_t' instead of 'intmax_t' to avoid big positive numbers being printed as negative numbers.

Bug report:
From: Konstantin Bukin @ 2020-09-13 15:04 UTC
To: mtk.manpages; +Cc: Konstantin Bukin, linux-man

inode numbers are expected to be positive. Casting them to a signed type may result in printing negative values. E.g. running example program on the following file:

$ ls -li test.txt
9280843260537405888 -r--r--r-- 1 kbukin hardware 300 Jul 21 06:36 test.txt

resutls in the following output:

$ ./example test.txt
ID of containing device:  [0,480]
File type:                regular file
I-node number:            -9165900813172145728
Mode:                     100444 (octal)
Link count:               1
Ownership:                UID=2743   GID=30
Preferred I/O block size: 32768 bytes
File size:                300 bytes
Blocks allocated:         8
Last status change:       Tue Jul 21 06:36:50 2020
Last file access:         Sat Sep 12 14:13:38 2020
Last file modification:   Tue Jul 21 06:36:50 2020
Quoted 6 lines

Such erroneous reporting happens for inode values greater than maximum value which can be stored in signed long. Casting does not seem to be necessary here. Printing inode as unsigned long fixes the issue.

Thanks! Patch applied.

Cheers,

Michael

Quoted 72 lines

---

Hi Michael,

I wrote a patch similar to Konstantin's patch, but using uintmax_t as we discussed.
I further fixed a few more cases of incorrect casts. I found all of those related to struct stat's members, but there may be more incorrect casts out there.

Cheers,

Alex

 man2/ioctl_ns.2 | 8 ++++----
 man2/stat.2     | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
man2/ioctl_ns.2 +4 -4
index b83db74e0..87b5168a7 100644
@@ -317,10 +317,10 @@ main(int argc, char *argv[])
             exit(EXIT_FAILURE);
         }
         printf("Device/Inode of owning user namespace is: "
-                "[%jx,%jx] / %jd\en",
+                "[%jx,%jx] / %ju\en",
                 (uintmax_t) major(sb.st_dev),
                 (uintmax_t) minor(sb.st_dev),
-                (intmax_t) sb.st_ino);
+                (uintmax_t) sb.st_ino);
 
         close(userns_fd);
     }
@@ -347,10 +347,10 @@ main(int argc, char *argv[])
             perror("fstat\-parentns");
             exit(EXIT_FAILURE);
         }
-        printf("Device/Inode of parent namespace is: [%jx,%jx] / %jd\en",
+        printf("Device/Inode of parent namespace is: [%jx,%jx] / %ju\en",
                 (uintmax_t) major(sb.st_dev),
                 (uintmax_t) minor(sb.st_dev),
-                (intmax_t) sb.st_ino);
+                (uintmax_t) sb.st_ino);
 
         close(parent_fd);
     }
man2/stat.2 +4 -4
index f71cc3831..13a1f37f7 100644
@@ -682,14 +682,14 @@ main(int argc, char *argv[])
     default:       printf("unknown?\en");                break;
     }
 
-    printf("I\-node number:            %jd\en", (intmax_t) sb.st_ino);
+    printf("I\-node number:            %ju\en", (uintmax_t) sb.st_ino);
 
     printf("Mode:                     %jo (octal)\en",
             (uintmax_t) sb.st_mode);
 
-    printf("Link count:               %jd\en", (intmax_t) sb.st_nlink);
-    printf("Ownership:                UID=%jd   GID=%jd\en",
-            (intmax_t) sb.st_uid, (intmax_t) sb.st_gid);
+    printf("Link count:               %ju\en", (uintmax_t) sb.st_nlink);
+    printf("Ownership:                UID=%ju   GID=%ju\en",
+            (uintmax_t) sb.st_uid, (uintmax_t) sb.st_gid);
 
     printf("Preferred I/O block size: %jd bytes\en",
             (intmax_t) sb.st_blksize);
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/