@@ -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)
346354int 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
0 commit comments