From ae2875e2481a088ff66e0677ba03b4fde9a519c4 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Sat, 13 Oct 2018 19:07:45 +0300 Subject: [PATCH] mdbx: avoid on-stack allocation/chkstk inside mdbx_check4nonlocal(). 6 of 17 for https://github.com/leo-yuriev/libmdbx/issues/43 Change-Id: Ib55a27c4859ff25e5b779a4514cd2e625ab99013 --- src/osal.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/osal.c b/src/osal.c index 5d626385..7306c1d0 100644 --- a/src/osal.c +++ b/src/osal.c @@ -817,26 +817,40 @@ int mdbx_check4nonlocal(mdbx_filehandle_t handle, int flags) { } if (mdbx_GetVolumeInformationByHandleW && mdbx_GetFinalPathNameByHandleW) { - WCHAR PathBuffer[INT16_MAX]; + WCHAR *PathBuffer = mdbx_malloc(sizeof(WCHAR) * INT16_MAX); + if (!PathBuffer) + return MDBX_ENOMEM; + + int rc = MDBX_SUCCESS; DWORD VolumeSerialNumber, FileSystemFlags; if (!mdbx_GetVolumeInformationByHandleW(handle, PathBuffer, INT16_MAX, &VolumeSerialNumber, NULL, - &FileSystemFlags, NULL, 0)) - return GetLastError(); + &FileSystemFlags, NULL, 0)) { + rc = GetLastError(); + goto bailout; + } if ((flags & MDBX_RDONLY) == 0) { - if (FileSystemFlags & (FILE_SEQUENTIAL_WRITE_ONCE | - FILE_READ_ONLY_VOLUME | FILE_VOLUME_IS_COMPRESSED)) - return ERROR_REMOTE_STORAGE_MEDIA_ERROR; + if (FileSystemFlags & + (FILE_SEQUENTIAL_WRITE_ONCE | FILE_READ_ONLY_VOLUME | + FILE_VOLUME_IS_COMPRESSED)) { + rc = ERROR_REMOTE_STORAGE_MEDIA_ERROR; + goto bailout; + } } if (!mdbx_GetFinalPathNameByHandleW(handle, PathBuffer, INT16_MAX, - FILE_NAME_NORMALIZED | VOLUME_NAME_NT)) - return GetLastError(); + FILE_NAME_NORMALIZED | + VOLUME_NAME_NT)) { + rc = GetLastError(); + goto bailout; + } if (_wcsnicmp(PathBuffer, L"\\Device\\Mup\\", 12) == 0) { - if (!(flags & MDBX_EXCLUSIVE)) - return ERROR_REMOTE_STORAGE_MEDIA_ERROR; + if (!(flags & MDBX_EXCLUSIVE)) { + rc = ERROR_REMOTE_STORAGE_MEDIA_ERROR; + goto bailout; + } } else if (mdbx_GetFinalPathNameByHandleW(handle, PathBuffer, INT16_MAX, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS)) { @@ -857,7 +871,7 @@ int mdbx_check4nonlocal(mdbx_filehandle_t handle, int flags) { case DRIVE_REMOTE: default: if (!(flags & MDBX_EXCLUSIVE)) - return ERROR_REMOTE_STORAGE_MEDIA_ERROR; + rc = ERROR_REMOTE_STORAGE_MEDIA_ERROR; // fall through case DRIVE_REMOVABLE: case DRIVE_FIXED: @@ -865,6 +879,9 @@ int mdbx_check4nonlocal(mdbx_filehandle_t handle, int flags) { break; } } + bailout: + mdbx_free(PathBuffer); + return rc; } #else (void)handle;