aboutsummaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-10-21 18:36:59 -0400
committerDavid Robillard <d@drobilla.net>2023-10-21 18:36:59 -0400
commitd8b116a95176ee130dbd4baabe4b6bd03d51bce3 (patch)
tree28146bbca86cd26f71c23b11f6439061b951e26b /src/common.c
parent49cfbf63374ec015e81c667c7e93e2f049a0ec1c (diff)
downloadpugl-d8b116a95176ee130dbd4baabe4b6bd03d51bce3.tar.gz
pugl-d8b116a95176ee130dbd4baabe4b6bd03d51bce3.tar.bz2
pugl-d8b116a95176ee130dbd4baabe4b6bd03d51bce3.zip
Fix potential memory leaks due to realloc() failure
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/common.c b/src/common.c
index 6f1e017..46b2f3d 100644
--- a/src/common.c
+++ b/src/common.c
@@ -146,12 +146,20 @@ puglNewView(PuglWorld* const world)
puglSetDefaultHints(view->hints);
- // Add to world view list
- ++world->numViews;
- world->views =
- (PuglView**)realloc(world->views, world->numViews * sizeof(PuglView*));
+ // Enlarge world view list
+ const size_t newNumViews = world->numViews + 1U;
+ PuglView** const views =
+ (PuglView**)realloc(world->views, newNumViews * sizeof(PuglView*));
+
+ if (!views) {
+ free(view);
+ return NULL;
+ }
- world->views[world->numViews - 1] = view;
+ // Add to world view list
+ world->views = views;
+ world->views[world->numViews] = view;
+ world->numViews = newNumViews;
return view;
}