Report a potential memory leak in setup_config()

From: wliang(at)stu(dot)xidian(dot)edu(dot)cn
To: pgsql-bugs <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Report a potential memory leak in setup_config()
Date: 2022-02-15 01:49:56
Message-ID: 64ee7543.20d.17efb1270ca.Coremail.wliang@stu.xidian.edu.cn
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message wliang 2022-02-15 02:05:04 Report a potential memory leak in describeOneTableDetails()
Previous Message Tom Lane 2022-02-15 01:22:34 Re: BUG #17391: While using --with-ssl=openssl and PG_TEST_EXTRA='ssl' options, SSL tests fail on OpenBSD 7.0