From a7ded33103cd9fadccf7328b3a36442646514a58 Mon Sep 17 00:00:00 2001 From: Enrico Giordani Date: Tue, 21 Jun 2016 13:53:33 +0200 Subject: [PATCH] [Fix] Possible AV during background save. This fix is a refinement of a previous fix to avoid a possible AV if the buffer to write to disk ends exactly at the last byte of a memory page. --- src/Win32_Interop/Win32_Common.cpp | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Win32_Interop/Win32_Common.cpp b/src/Win32_Interop/Win32_Common.cpp index 2fbce821..0a2127de 100644 --- a/src/Win32_Interop/Win32_Common.cpp +++ b/src/Win32_Interop/Win32_Common.cpp @@ -29,20 +29,13 @@ namespace Globals /* This function is used to force the VEH on the entire size of the buffer length, * in the event that the buffer crosses the memory page boundaries */ void EnsureMemoryIsMapped(const void *buffer, size_t size) { - // Use 'volatile' to make sure the compiler doesn't remove "c = *((char*) (p + offset));" - volatile char c; - char* p = (char*) buffer; - char* pStart = p - ((size_t) p % Globals::pageSize); - char* pEnd = p + size; - if ((size_t) (pEnd - pStart) > Globals::pageSize) { - size_t offset = 0; - while (offset < size) { - offset += Globals::pageSize; - if (offset > size) { - offset = size; - } - c = *((char*) (p + offset)); - } + char* pFirstByte = (char*) buffer; + char* pLastByte = (char*) buffer + size - 1; + char* pFirstPage = pFirstByte - ((size_t) pFirstByte % Globals::pageSize); + char* pLastPage = pLastByte - ((size_t) pLastByte % Globals::pageSize); + // Use 'volatile' to make sure the compiler doesn't remove the memory access + for (volatile char* p = pFirstPage; p <= pLastPage; p += Globals::pageSize) { + volatile char c = *p; } } -- GitLab