pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: wliang@stu.xidian.edu.cn
To: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Subject: Report a potential memory leak in setup_config()
Date: Tue, 15 Feb 2022 09:49:56 +0800 (GMT+08:00)
Message-ID: <64ee7543.20d.17efb1270ca.Coremail.wliang@stu.xidian.edu.cn> (raw)

Hiall,


IfindapotentialmemoryleakinPostgresSQL14.1, whichisinthefunctionsetup_config (./src/bin/initdb/initdb.c).


Specifically, atline1095, functionpretty_wal_size() iscalled, whichallocatesachunkofmemorybyusing pg_mallocandreturnsit. However, thereturnchunkisdirectlypassedtosnprintfasits3rdparameter. Asaresult, thereisamemoryleak.


1053staticvoid
1054setup_config(void)
1055 {
...
1094     snprintf(repltok, sizeof(repltok), "min_wal_size = %s",
1095              pretty_wal_size(DEFAULT_MIN_WAL_SEGS));
...
1347 }


1036staticchar *
1037pretty_wal_size(intsegment_count)
1038 {
1039     int         sz = wal_segment_size_mb * segment_count;
1040     char       *result = pg_malloc(14);
1041 
1042     if ((sz % 1024) == 0)
1043         snprintf(result, 14, "%dGB", sz / 1024);
1044     else
1045         snprintf(result, 14, "%dMB", sz);
1046 
1047     returnresult;
1048 }


Webelievewecanfixthisproblembyaddingavariabletoreceivethereturnofpretty_wal_size() andemployingpg_free() tofreetheleakedmemory.


I'mlookingforwardtoyourconfirmation.


Best,


Wentao

view thread (15+ messages)  latest in thread

Message-ID: <64ee7543.20d.17efb1270ca.Coremail.wliang@stu.xidian.edu.cn>
Permalink:  ../64ee7543.20d.17efb1270ca.Coremail.wliang@stu.xidian.edu.cn/
Also on:    postgresql.org/message-id/64ee7543.20d.17efb1270ca.Coremail.wliang@stu.xidian.edu.cn

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: wliang@stu.xidian.edu.cn
  Subject: Re: Report a potential memory leak in setup_config()
  In-Reply-To: <64ee7543.20d.17efb1270ca.Coremail.wliang@stu.xidian.edu.cn>
  References: <64ee7543.20d.17efb1270ca.Coremail.wliang@stu.xidian.edu.cn>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox