Skip to content
This repository has been archived by the owner on Feb 20, 2021. It is now read-only.

Commit

Permalink
Fix AV in AllocHeapBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
orangemocha committed Feb 2, 2015
1 parent 7796c03 commit 497d561
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Win32_Interop/Win32_QFork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <Psapi.h>
#include <ShlObj.h>
#include <Shlwapi.h>
#include <assert.h>

#define QFORK_MAIN_IMPL
#include "Win32_QFork.h"
Expand Down Expand Up @@ -1170,16 +1171,26 @@ LPVOID AllocHeapBlock(size_t size, BOOL allocateHigh) {
}
int contiguousBlocksToAllocate = (int)(size / g_pQForkControl->heapBlockSize);

if (contiguousBlocksToAllocate > g_pQForkControl->availableBlocksInHeap) {
errno = ENOMEM;
return retPtr;
}

size_t mapped = 0;
int startIndex = allocateHigh ? g_pQForkControl->availableBlocksInHeap - 1 : contiguousBlocksToAllocate - 1;
int endIndex = allocateHigh ? -1 : g_pQForkControl->availableBlocksInHeap - contiguousBlocksToAllocate + 1;
int startIndex = allocateHigh ? g_pQForkControl->availableBlocksInHeap - 1 : 0;
int endIndex = allocateHigh ?
contiguousBlocksToAllocate - 2 :
g_pQForkControl->availableBlocksInHeap - contiguousBlocksToAllocate + 1;
int direction = allocateHigh ? -1 : 1;
int blockIndex = 0;
int contiguousBlocksFound = 0;
for(blockIndex = startIndex;
blockIndex != endIndex;
blockIndex += direction) {
for (int n = 0; n < contiguousBlocksToAllocate; n++) {
assert((blockIndex + n * direction >= 0) &&
(blockIndex + n * direction < g_pQForkControl->availableBlocksInHeap));

if (g_pQForkControl->heapBlockMap[blockIndex + n * direction] == BlockState::bsUNMAPPED) {
contiguousBlocksFound++;
}
Expand Down

0 comments on commit 497d561

Please sign in to comment.