fix memory leak in Java_java_io_File_lastModified (exposed in debug mode)

This commit is contained in:
Joshua Warner 2013-10-17 13:54:59 -06:00
parent 526e3a41a8
commit b3d4f33522

View File

@ -546,22 +546,22 @@ Java_java_io_File_lastModified(JNIEnv* e, jclass, jstring path)
{ {
string_t chars = getChars(e, path); string_t chars = getChars(e, path);
if (chars) { if (chars) {
#ifdef PLATFORM_WINDOWS #ifdef PLATFORM_WINDOWS
// Option: without opening file // Option: without opening file
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364946(v=vs.85).aspx // http://msdn.microsoft.com/en-us/library/windows/desktop/aa364946(v=vs.85).aspx
#if !defined(WINAPI_FAMILY) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) # if !defined(WINAPI_FAMILY) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
HANDLE hFile = CreateFileW HANDLE hFile = CreateFileW
(chars, FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); (chars, FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
#else # else
HANDLE hFile = CreateFile2 HANDLE hFile = CreateFile2
(chars, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr); (chars, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
#endif # endif
releaseChars(e, path, chars); releaseChars(e, path, chars);
if (hFile == INVALID_HANDLE_VALUE) if (hFile == INVALID_HANDLE_VALUE)
return 0; return 0;
LARGE_INTEGER fileDate, filetimeToUnixEpochAdjustment; LARGE_INTEGER fileDate, filetimeToUnixEpochAdjustment;
filetimeToUnixEpochAdjustment.QuadPart = 11644473600000L * 10000L; filetimeToUnixEpochAdjustment.QuadPart = 11644473600000L * 10000L;
#if !defined(WINAPI_FAMILY) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) # if !defined(WINAPI_FAMILY) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
FILETIME fileLastWriteTime; FILETIME fileLastWriteTime;
if (!GetFileTime(hFile, 0, 0, &fileLastWriteTime)) if (!GetFileTime(hFile, 0, 0, &fileLastWriteTime))
{ {
@ -570,7 +570,7 @@ Java_java_io_File_lastModified(JNIEnv* e, jclass, jstring path)
} }
fileDate.HighPart = fileLastWriteTime.dwHighDateTime; fileDate.HighPart = fileLastWriteTime.dwHighDateTime;
fileDate.LowPart = fileLastWriteTime.dwLowDateTime; fileDate.LowPart = fileLastWriteTime.dwLowDateTime;
#else # else
FILE_BASIC_INFO fileInfo; FILE_BASIC_INFO fileInfo;
if (!GetFileInformationByHandleEx(hFile, FileBasicInfo, &fileInfo, sizeof(fileInfo))) if (!GetFileInformationByHandleEx(hFile, FileBasicInfo, &fileInfo, sizeof(fileInfo)))
{ {
@ -578,20 +578,22 @@ Java_java_io_File_lastModified(JNIEnv* e, jclass, jstring path)
return 0; return 0;
} }
fileDate = fileInfo.ChangeTime; fileDate = fileInfo.ChangeTime;
#endif # endif
CloseHandle(hFile); CloseHandle(hFile);
fileDate.QuadPart -= filetimeToUnixEpochAdjustment.QuadPart; fileDate.QuadPart -= filetimeToUnixEpochAdjustment.QuadPart;
return fileDate.QuadPart / 10000000L; return fileDate.QuadPart / 10000000L;
#else #else
struct stat fileStat; struct stat fileStat;
if (stat(chars, &fileStat) == -1) { int res = stat(chars, &fileStat);
releaseChars(e, path, chars); releaseChars(e, path, chars);
if (res == -1) {
return 0; return 0;
} }
return (static_cast<jlong>(fileStat.st_mtim.tv_sec) * 1000) + return (static_cast<jlong>(fileStat.st_mtim.tv_sec) * 1000) +
(static_cast<jlong>(fileStat.st_mtim.tv_nsec) / (1000*1000)); (static_cast<jlong>(fileStat.st_mtim.tv_nsec) / (1000*1000));
#endif #endif
} }
return 0; return 0;