summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Paquier2023-07-31 02:36:44 +0000
committerMichael Paquier2023-07-31 02:36:44 +0000
commitf1e9f6bbfa536992eac6c094882b3afcd9e90fb4 (patch)
tree9a9396f0885689da09dfdd42a17e64c6153787e9
parentbf227926d22b5cffba4b6724df0eb239a7037cbd (diff)
Avoid memory leak in rmtree() when path cannot be opened
An allocation done for the directory names to recurse into for their deletion was done before OPENDIR(), so, assuming that a failure happens, this could leak a bit of memory. Author: Ranier Vilela Reviewed-by: Daniel Gustafsson Discussion: https://postgr.es/m/CAEudQAoN3-2ZKBALThnEk_q2hu8En5A0WG9O+5siJTQKVZzoWQ@mail.gmail.com
-rw-r--r--src/common/rmtree.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/src/common/rmtree.c b/src/common/rmtree.c
index cd99d3f471..fdc3709321 100644
--- a/src/common/rmtree.c
+++ b/src/common/rmtree.c
@@ -55,7 +55,7 @@ rmtree(const char *path, bool rmtopdir)
bool result = true;
size_t dirnames_size = 0;
size_t dirnames_capacity = 8;
- char **dirnames = palloc(sizeof(char *) * dirnames_capacity);
+ char **dirnames;
dir = OPENDIR(path);
if (dir == NULL)
@@ -64,6 +64,8 @@ rmtree(const char *path, bool rmtopdir)
return false;
}
+ dirnames = (char **) palloc(sizeof(char *) * dirnames_capacity);
+
while (errno = 0, (de = readdir(dir)))
{
if (strcmp(de->d_name, ".") == 0 ||