Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free buffer early when converting numerics #2456

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion contrib/babelfishpg_tds/src/backend/tds/tdstypeio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,8 @@ TdsTypeNumericToDatum(StringInfo buf, int scale)
Numeric res;
int len,
sign;
char *decString;
char *decString,
*decStringOrig;
int temp1,
temp2;
uint128 num = 0;
Expand All @@ -1131,6 +1132,7 @@ TdsTypeNumericToDatum(StringInfo buf, int scale)
}

decString = (char *) palloc0(sizeof(char) * 40);
decStringOrig = decString;

if (num != 0)
Integer2String(num, decString);
Expand Down Expand Up @@ -1161,8 +1163,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)
{
Expand All @@ -1189,6 +1193,7 @@ TdsTypeNumericToDatum(StringInfo buf, int scale)
decString++;

res = TdsSetVarFromStrWrapper(decString);
pfree(decStringOrig);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I couldn't add comment above, but we should free memory allocated to zeros on line no. 1153
  2. Just to safer side and to avoid double free, use if (decStringOrig) then free

Copy link
Contributor

@KushaalShroff KushaalShroff Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no chance that decStringOrig is going to be NULL here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added memory free call to zeros buffer. Agree about the NULL checks before calling pfree, just in this case decStringOrig should not be NULL on any code path.

PG_RETURN_NUMERIC(res);
}

Expand Down
Loading