Skip to content

Commit

Permalink
Fixed FreeRDP#5645: realloc return handling
Browse files Browse the repository at this point in the history
  • Loading branch information
akallabeth committed Oct 4, 2019
1 parent a301e08 commit fc80ab4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
33 changes: 27 additions & 6 deletions client/X11/generate_argument_docbook.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
LPSTR tr_esc_str(LPCSTR arg, bool format)
{
LPSTR tmp = NULL;
LPSTR tmp2 = NULL;
size_t cs = 0, x, ds, len;
size_t s;

Expand All @@ -25,7 +26,12 @@ LPSTR tr_esc_str(LPCSTR arg, bool format)
ds = s + 1;

if (s)
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
{
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
if (!tmp2)
free(tmp);
tmp = tmp2;
}

if (NULL == tmp)
{
Expand All @@ -43,7 +49,10 @@ LPSTR tr_esc_str(LPCSTR arg, bool format)
case '<':
len = format ? 13 : 4;
ds += len - 1;
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
if (!tmp2)
free(tmp);
tmp = tmp2;

if (NULL == tmp)
{
Expand All @@ -64,7 +73,10 @@ LPSTR tr_esc_str(LPCSTR arg, bool format)
case '>':
len = format ? 14 : 4;
ds += len - 1;
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
if (!tmp2)
free(tmp);
tmp = tmp2;

if (NULL == tmp)
{
Expand All @@ -84,7 +96,10 @@ LPSTR tr_esc_str(LPCSTR arg, bool format)

case '\'':
ds += 5;
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
if (!tmp2)
free(tmp);
tmp = tmp2;

if (NULL == tmp)
{
Expand All @@ -102,7 +117,10 @@ LPSTR tr_esc_str(LPCSTR arg, bool format)

case '"':
ds += 5;
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
if (!tmp2)
free(tmp);
tmp = tmp2;

if (NULL == tmp)
{
Expand All @@ -120,7 +138,10 @@ LPSTR tr_esc_str(LPCSTR arg, bool format)

case '&':
ds += 4;
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
if (!tmp2)
free(tmp);
tmp = tmp2;

if (NULL == tmp)
{
Expand Down
20 changes: 16 additions & 4 deletions libfreerdp/codec/region.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,12 @@ static BOOL region16_simplify_bands(REGION16* region)

if (finalNbRects != nbRects)
{
int allocSize = sizeof(REGION16_DATA) + (finalNbRects * sizeof(RECTANGLE_16));
region->data = realloc(region->data, allocSize);
REGION16_DATA* data;
size_t allocSize = sizeof(REGION16_DATA) + (finalNbRects * sizeof(RECTANGLE_16));
data = realloc(region->data, allocSize);
if (!data)
free(region->data);
region->data = data;

if (!region->data)
{
Expand All @@ -485,6 +489,7 @@ static BOOL region16_simplify_bands(REGION16* region)

BOOL region16_union_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect)
{
REGION16_DATA* data;
const RECTANGLE_16* srcExtents;
RECTANGLE_16* dstExtents;
const RECTANGLE_16* currentBand, *endSrcRect, *nextBand;
Expand Down Expand Up @@ -673,7 +678,10 @@ BOOL region16_union_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16*
dstExtents->bottom = MAX(rect->bottom, srcExtents->bottom);
dstExtents->right = MAX(rect->right, srcExtents->right);
newItems->size = sizeof(REGION16_DATA) + (usedRects * sizeof(RECTANGLE_16));
dst->data = realloc(newItems, newItems->size);
data = realloc(newItems, newItems->size);
if (!data)
free(dst->data);
dst->data = data;

if (!dst->data)
{
Expand Down Expand Up @@ -717,6 +725,7 @@ BOOL region16_intersects_rect(const REGION16* src, const RECTANGLE_16* arg2)

BOOL region16_intersect_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect)
{
REGION16_DATA* data;
REGION16_DATA* newItems;
const RECTANGLE_16* srcPtr, *endPtr, *srcExtents;
RECTANGLE_16* dstPtr;
Expand Down Expand Up @@ -789,7 +798,10 @@ BOOL region16_intersect_rect(REGION16* dst, const REGION16* src, const RECTANGLE
if ((dst->data->size > 0) && (dst->data != &empty_region))
free(dst->data);

dst->data = realloc(newItems, newItems->size);
data = realloc(newItems, newItems->size);
if (!data)
free(dst->data);
dst->data = data;

if (!dst->data)
{
Expand Down
6 changes: 5 additions & 1 deletion winpr/libwinpr/utils/lodepng/lodepng.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,11 +841,15 @@ unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequen
static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigned* frequencies,
size_t mincodes, size_t numcodes, unsigned maxbitlen)
{
unsigned* lengths;
unsigned error = 0;
while(!frequencies[numcodes - 1] && numcodes > mincodes) numcodes--; /*trim zeroes*/
tree->maxbitlen = maxbitlen;
tree->numcodes = (unsigned)numcodes; /*number of symbols*/
tree->lengths = (unsigned*)realloc(tree->lengths, numcodes * sizeof(unsigned));
lengths = (unsigned*)realloc(tree->lengths, numcodes * sizeof(unsigned));
if (!lengths)
free(tree->lengths);
tree->lengths = lengths;
if(!tree->lengths) return 83; /*alloc fail*/
/*initialize all lengths to 0*/
memset(tree->lengths, 0, numcodes * sizeof(unsigned));
Expand Down

0 comments on commit fc80ab4

Please sign in to comment.