1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

Avoid memory leak on error while parsing pg_stat_statements dump file

By using palloc() instead of raw malloc().

Reported-by: Gaurav Singh <gaurav.singh@yugabyte.com>
Reviewed-by: Lukas Fittl <lukas@fittl.com>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Discussion: https://www.postgresql.org/message-id/CAEcQ1bYR9s4eQLFDjzzJHU8fj-MTbmRpW-9J-r2gsCn+HEsynw@mail.gmail.com
Backpatch-through: 14
This commit is contained in:
Heikki Linnakangas
2026-03-27 12:20:38 +02:00
parent bf7ecf3531
commit a6d03067ff

View File

@@ -780,7 +780,7 @@ pgss_shmem_shutdown(int code, Datum arg)
if (fwrite(&pgss->stats, sizeof(pgssGlobalStats), 1, file) != 1)
goto error;
free(qbuffer);
pfree(qbuffer);
qbuffer = NULL;
if (FreeFile(file))
@@ -805,7 +805,7 @@ error:
errmsg("could not write file \"%s\": %m",
PGSS_DUMP_FILE ".tmp")));
if (qbuffer)
free(qbuffer);
pfree(qbuffer);
if (file)
FreeFile(file);
unlink(PGSS_DUMP_FILE ".tmp");
@@ -1650,7 +1650,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
pgss->gc_count != gc_count)
{
if (qbuffer)
free(qbuffer);
pfree(qbuffer);
qbuffer = qtext_load_file(&qbuffer_size);
}
}
@@ -1819,7 +1819,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
LWLockRelease(pgss->lock);
if (qbuffer)
free(qbuffer);
pfree(qbuffer);
tuplestore_donestoring(tupstore);
}
@@ -2129,7 +2129,7 @@ error:
}
/*
* Read the external query text file into a malloc'd buffer.
* Read the external query text file into a palloc'd buffer.
*
* Returns NULL (without throwing an error) if unable to read, eg
* file not there or insufficient memory.
@@ -2171,7 +2171,7 @@ qtext_load_file(Size *buffer_size)
/* Allocate buffer; beware that off_t might be wider than size_t */
if (stat.st_size <= MaxAllocHugeSize)
buf = (char *) malloc(stat.st_size);
buf = (char *) palloc_extended(stat.st_size, MCXT_ALLOC_HUGE | MCXT_ALLOC_NO_OOM);
else
buf = NULL;
if (buf == NULL)
@@ -2210,7 +2210,7 @@ qtext_load_file(Size *buffer_size)
(errcode_for_file_access(),
errmsg("could not read file \"%s\": %m",
PGSS_TEXT_FILE)));
free(buf);
pfree(buf);
CloseTransientFile(fd);
return NULL;
}
@@ -2425,7 +2425,7 @@ gc_qtexts(void)
else
pgss->mean_query_len = ASSUMED_LENGTH_INIT;
free(qbuffer);
pfree(qbuffer);
/*
* OK, count a garbage collection cycle. (Note: even though we have
@@ -2443,7 +2443,7 @@ gc_fail:
if (qfile)
FreeFile(qfile);
if (qbuffer)
free(qbuffer);
pfree(qbuffer);
/*
* Since the contents of the external file are now uncertain, mark all