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

results 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.

Reported-by: Konstantin Bukin <kbukin@gmail.com>
Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Alejandro Colomar 2020-09-23 16:54:26 +02:00 committed by Michael Kerrisk
parent ab7a387479
commit a39e673a89
2 changed files with 8 additions and 8 deletions

View File

@ -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);
}

View File

@ -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);