Skip to content

Commit 5624d64

Browse files
harshdubey166Harsh Dubey
authored andcommitted
Fixing memory leaks observed in ASan (babelfish-for-postgresql#4690)
This commit fixes memory leaks observed when testing with ASan 1. In the function TdsSendRowDescription() , strdup() allocates memory using the system's malloc(), which is completely outside PostgreSQL's memory management. This means the string stored in relMetaDataInfo->partName[1] lives in malloc-heap, not in any PostgreSQL memory context. By using pstrdup() , it allocates using palloc() inside the current memory context (MessageContext), so it's properly tracked and will be freed when that context is destroyed. 2. Similarly in the function rewrite_if_condition(), strdup uses malloc which is invisible to PostgreSQL's memory management while pstrdup allocates in the current memory context, which gets cleaned up automatically. Task: BABEL-6422 Authored by : harshdu@amazon.com
1 parent 5a8349b commit 5624d64

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

contrib/babelfishpg_tds/src/backend/tds/tdsresponse.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,8 +1636,13 @@ PrepareRowDescription(TupleDesc typeinfo, PlannedStmt *plannedstmt, List *target
16361636
* schema name only assuming its shared schema.
16371637
*/
16381638
if (relMetaDataInfo->partName[1] == NULL)
1639-
relMetaDataInfo->partName[1] = strdup(physical_schema_name);
1640-
1639+
{
1640+
if (physical_schema_name != NULL)
1641+
relMetaDataInfo->partName[1] = pstrdup(physical_schema_name);
1642+
else
1643+
relMetaDataInfo->partName[1] = NULL;
1644+
}
1645+
16411646
if (physical_schema_name)
16421647
pfree(physical_schema_name);
16431648

contrib/babelfishpg_tsql/src/tsqlIface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2999,7 +2999,7 @@ class tsqlBuilder : public tsqlCommonMutator
29992999
clear_rewritten_query_fragment();
30003000

30013001
/* Now we can prepend SELECT to rewritten search_condition */
3002-
expr->query = strdup((std::string("SELECT ") + std::string(expr->query)).c_str());
3002+
expr->query = pstrdup((std::string("SELECT ") + std::string(expr->query)).c_str());
30033003
return expr;
30043004
}
30053005

0 commit comments

Comments
 (0)