Using fseek and ftell to determine the size of a file has a vulnerability?

后端 未结 5 1548
天涯浪人
天涯浪人 2020-12-11 16:01

I\'ve read posts that show how to use fseek and ftell to determine the size of a file.

FILE *fp;
long file_size;
char *buffer;

fp = fopen(\"foo.bin\", \"r\         


        
5条回答
  •  时光取名叫无心
    2020-12-11 16:18

    The link is one of the many nonsensical pieces of C coding advice from CERT. Their justification is based on liberties the C standard allows an implementation to take, but which are not allowed by POSIX and thus irrelevant in all cases where you have fstat as an alternative.

    POSIX requires:

    1. that the "b" modifier for fopen have no effect, i.e. that text and binary mode behave identically. This means their concern about invoking UB on text files is nonsense.

    2. that files have a byte-resolution size set by write operations and truncate operations. This means their concern about random numbers of null bytes at the end of the file is nonsense.

    Sadly with all the nonsense like this they publish, it's hard to know which CERT publications to take seriously. Which is a shame, because lots of them are serious.

提交回复
热议问题