Skip to content

Commit db3948e

Browse files
committed
Fix realloc misusages
1 parent 606b70e commit db3948e

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

src/sdl-instead/game.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ int games_replace(const char *path, const char *dir)
272272
int rc;
273273
char *p;
274274
struct game *g;
275+
struct game *new_games;
275276
if (!is_game(path, dir))
276277
return -1;
277278
g = game_lookup(dir);
@@ -291,9 +292,10 @@ int games_replace(const char *path, const char *dir)
291292
games_sort();
292293
return 0;
293294
}
294-
games = realloc(games, sizeof(struct game) * (1 + games_nr));
295-
if (!games)
295+
new_games = realloc(games, sizeof(struct game) * (1 + games_nr));
296+
if (!new_games)
296297
return -1;
298+
games = new_games;
297299
rc = games_add(path, dir);
298300
if (!rc)
299301
games_sort();
@@ -305,6 +307,7 @@ int games_lookup(const char *path)
305307
int n = 0, i = 0;
306308
DIR *d;
307309
struct dirent *de;
310+
struct game *new_games;
308311

309312
if (!path)
310313
return 0;
@@ -326,7 +329,12 @@ int games_lookup(const char *path)
326329
rewinddir(d);
327330
if (!n)
328331
goto out;
329-
games = realloc(games, sizeof(struct game) * (n + games_nr));
332+
new_games = realloc(games, sizeof(struct game) * (n + games_nr));
333+
if (!new_games) {
334+
closedir(d);
335+
return -1;
336+
}
337+
games = new_games;
330338
while ((de = readdir(d)) && i < n) {
331339
/*if (de->d_type != DT_DIR)
332340
continue;*/
@@ -346,11 +354,14 @@ int games_lookup(const char *path)
346354
int games_remove(int gtr)
347355
{
348356
int rc;
357+
struct game *new_games;
349358
rc = remove_dir(games[gtr].path);
350359
free(games[gtr].name); free(games[gtr].dir); free(games[gtr].path);
351360
games_nr --;
352361
memmove(&games[gtr], &games[gtr + 1], (games_nr - gtr) * sizeof(struct game));
353-
games = realloc(games, games_nr * sizeof(struct game));
362+
new_games = realloc(games, games_nr * sizeof(struct game));
363+
if (new_games) /* failure to shrink otherwise, and it's non-fatal */
364+
games = new_games;
354365
return rc;
355366
}
356367

src/sdl-instead/graphics.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,7 @@ static SDL_Rect **SDL_ListModes(const SDL_PixelFormat * format, Uint32 flags)
13611361
SDL_DisplayMode mode;
13621362
int i, nmodes;
13631363
SDL_Rect **modes;
1364+
SDL_Rect **new_modes;
13641365
Uint32 Rmask, Gmask, Bmask, Amask;
13651366
int bpp;
13661367

@@ -1387,10 +1388,11 @@ static SDL_Rect **SDL_ListModes(const SDL_PixelFormat * format, Uint32 flags)
13871388
&& modes[nmodes - 1]->h == mode.h) {
13881389
continue;
13891390
}
1390-
modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
1391-
if (!modes) {
1391+
new_modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
1392+
if (!new_modes) {
13921393
return NULL;
13931394
}
1395+
modes = new_modes;
13941396
modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
13951397
if (!modes[nmodes]) {
13961398
return NULL;
@@ -2657,11 +2659,19 @@ struct xref *xref_new(char *link)
26572659
return p;
26582660
}
26592661

2660-
void xref_add_word(struct xref *xref, struct word *word)
2662+
int xref_add_word(struct xref *xref, struct word *word)
26612663
{
2662-
xref->words = realloc(xref->words, (xref->num + 1) * sizeof(struct word*));
2664+
struct word **new_words;
2665+
2666+
new_words = realloc(xref->words, (xref->num + 1) * sizeof(struct word*));
2667+
if (!new_words)
2668+
return -1;
2669+
2670+
xref->words = new_words;
26632671
xref->words[xref->num ++] = word;
26642672
word->xref = xref;
2673+
2674+
return 0;
26652675
}
26662676

26672677
void xref_free(struct xref *xref)

src/sdl-instead/menu.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ int menu_langs_lookup(const char *path)
860860
int n = 0, i = 0;
861861
DIR *d;
862862
struct dirent *de;
863+
struct lang *new_langs;
863864

864865
if (!path)
865866
return 0;
@@ -877,7 +878,12 @@ int menu_langs_lookup(const char *path)
877878
if (!n)
878879
goto out;
879880

880-
langs = realloc(langs, sizeof(struct lang) * (n + langs_nr));
881+
new_langs = realloc(langs, sizeof(struct lang) * (n + langs_nr));
882+
if (!new_langs) {
883+
closedir(d);
884+
return -1;
885+
}
886+
langs = new_langs;
881887

882888
while ((de = readdir(d)) && i < n) {
883889
if (!is_lang(path, de->d_name))

src/sdl-instead/themes.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,7 @@ int themes_lookup(const char *path)
931931
int n = 0, i = 0;
932932
DIR *d;
933933
struct dirent *de;
934+
struct theme *new_themes;
934935

935936
if (!path)
936937
return 0;
@@ -949,7 +950,12 @@ int themes_lookup(const char *path)
949950
rewinddir(d);
950951
if (!n)
951952
goto out;
952-
themes = realloc(themes, sizeof(struct theme) * (n + themes_nr));
953+
new_themes = realloc(themes, sizeof(struct theme) * (n + themes_nr));
954+
if (!new_themes) {
955+
closedir(d);
956+
return -1;
957+
}
958+
themes = new_themes;
953959
while ((de = readdir(d)) && i < n) {
954960
/*if (de->d_type != DT_DIR)
955961
continue;*/

0 commit comments

Comments
 (0)