c - Using fseek and ftell to determine the size of a file has a vulnerability? -
i've read posts show how use fseek , ftell determine size of file.
file *fp; long file_size; char *buffer; fp = fopen("foo.bin", "r"); if (null == fp) { /* handle error */ } if (fseek(fp, 0 , seek_end) != 0) { /* handle error */ } file_size = ftell(fp); buffer = (char*)malloc(file_size); if (null == buffer){ /* handle error */ }
i use technique ran link describes potential vulnerability.
the link recommends using fstat instead. can comment on this?
the link 1 of many nonsensical pieces of c coding advice cert. justification based on liberties c standard allows implementation take, not allowed posix , irrelevant in cases have fstat
alternative.
posix requires:
that
"b"
modifierfopen
have no effect, i.e. text , binary mode behave identically. means concern invoking ub on text files nonsense.that files have byte-resolution size set write operations , truncate operations. means concern random numbers of null bytes @ end of file nonsense.
sadly nonsense publish, it's hard know cert publications take seriously. shame, because lots of them serious.
Comments
Post a Comment