diff options
author | Ben Wagner <bungeman@google.com> | 2017-03-17 13:49:31 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-03-17 20:08:52 +0000 |
commit | eefa289a214ea3917e5e00554f231adb5ad6f712 (patch) | |
tree | db2e0da8a27d30931cbe5c7aaddb559c805b0a1e /src/ports/SkOSFile_posix.cpp | |
parent | 7da8d646a2114b252e79a0c93a4aeb153d2e1390 (diff) |
sk_fgetsize to not use ftell.
The previous version of sk_fgetsize used ftell and fseek to compute
the size of a file. There are so many issues with this that it is called
out by securecoding.cert.org as FIO19-C as a thing not to do. We already
have correct code for computing the size of a file in the mmap code, so
use that instead.
Change-Id: I1d771124989d0ec1523f6d858814ee563263213a
Reviewed-on: https://skia-review.googlesource.com/9860
Reviewed-by: Leon Scroggins <scroggo@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
Diffstat (limited to 'src/ports/SkOSFile_posix.cpp')
-rw-r--r-- | src/ports/SkOSFile_posix.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/ports/SkOSFile_posix.cpp b/src/ports/SkOSFile_posix.cpp index 48b5b95ad3..40028246ff 100644 --- a/src/ports/SkOSFile_posix.cpp +++ b/src/ports/SkOSFile_posix.cpp @@ -19,6 +19,25 @@ #include <sys/types.h> #include <unistd.h> +size_t sk_fgetsize(FILE* f) { + int fd = fileno(f); + if (fd < 0) { + return 0; + } + + struct stat status; + if (0 != fstat(fd, &status)) { + return 0; + } + if (!S_ISREG(status.st_mode)) { + return 0; + } + if (!SkTFitsIn<size_t>(status.st_size)) { + return 0; + } + return static_cast<size_t>(status.st_size); +} + bool sk_exists(const char *path, SkFILE_Flags flags) { int mode = F_OK; if (flags & kRead_SkFILE_Flag) { |