Skip to content

Commit 9fee4ae

Browse files
committed
Fixed #5645: realloc return handling
1 parent a301e08 commit 9fee4ae

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

Diff for: client/X11/generate_argument_docbook.c

+27-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
LPSTR tr_esc_str(LPCSTR arg, bool format)
1010
{
1111
LPSTR tmp = NULL;
12+
LPSTR tmp2 = NULL;
1213
size_t cs = 0, x, ds, len;
1314
size_t s;
1415

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

2728
if (s)
28-
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
29+
{
30+
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
31+
if (!tmp2)
32+
free(tmp);
33+
tmp = tmp2;
34+
}
2935

3036
if (NULL == tmp)
3137
{
@@ -43,7 +49,10 @@ LPSTR tr_esc_str(LPCSTR arg, bool format)
4349
case '<':
4450
len = format ? 13 : 4;
4551
ds += len - 1;
46-
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
52+
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
53+
if (!tmp2)
54+
free(tmp);
55+
tmp = tmp2;
4756

4857
if (NULL == tmp)
4958
{
@@ -64,7 +73,10 @@ LPSTR tr_esc_str(LPCSTR arg, bool format)
6473
case '>':
6574
len = format ? 14 : 4;
6675
ds += len - 1;
67-
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
76+
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
77+
if (!tmp2)
78+
free(tmp);
79+
tmp = tmp2;
6880

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

8597
case '\'':
8698
ds += 5;
87-
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
99+
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
100+
if (!tmp2)
101+
free(tmp);
102+
tmp = tmp2;
88103

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

103118
case '"':
104119
ds += 5;
105-
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
120+
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
121+
if (!tmp2)
122+
free(tmp);
123+
tmp = tmp2;
106124

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

121139
case '&':
122140
ds += 4;
123-
tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
141+
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
142+
if (!tmp2)
143+
free(tmp);
144+
tmp = tmp2;
124145

125146
if (NULL == tmp)
126147
{

Diff for: libfreerdp/codec/region.c

+14-3
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,12 @@ static BOOL region16_simplify_bands(REGION16* region)
467467

468468
if (finalNbRects != nbRects)
469469
{
470-
int allocSize = sizeof(REGION16_DATA) + (finalNbRects * sizeof(RECTANGLE_16));
471-
region->data = realloc(region->data, allocSize);
470+
REGION16_DATA* data;
471+
size_t allocSize = sizeof(REGION16_DATA) + (finalNbRects * sizeof(RECTANGLE_16));
472+
data = realloc(region->data, allocSize);
473+
if (!data)
474+
free(region->data);
475+
region->data = data;
472476

473477
if (!region->data)
474478
{
@@ -485,10 +489,12 @@ static BOOL region16_simplify_bands(REGION16* region)
485489

486490
BOOL region16_union_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect)
487491
{
492+
REGION16_DATA* data;
488493
const RECTANGLE_16* srcExtents;
489494
RECTANGLE_16* dstExtents;
490495
const RECTANGLE_16* currentBand, *endSrcRect, *nextBand;
491496
REGION16_DATA* newItems = NULL;
497+
REGION16_DATA* tmpItems = NULL;
492498
RECTANGLE_16* dstRect = NULL;
493499
UINT32 usedRects, srcNbRects;
494500
UINT16 topInterBand;
@@ -673,7 +679,11 @@ BOOL region16_union_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16*
673679
dstExtents->bottom = MAX(rect->bottom, srcExtents->bottom);
674680
dstExtents->right = MAX(rect->right, srcExtents->right);
675681
newItems->size = sizeof(REGION16_DATA) + (usedRects * sizeof(RECTANGLE_16));
676-
dst->data = realloc(newItems, newItems->size);
682+
tmpItems = realloc(newItems, newItems->size);
683+
if (!tmpItems)
684+
free(newItems);
685+
newItems = tmpItems;
686+
dst->data = newItems;
677687

678688
if (!dst->data)
679689
{
@@ -717,6 +727,7 @@ BOOL region16_intersects_rect(const REGION16* src, const RECTANGLE_16* arg2)
717727

718728
BOOL region16_intersect_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect)
719729
{
730+
REGION16_DATA* data;
720731
REGION16_DATA* newItems;
721732
const RECTANGLE_16* srcPtr, *endPtr, *srcExtents;
722733
RECTANGLE_16* dstPtr;

Diff for: winpr/libwinpr/utils/lodepng/lodepng.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,15 @@ unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequen
841841
static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigned* frequencies,
842842
size_t mincodes, size_t numcodes, unsigned maxbitlen)
843843
{
844+
unsigned* lengths;
844845
unsigned error = 0;
845846
while(!frequencies[numcodes - 1] && numcodes > mincodes) numcodes--; /*trim zeroes*/
846847
tree->maxbitlen = maxbitlen;
847848
tree->numcodes = (unsigned)numcodes; /*number of symbols*/
848-
tree->lengths = (unsigned*)realloc(tree->lengths, numcodes * sizeof(unsigned));
849+
lengths = (unsigned*)realloc(tree->lengths, numcodes * sizeof(unsigned));
850+
if (!lengths)
851+
free(tree->lengths);
852+
tree->lengths = lengths;
849853
if(!tree->lengths) return 83; /*alloc fail*/
850854
/*initialize all lengths to 0*/
851855
memset(tree->lengths, 0, numcodes * sizeof(unsigned));

0 commit comments

Comments
 (0)