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>
diff --git a/src/ports/SkOSFile_win.cpp b/src/ports/SkOSFile_win.cpp
index ceafc79..9c5ada6 100644
--- a/src/ports/SkOSFile_win.cpp
+++ b/src/ports/SkOSFile_win.cpp
@@ -16,6 +16,27 @@
#include <stdio.h>
#include <sys/stat.h>
+size_t sk_fgetsize(FILE* f) {
+ int fileno = sk_fileno(f);
+ if (fileno < 0) {
+ return 0;
+ }
+
+ HANDLE file = (HANDLE)_get_osfhandle(fileno);
+ if (INVALID_HANDLE_VALUE == file) {
+ return 0;
+ }
+
+ LARGE_INTEGER fileSize;
+ if (0 == GetFileSizeEx(file, &fileSize)) {
+ return 0;
+ }
+ if (!SkTFitsIn<size_t>(fileSize.QuadPart)) {
+ return 0;
+ }
+ return static_cast<size_t>(fileSize.QuadPart);
+}
+
bool sk_exists(const char *path, SkFILE_Flags flags) {
int mode = 0; // existence
if (flags & kRead_SkFILE_Flag) {