mirror of
https://github.com/morgan9e/FreeRDP
synced 2026-04-15 00:44:19 +09:00
[winpr,collections] fix Queue and MessageQueue
EnsureCapacity was nulling the wrong elements, this commit rectifies that.
This commit is contained in:
@@ -135,18 +135,24 @@ static BOOL MessageQueue_EnsureCapacity(wMessageQueue* queue, size_t count)
|
|||||||
size_t slots = new_capacity - old_capacity;
|
size_t slots = new_capacity - old_capacity;
|
||||||
const size_t batch = (tocopy < slots) ? tocopy : slots;
|
const size_t batch = (tocopy < slots) ? tocopy : slots;
|
||||||
CopyMemory(&(queue->array[old_capacity]), queue->array, batch * sizeof(wMessage));
|
CopyMemory(&(queue->array[old_capacity]), queue->array, batch * sizeof(wMessage));
|
||||||
ZeroMemory(queue->array, batch * sizeof(wMessage));
|
|
||||||
|
|
||||||
/* Tail is decremented. if the whole thing is appended
|
/* Tail is decremented. if the whole thing is appended
|
||||||
* just move the existing tail by old_capacity */
|
* just move the existing tail by old_capacity */
|
||||||
if (tocopy < slots)
|
if (tocopy < slots)
|
||||||
|
{
|
||||||
|
ZeroMemory(queue->array, batch * sizeof(wMessage));
|
||||||
queue->tail += old_capacity;
|
queue->tail += old_capacity;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const size_t movesize = (queue->tail - batch) * sizeof(wMessage);
|
const size_t remain = queue->tail - batch;
|
||||||
|
const size_t movesize = remain * sizeof(wMessage);
|
||||||
memmove_s(queue->array, queue->tail * sizeof(wMessage), &queue->array[batch],
|
memmove_s(queue->array, queue->tail * sizeof(wMessage), &queue->array[batch],
|
||||||
movesize);
|
movesize);
|
||||||
ZeroMemory(&queue->array[batch], movesize);
|
|
||||||
|
const size_t zerooffset = remain;
|
||||||
|
const size_t zerosize = (queue->tail - remain) * sizeof(wMessage);
|
||||||
|
ZeroMemory(&queue->array[zerooffset], zerosize);
|
||||||
queue->tail -= batch;
|
queue->tail -= batch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -188,18 +188,23 @@ BOOL Queue_Contains(wQueue* queue, const void* obj)
|
|||||||
|
|
||||||
static BOOL Queue_EnsureCapacity(wQueue* queue, size_t count)
|
static BOOL Queue_EnsureCapacity(wQueue* queue, size_t count)
|
||||||
{
|
{
|
||||||
|
const size_t blocksize = 32ull;
|
||||||
WINPR_ASSERT(queue);
|
WINPR_ASSERT(queue);
|
||||||
|
|
||||||
if (queue->size + count > queue->capacity)
|
if (queue->growthFactor > SIZE_MAX / blocksize)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
const size_t increment = blocksize * queue->growthFactor;
|
||||||
|
if (queue->size > SIZE_MAX - count)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
const size_t required = queue->size + count;
|
||||||
|
if (required > queue->capacity)
|
||||||
{
|
{
|
||||||
if (queue->growthFactor > SIZE_MAX / 32ull)
|
const size_t old_capacity = queue->capacity;
|
||||||
return FALSE;
|
if (required > SIZE_MAX - increment)
|
||||||
if (queue->size > SIZE_MAX - count)
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
const size_t increment = 32ull * queue->growthFactor;
|
|
||||||
const size_t old_capacity = queue->capacity;
|
|
||||||
const size_t required = queue->size + count;
|
|
||||||
const size_t new_capacity = required + increment - required % increment;
|
const size_t new_capacity = required + increment - required % increment;
|
||||||
if (new_capacity > SIZE_MAX / sizeof(BYTE*))
|
if (new_capacity > SIZE_MAX / sizeof(BYTE*))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
@@ -222,18 +227,24 @@ static BOOL Queue_EnsureCapacity(wQueue* queue, size_t count)
|
|||||||
const size_t batch = (tocopy < slots) ? tocopy : slots;
|
const size_t batch = (tocopy < slots) ? tocopy : slots;
|
||||||
|
|
||||||
CopyMemory(&(queue->array[old_capacity]), queue->array, batch * sizeof(uintptr_t));
|
CopyMemory(&(queue->array[old_capacity]), queue->array, batch * sizeof(uintptr_t));
|
||||||
ZeroMemory(queue->array, batch * sizeof(uintptr_t));
|
|
||||||
|
|
||||||
/* Tail is decremented. if the whole thing is appended
|
/* Tail is decremented. if the whole thing is appended
|
||||||
* just move the existing tail by old_capacity */
|
* just move the existing tail by old_capacity */
|
||||||
if (tocopy < slots)
|
if (tocopy < slots)
|
||||||
|
{
|
||||||
|
ZeroMemory(queue->array, batch * sizeof(uintptr_t));
|
||||||
queue->tail += old_capacity;
|
queue->tail += old_capacity;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const size_t movesize = (queue->tail - batch) * sizeof(uintptr_t);
|
const size_t remain = queue->tail - batch;
|
||||||
|
const size_t movesize = remain * sizeof(uintptr_t);
|
||||||
memmove_s(queue->array, queue->tail * sizeof(uintptr_t), &queue->array[batch],
|
memmove_s(queue->array, queue->tail * sizeof(uintptr_t), &queue->array[batch],
|
||||||
movesize);
|
movesize);
|
||||||
ZeroMemory(&queue->array[batch], movesize);
|
|
||||||
|
const size_t zerooffset = remain;
|
||||||
|
const size_t zerosize = (queue->tail - remain) * sizeof(uintptr_t);
|
||||||
|
ZeroMemory(&queue->array[zerooffset], zerosize);
|
||||||
queue->tail -= batch;
|
queue->tail -= batch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user