Fix tiny memory leaks
authorPeter Eisentraut <[email protected]>
Sat, 1 Oct 2022 10:48:24 +0000 (12:48 +0200)
committerPeter Eisentraut <[email protected]>
Sat, 1 Oct 2022 10:48:24 +0000 (12:48 +0200)
Both check_application_name() and check_cluster_name() use
pg_clean_ascii() but didn't release the memory.  Depending on when the
GUC is set, this might be cleaned up at some later time or it would
leak postmaster memory once.  In any case, it seems better not to have
to rely on such analysis and make the code locally robust.  Also, this
makes Valgrind happier.

Author: Masahiko Sawada <[email protected]>
Reviewed-by: Jacob Champion <[email protected]>
Discussion: https://www.postgresql.org/message-id/CAD21AoBmFNy9MPfA0UUbMubQqH3AaK5U3mrv6pSeWrwCk3LJ8g@mail.gmail.com

src/backend/commands/variable.c

index c795cb7a29ceb01cf23fe72faa5e5de003f9745d..e555fb3150195f30f49e6c2618972ba9adadd129 100644 (file)
@@ -1025,17 +1025,22 @@ bool
 check_application_name(char **newval, void **extra, GucSource source)
 {
    char       *clean;
+   char       *ret;
 
    /* Only allow clean ASCII chars in the application name */
    clean = pg_clean_ascii(*newval, MCXT_ALLOC_NO_OOM);
    if (!clean)
        return false;
 
-   clean = guc_strdup(WARNING, clean);
-   if (!clean)
+   ret = guc_strdup(WARNING, clean);
+   if (!ret)
+   {
+       pfree(clean);
        return false;
+   }
 
-   *newval = clean;
+   pfree(clean);
+   *newval = ret;
    return true;
 }
 
@@ -1056,17 +1061,22 @@ bool
 check_cluster_name(char **newval, void **extra, GucSource source)
 {
    char       *clean;
+   char       *ret;
 
    /* Only allow clean ASCII chars in the cluster name */
    clean = pg_clean_ascii(*newval, MCXT_ALLOC_NO_OOM);
    if (!clean)
        return false;
 
-   clean = guc_strdup(WARNING, clean);
-   if (!clean)
+   ret = guc_strdup(WARNING, clean);
+   if (!ret)
+   {
+       pfree(clean);
        return false;
+   }
 
-   *newval = clean;
+   pfree(clean);
+   *newval = ret;
    return true;
 }