OSDN Git Service

Fix wc -c optimization.
authorElliott Hughes <enh@google.com>
Fri, 12 Feb 2016 16:13:06 +0000 (08:13 -0800)
committerRob Landley <rob@landley.net>
Sun, 14 Feb 2016 18:36:28 +0000 (12:36 -0600)
Check the fstat(2) return value rather than read uninitialized memory if
it failed, and add a special case for files that claim to be zero-length
but aren't (as is common in /proc on Linux).

toys/posix/wc.c

index 62d1f7a..e7afc81 100644 (file)
@@ -53,8 +53,8 @@ static void do_wc(int fd, char *name)
   if (toys.optflags == FLAG_c) {
     struct stat st;
 
-    fstat(fd, &st);
-    if (S_ISREG(st.st_mode)) {
+    // On Linux, files in /proc often report their size as 0.
+    if (!fstat(fd, &st) && S_ISREG(st.st_mode) && st.st_size > 0) {
       lengths[2] = st.st_size;
       goto show;
     }