mirror of
https://github.com/morgan9e/FreeRDP
synced 2026-04-15 00:44:19 +09:00
[winpr,stream] improve return checks in Stream_EnsureCapacity
* Properly check all return values and propagate them back to caller * Simplify new stream size calculation
This commit is contained in:
@@ -44,43 +44,37 @@
|
||||
BOOL Stream_EnsureCapacity(wStream* s, size_t size)
|
||||
{
|
||||
WINPR_ASSERT(s);
|
||||
if (s->capacity < size)
|
||||
if (s->capacity >= size)
|
||||
return TRUE;
|
||||
|
||||
const size_t increment = 128ull;
|
||||
const size_t old_capacity = s->capacity;
|
||||
const size_t new_capacity = size + increment - size % increment;
|
||||
const size_t position = Stream_GetPosition(s);
|
||||
|
||||
BYTE* new_buf = NULL;
|
||||
if (!s->isOwner)
|
||||
{
|
||||
BYTE* new_buf = NULL;
|
||||
|
||||
size_t old_capacity = s->capacity;
|
||||
size_t new_capacity = old_capacity;
|
||||
|
||||
do
|
||||
{
|
||||
if (new_capacity > SIZE_MAX - 128ull)
|
||||
return FALSE;
|
||||
new_capacity += 128ull;
|
||||
} while (new_capacity <= size);
|
||||
|
||||
const size_t position = Stream_GetPosition(s);
|
||||
|
||||
if (!s->isOwner)
|
||||
{
|
||||
new_buf = (BYTE*)malloc(new_capacity);
|
||||
CopyMemory(new_buf, s->buffer, s->capacity);
|
||||
s->isOwner = TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
new_buf = (BYTE*)realloc(s->buffer, new_capacity);
|
||||
}
|
||||
|
||||
new_buf = (BYTE*)malloc(new_capacity);
|
||||
if (!new_buf)
|
||||
return FALSE;
|
||||
s->buffer = new_buf;
|
||||
s->capacity = new_capacity;
|
||||
s->length = new_capacity;
|
||||
ZeroMemory(&s->buffer[old_capacity], s->capacity - old_capacity);
|
||||
|
||||
Stream_SetPosition(s, position);
|
||||
CopyMemory(new_buf, s->buffer, s->capacity);
|
||||
s->isOwner = TRUE;
|
||||
}
|
||||
return TRUE;
|
||||
else
|
||||
{
|
||||
new_buf = (BYTE*)realloc(s->buffer, new_capacity);
|
||||
if (!new_buf)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
s->buffer = new_buf;
|
||||
s->capacity = new_capacity;
|
||||
s->length = new_capacity;
|
||||
ZeroMemory(&s->buffer[old_capacity], s->capacity - old_capacity);
|
||||
|
||||
return Stream_SetPosition(s, position);
|
||||
}
|
||||
|
||||
BOOL Stream_EnsureRemainingCapacity(wStream* s, size_t size)
|
||||
|
||||
Reference in New Issue
Block a user