From fe50406c2a7adc58d3fb7f309da5945bbb59cb40 Mon Sep 17 00:00:00 2001 From: Alex Kasko Date: Mon, 8 Apr 2024 13:02:31 +0100 Subject: [PATCH] Free buffer early when converting numerics 2x (#2471) `TdsTypeNumericToDatum` [allocates a buffer](https://github.com/babelfish-for postgresql/babelfish_extensions/blob/6606b1118978477186869c3dddd2c6d85fcc6387/contrib/babelfishpg_tds/src/backend/tds/tdstypeio.c#L1133) for parsing incoming numeric data, but does not release this buffer. So it remains in `MessageContext` until the end of BCP import call. Proposed patch frees this buffer before exiting `TdsTypeNumericToDatum`. ### Issues Resolved #2455, BABEL-4882 Signed-off-by: Alex Kasko --- contrib/babelfishpg_tds/src/backend/tds/tdstypeio.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/contrib/babelfishpg_tds/src/backend/tds/tdstypeio.c b/contrib/babelfishpg_tds/src/backend/tds/tdstypeio.c index ac4135901b..8cc5f7dab2 100644 --- a/contrib/babelfishpg_tds/src/backend/tds/tdstypeio.c +++ b/contrib/babelfishpg_tds/src/backend/tds/tdstypeio.c @@ -1069,7 +1069,8 @@ TdsTypeNumericToDatum(StringInfo buf, int scale) Numeric res; int len, sign; - char *decString; + char *decString, + *decStringOrig; int temp1, temp2; uint128 num = 0; @@ -1088,6 +1089,7 @@ TdsTypeNumericToDatum(StringInfo buf, int scale) } decString = (char *) palloc0(sizeof(char) * 40); + decStringOrig = decString; if (num != 0) Integer2String(num, decString); @@ -1118,8 +1120,10 @@ TdsTypeNumericToDatum(StringInfo buf, int scale) * index during shifting the scale part of the string. */ decString = psprintf("-%s%s.", zeros, tempString + 1); + decStringOrig = decString; len = strlen(decString) - 1; pfree(tempString); + pfree(zeros); } if (num != 0) { @@ -1146,6 +1150,7 @@ TdsTypeNumericToDatum(StringInfo buf, int scale) decString++; res = TdsSetVarFromStrWrapper(decString); + pfree(decStringOrig); PG_RETURN_NUMERIC(res); }