Skip to content

Commit 3dd084c

Browse files
committed
Fix volatile-safety issue in pltcl_SPI_execute_plan().
The "callargs" variable is modified within PG_TRY and then referenced within PG_CATCH, which is exactly the coding pattern we've now found to be unsafe. Marking "callargs" volatile would be problematic because it is passed by reference to some Tcl functions, so fix the problem by not modifying it within PG_TRY. We can just postpone the free() till we exit the PG_TRY construct, as is already done elsewhere in this same file. Also, fix failure to free(callargs) when exiting on too-many-arguments error. This is only a minor memory leak, but a leak nonetheless. In passing, remove some unnecessary "volatile" markings in the same function. Those doubtless are there because gcc 2.95.3 whinged about them, but we now know that its algorithm for complaining is many bricks shy of a load. This is certainly a live bug with compilers that optimize similarly to current gcc, so back-patch to all active branches.
1 parent 5c393a0 commit 3dd084c

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/pl/tcl/pltcl.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,9 +2161,9 @@ pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp,
21612161
int j;
21622162
Tcl_HashEntry *hashent;
21632163
pltcl_query_desc *qdesc;
2164-
const char *volatile nulls = NULL;
2165-
CONST84 char *volatile arrayname = NULL;
2166-
CONST84 char *volatile loop_body = NULL;
2164+
const char *nulls = NULL;
2165+
CONST84 char *arrayname = NULL;
2166+
CONST84 char *loop_body = NULL;
21672167
int count = 0;
21682168
int callnargs;
21692169
CONST84 char **callargs = NULL;
@@ -2293,6 +2293,8 @@ pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp,
22932293
if (i != argc)
22942294
{
22952295
Tcl_SetResult(interp, usage, TCL_STATIC);
2296+
if (callargs)
2297+
ckfree((char *) callargs);
22962298
return TCL_ERROR;
22972299
}
22982300

@@ -2331,10 +2333,6 @@ pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp,
23312333
}
23322334
}
23332335

2334-
if (callargs)
2335-
ckfree((char *) callargs);
2336-
callargs = NULL;
2337-
23382336
/************************************************************
23392337
* Execute the plan
23402338
************************************************************/
@@ -2361,6 +2359,9 @@ pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp,
23612359
}
23622360
PG_END_TRY();
23632361

2362+
if (callargs)
2363+
ckfree((char *) callargs);
2364+
23642365
return my_rc;
23652366
}
23662367

0 commit comments

Comments
 (0)