Skip to contentSkip to conversation

stat.2: fixed inode printing in example program

PATCH In review Konstantin Bukin 2 participants 3 messages Sep 13, 2020 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 stat.2: fixed inode printing in example program

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 long fixes the issue.

---
 man2/stat.2 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
man2/stat.2 +1 -1
index 7e5417480..89990e581 100644
@@ -681,7 +681,7 @@ main(int argc, char *argv[])
     default:       printf("unknown?\en");                break;
     }
 
-    printf("I\-node number:            %ld\en", (long) sb.st_ino);
+    printf("I\-node number:            %llu\en", sb.st_ino);
 
     printf("Mode:                     %lo (octal)\en",
             (unsigned long) sb.st_mode);
Signature · 1 line
2.17.0
#1 lore raw
Quoted 3 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 long fixes the issue.

The cast is necessary.

Relevant standard: C18 §6.5.2.2 6

Details: 'ino_t' may (and will likely) be defined as 'unsigned long'. If you try to print an 'unsigned long' with "%llu" you will:
- have Undefined Behavior (and a warning) if 'sizeof(long) < sizeof(long long)'.
- have a warning if 'sizeof(long) == sizeof(long long)'.

So if you want to use 'unsigned long long', you'll need to cast.

Cheers,

Alex

#2 lore raw

Fair enough, cast is needed. I replied in the main thread with a fixed patch.

Thank you,
Konstantin.

Quoted 20 lines · Alejandro Colomar
Quoted 4 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 long fixes the issue.

The cast is necessary.

Relevant standard: C18 §6.5.2.2 6

Details: 'ino_t' may (and will likely) be defined as 'unsigned long'. If you try to print an 'unsigned long' with "%llu" you will:
- have Undefined Behavior (and a warning) if 'sizeof(long) < sizeof(long long)'.
- have a warning if 'sizeof(long) == sizeof(long long)'.

So if you want to use 'unsigned long long', you'll need to cast.

Cheers,

Alex