Skip to content

Commit

Permalink
Fix realloc misusages
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Jan 17, 2014
1 parent 606b70e commit db3948e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
19 changes: 15 additions & 4 deletions src/sdl-instead/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ int games_replace(const char *path, const char *dir)
int rc;
char *p;
struct game *g;
struct game *new_games;
if (!is_game(path, dir))
return -1;
g = game_lookup(dir);
Expand All @@ -291,9 +292,10 @@ int games_replace(const char *path, const char *dir)
games_sort();
return 0;
}
games = realloc(games, sizeof(struct game) * (1 + games_nr));
if (!games)
new_games = realloc(games, sizeof(struct game) * (1 + games_nr));
if (!new_games)
return -1;
games = new_games;
rc = games_add(path, dir);
if (!rc)
games_sort();
Expand All @@ -305,6 +307,7 @@ int games_lookup(const char *path)
int n = 0, i = 0;
DIR *d;
struct dirent *de;
struct game *new_games;

if (!path)
return 0;
Expand All @@ -326,7 +329,12 @@ int games_lookup(const char *path)
rewinddir(d);
if (!n)
goto out;
games = realloc(games, sizeof(struct game) * (n + games_nr));
new_games = realloc(games, sizeof(struct game) * (n + games_nr));
if (!new_games) {
closedir(d);
return -1;
}
games = new_games;
while ((de = readdir(d)) && i < n) {
/*if (de->d_type != DT_DIR)
continue;*/
Expand All @@ -346,11 +354,14 @@ int games_lookup(const char *path)
int games_remove(int gtr)
{
int rc;
struct game *new_games;
rc = remove_dir(games[gtr].path);
free(games[gtr].name); free(games[gtr].dir); free(games[gtr].path);
games_nr --;
memmove(&games[gtr], &games[gtr + 1], (games_nr - gtr) * sizeof(struct game));
games = realloc(games, games_nr * sizeof(struct game));
new_games = realloc(games, games_nr * sizeof(struct game));
if (new_games) /* failure to shrink otherwise, and it's non-fatal */
games = new_games;
return rc;
}

Expand Down
18 changes: 14 additions & 4 deletions src/sdl-instead/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,7 @@ static SDL_Rect **SDL_ListModes(const SDL_PixelFormat * format, Uint32 flags)
SDL_DisplayMode mode;
int i, nmodes;
SDL_Rect **modes;
SDL_Rect **new_modes;
Uint32 Rmask, Gmask, Bmask, Amask;
int bpp;

Expand All @@ -1387,10 +1388,11 @@ static SDL_Rect **SDL_ListModes(const SDL_PixelFormat * format, Uint32 flags)
&& modes[nmodes - 1]->h == mode.h) {
continue;
}
modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
if (!modes) {
new_modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
if (!new_modes) {
return NULL;
}
modes = new_modes;
modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
if (!modes[nmodes]) {
return NULL;
Expand Down Expand Up @@ -2657,11 +2659,19 @@ struct xref *xref_new(char *link)
return p;
}

void xref_add_word(struct xref *xref, struct word *word)
int xref_add_word(struct xref *xref, struct word *word)
{
xref->words = realloc(xref->words, (xref->num + 1) * sizeof(struct word*));
struct word **new_words;

new_words = realloc(xref->words, (xref->num + 1) * sizeof(struct word*));
if (!new_words)
return -1;

xref->words = new_words;
xref->words[xref->num ++] = word;
word->xref = xref;

return 0;
}

void xref_free(struct xref *xref)
Expand Down
8 changes: 7 additions & 1 deletion src/sdl-instead/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ int menu_langs_lookup(const char *path)
int n = 0, i = 0;
DIR *d;
struct dirent *de;
struct lang *new_langs;

if (!path)
return 0;
Expand All @@ -877,7 +878,12 @@ int menu_langs_lookup(const char *path)
if (!n)
goto out;

langs = realloc(langs, sizeof(struct lang) * (n + langs_nr));
new_langs = realloc(langs, sizeof(struct lang) * (n + langs_nr));
if (!new_langs) {
closedir(d);
return -1;
}
langs = new_langs;

while ((de = readdir(d)) && i < n) {
if (!is_lang(path, de->d_name))
Expand Down
8 changes: 7 additions & 1 deletion src/sdl-instead/themes.c
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ int themes_lookup(const char *path)
int n = 0, i = 0;
DIR *d;
struct dirent *de;
struct theme *new_themes;

if (!path)
return 0;
Expand All @@ -949,7 +950,12 @@ int themes_lookup(const char *path)
rewinddir(d);
if (!n)
goto out;
themes = realloc(themes, sizeof(struct theme) * (n + themes_nr));
new_themes = realloc(themes, sizeof(struct theme) * (n + themes_nr));
if (!new_themes) {
closedir(d);
return -1;
}
themes = new_themes;
while ((de = readdir(d)) && i < n) {
/*if (de->d_type != DT_DIR)
continue;*/
Expand Down

0 comments on commit db3948e

Please sign in to comment.