Subject: Re: bug#18621: [BUG] wc -c incorrectly counts bytes in /sys

From: Paul Eggert <[email-masked]>
Date: Fri, 03 Oct 2014 11:47:49 -0700

On 10/03/2014 11:26 AM, Jim Meyering wrote:
> That looks like a fine fix.

Unfortunately that fix would make 'wc -c' waaaaay slower for a file that 
consists entirely of a big hole.

How about if we change usable_st_size to return false for these proc 
files, with a heuristic as tight as we can make it, and to have 
coreutils check usable_st_size in more places.  Something like this, 
perhaps:

   /* Return a boolean indicating whether SB->st_size is correct. */
   static inline bool
   usable_st_size (struct stat const *sb)
   {
     if (S_ISREG (sb->st_mode))
       {
         /* proc files like /sys/kernel/vmcoreinfo are weird: their
            st_size values do not reflect what's actually in them.
            The following heuristic attempts to catch proc files without
            catching many regular files that just happen to have the same
            signature.  */
         return ! (sb->st_uid == 0 && sb->st_gid == 0 && sb->st_blocks == 0
                   && sb->st_size == ST_BLKSIZE (*sb));
       }
     return (S_ISLNK (sb->st_mode) || S_TYPEISSHM (sb) || S_TYPEISTMO (sb));
   }

and then review every place where coreutils currently uses st_size and 
prepend a check for usable_st_size if needed.