On 08/05/2016 11:43 AM, Ralf Ramsauer wrote: >>>> >>> Being too smart about such things is - in general - not a good idea. It >>>> >>> is usually better to try whatever you want to do and handle errors. >>>> >>> Size > 0 checks will fail in procfs and readable size == stat.size will >>>> >>> fail in sysfs. Similar things can be true for permission checks and >>> >> Current code is not suitable for procfs, as malloc() is aligned to >>> >> st_size. sysfs will return PAGE_SIZE as an upper boundary. Patch 4/7 >>> >> fixes this issue by allowing read() to return smaller values as st_size. >>>> >>> funny filesystems like fuse or cifs. >>> >> Didn't experience such behaviour yet - guess you did :) >>>> >>> >>>>> >>>> buffer = malloc(stat.st_size); >>> >> Ok, so if we don't check and st_size is zero, we're calling malloc(0) >>> >> and according to the malloc manpage, it might return NULL, and the >>> >> produced error message "insufficient memory" is not the proper message. >> > Allocating a reasonable initial amount then realloc() as you read the >> > file should work. > Hm, results in looped read(). I'll think about that. Thinking about that: Wouldn't it be nicer to drop the struct stat and not respect st_size at all?
Like Valentine mentioned before, preallocation a reasonable amount of memory (1024, 4096, PAGE_SIZE?) and reallocating it should be fine and this would work in all three cases: regular files, procfs and sysfs. No need to fail on files with size 0. Ralf > > Ralf -- Ralf Ramsauer PGP: 0x8F10049B -- You received this message because you are subscribed to the Google Groups "Jailhouse" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
