mirror of
https://github.com/corda/corda.git
synced 2025-01-03 19:54:13 +00:00
Fix for File.length() integer overflow on 32-bit Windows.
The File.length() method was returning a signed 32-bit value on 32-bit Windows systems. This was causing an integer overflow on file sizes greater than 2 GB. This appears to be caused by the way Windows handles the STAT() function. This patch checks whether the current platform is Windows then uses the Windows API to get the correct file size and return it as a jlong.
This commit is contained in:
parent
be01e5b687
commit
446c09dd33
@ -342,15 +342,32 @@ Java_java_io_File_toAbsolutePath(JNIEnv* e UNUSED, jclass, jstring path)
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_java_io_File_length(JNIEnv* e, jclass, jstring path)
|
||||
{
|
||||
string_t chars = getChars(e, path);
|
||||
if (chars) {
|
||||
STRUCT_STAT s;
|
||||
int r = STAT(chars, &s);
|
||||
releaseChars(e, path, chars);
|
||||
if (r == 0) {
|
||||
return s.st_size;
|
||||
|
||||
#ifdef PLATFORM_WINDOWS
|
||||
|
||||
LARGE_INTEGER fileSize;
|
||||
HANDLE file = CreateFileW((wchar_t *)e->GetStringChars(path, 0), FILE_READ_DATA, FILE_SHARE_READ, 0,
|
||||
OPEN_EXISTING, 0, 0);
|
||||
e->ReleaseStringChars(path, 0);
|
||||
if (file != INVALID_HANDLE_VALUE)
|
||||
GetFileSizeEx(file, &fileSize);
|
||||
else return -1;
|
||||
CloseHandle(file);
|
||||
return (jlong)fileSize.QuadPart;
|
||||
|
||||
#else
|
||||
|
||||
string_t chars = getChars(e, path);
|
||||
if (chars) {
|
||||
STRUCT_STAT s;
|
||||
int r = STAT(chars, &s);
|
||||
releaseChars(e, path, chars);
|
||||
if (r == 0) {
|
||||
return s.st_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user