31
31
* ENHANCEMENTS, OR MODIFICATIONS.
32
32
*
33
33
* IDENTIFICATION
34
- * $Header: /cvsroot/pgsql/src/pl/tcl/pltcl.c,v 1.64 2002/09/26 05:39:03 momjian Exp $
34
+ * $Header: /cvsroot/pgsql/src/pl/tcl/pltcl.c,v 1.65 2002/10/14 04:20:52 momjian Exp $
35
35
*
36
36
**********************************************************************/
37
37
38
38
#include "postgres.h"
39
39
40
40
#include <tcl.h>
41
41
42
- #include <stdio.h>
43
- #include <stdlib.h>
44
- #include <stdarg.h>
45
42
#include <unistd.h>
46
43
#include <fcntl.h>
47
- #include <string.h>
48
44
#include <setjmp.h>
49
45
50
46
#include "access/heapam.h"
@@ -308,6 +304,7 @@ pltcl_init_load_unknown(Tcl_Interp *interp)
308
304
************************************************************/
309
305
spi_rc = SPI_exec ("select 1 from pg_class "
310
306
"where relname = 'pltcl_modules'" , 1 );
307
+ SPI_freetuptable (SPI_tuptable );
311
308
if (spi_rc != SPI_OK_SELECT )
312
309
elog (ERROR , "pltcl_init_load_unknown(): select from pg_class failed" );
313
310
if (SPI_processed == 0 )
@@ -334,6 +331,7 @@ pltcl_init_load_unknown(Tcl_Interp *interp)
334
331
if (SPI_processed == 0 )
335
332
{
336
333
Tcl_DStringFree (& unknown_src );
334
+ SPI_freetuptable (SPI_tuptable );
337
335
elog (WARNING , "pltcl: Module unknown not found in pltcl_modules" );
338
336
return ;
339
337
}
@@ -359,6 +357,7 @@ pltcl_init_load_unknown(Tcl_Interp *interp)
359
357
}
360
358
tcl_rc = Tcl_GlobalEval (interp , Tcl_DStringValue (& unknown_src ));
361
359
Tcl_DStringFree (& unknown_src );
360
+ SPI_freetuptable (SPI_tuptable );
362
361
}
363
362
364
363
@@ -955,9 +954,11 @@ compile_pltcl_function(Oid fn_oid, bool is_trigger)
955
954
* Build our internal proc name from the functions Oid
956
955
************************************************************/
957
956
if (!is_trigger )
958
- sprintf (internal_proname , "__PLTcl_proc_%u" , fn_oid );
957
+ snprintf (internal_proname , sizeof (internal_proname ),
958
+ "__PLTcl_proc_%u" , fn_oid );
959
959
else
960
- sprintf (internal_proname , "__PLTcl_proc_%u_trigger" , fn_oid );
960
+ snprintf (internal_proname , sizeof (internal_proname ),
961
+ "__PLTcl_proc_%u_trigger" , fn_oid );
961
962
962
963
/************************************************************
963
964
* Lookup the internal proc name in the hashtable
@@ -1127,7 +1128,7 @@ compile_pltcl_function(Oid fn_oid, bool is_trigger)
1127
1128
prodesc -> arg_is_rel [i ] = 1 ;
1128
1129
if (i > 0 )
1129
1130
strcat (proc_internal_args , " " );
1130
- sprintf (buf , "__PLTcl_Tup_%d" , i + 1 );
1131
+ snprintf (buf , sizeof ( buf ) , "__PLTcl_Tup_%d" , i + 1 );
1131
1132
strcat (proc_internal_args , buf );
1132
1133
ReleaseSysCache (typeTup );
1133
1134
continue ;
@@ -1140,7 +1141,7 @@ compile_pltcl_function(Oid fn_oid, bool is_trigger)
1140
1141
1141
1142
if (i > 0 )
1142
1143
strcat (proc_internal_args , " " );
1143
- sprintf (buf , "%d" , i + 1 );
1144
+ snprintf (buf , sizeof ( buf ) , "%d" , i + 1 );
1144
1145
strcat (proc_internal_args , buf );
1145
1146
1146
1147
ReleaseSysCache (typeTup );
@@ -1177,7 +1178,8 @@ compile_pltcl_function(Oid fn_oid, bool is_trigger)
1177
1178
{
1178
1179
if (!prodesc -> arg_is_rel [i ])
1179
1180
continue ;
1180
- sprintf (buf , "array set %d $__PLTcl_Tup_%d\n" , i + 1 , i + 1 );
1181
+ snprintf (buf , sizeof (buf ), "array set %d $__PLTcl_Tup_%d\n" ,
1182
+ i + 1 , i + 1 );
1181
1183
Tcl_DStringAppend (& proc_internal_body , buf , -1 );
1182
1184
}
1183
1185
}
@@ -1475,6 +1477,7 @@ pltcl_SPI_exec(ClientData cdata, Tcl_Interp *interp,
1475
1477
int ntuples ;
1476
1478
HeapTuple * volatile tuples ;
1477
1479
volatile TupleDesc tupdesc = NULL ;
1480
+ SPITupleTable * tuptable ;
1478
1481
sigjmp_buf save_restart ;
1479
1482
1480
1483
char * usage = "syntax error - 'SPI_exec "
@@ -1557,14 +1560,16 @@ pltcl_SPI_exec(ClientData cdata, Tcl_Interp *interp,
1557
1560
{
1558
1561
case SPI_OK_UTILITY :
1559
1562
Tcl_SetResult (interp , "0" , TCL_VOLATILE );
1563
+ SPI_freetuptable (SPI_tuptable );
1560
1564
return TCL_OK ;
1561
1565
1562
1566
case SPI_OK_SELINTO :
1563
1567
case SPI_OK_INSERT :
1564
1568
case SPI_OK_DELETE :
1565
1569
case SPI_OK_UPDATE :
1566
- sprintf (buf , "%d" , SPI_processed );
1570
+ snprintf (buf , sizeof ( buf ) , "%d" , SPI_processed );
1567
1571
Tcl_SetResult (interp , buf , TCL_VOLATILE );
1572
+ SPI_freetuptable (SPI_tuptable );
1568
1573
return TCL_OK ;
1569
1574
1570
1575
case SPI_OK_SELECT :
@@ -1607,7 +1612,7 @@ pltcl_SPI_exec(ClientData cdata, Tcl_Interp *interp,
1607
1612
return TCL_ERROR ;
1608
1613
1609
1614
default :
1610
- sprintf (buf , "%d" , spi_rc );
1615
+ snprintf (buf , sizeof ( buf ) , "%d" , spi_rc );
1611
1616
Tcl_AppendResult (interp , "pltcl: SPI_exec() failed - " ,
1612
1617
"unknown RC " , buf , NULL );
1613
1618
return TCL_ERROR ;
@@ -1645,12 +1650,15 @@ pltcl_SPI_exec(ClientData cdata, Tcl_Interp *interp,
1645
1650
{
1646
1651
if (ntuples > 0 )
1647
1652
pltcl_set_tuple_values (interp , arrayname , 0 , tuples [0 ], tupdesc );
1648
- sprintf (buf , "%d" , ntuples );
1653
+ snprintf (buf , sizeof ( buf ) , "%d" , ntuples );
1649
1654
Tcl_SetResult (interp , buf , TCL_VOLATILE );
1655
+ SPI_freetuptable (SPI_tuptable );
1650
1656
memcpy (& Warn_restart , & save_restart , sizeof (Warn_restart ));
1651
1657
return TCL_OK ;
1652
1658
}
1653
1659
1660
+ tuptable = SPI_tuptable ;
1661
+
1654
1662
/************************************************************
1655
1663
* There is a loop body - process all tuples and evaluate
1656
1664
* the body on each
@@ -1668,20 +1676,24 @@ pltcl_SPI_exec(ClientData cdata, Tcl_Interp *interp,
1668
1676
continue ;
1669
1677
if (loop_rc == TCL_RETURN )
1670
1678
{
1679
+ SPI_freetuptable (tuptable );
1671
1680
memcpy (& Warn_restart , & save_restart , sizeof (Warn_restart ));
1672
1681
return TCL_RETURN ;
1673
1682
}
1674
1683
if (loop_rc == TCL_BREAK )
1675
1684
break ;
1685
+ SPI_freetuptable (tuptable );
1676
1686
memcpy (& Warn_restart , & save_restart , sizeof (Warn_restart ));
1677
1687
return TCL_ERROR ;
1678
1688
}
1679
1689
1690
+ SPI_freetuptable (tuptable );
1691
+
1680
1692
/************************************************************
1681
1693
* Finally return the number of tuples
1682
1694
************************************************************/
1683
1695
memcpy (& Warn_restart , & save_restart , sizeof (Warn_restart ));
1684
- sprintf (buf , "%d" , ntuples );
1696
+ snprintf (buf , sizeof ( buf ) , "%d" , ntuples );
1685
1697
Tcl_SetResult (interp , buf , TCL_VOLATILE );
1686
1698
return TCL_OK ;
1687
1699
}
@@ -1690,7 +1702,7 @@ pltcl_SPI_exec(ClientData cdata, Tcl_Interp *interp,
1690
1702
/**********************************************************************
1691
1703
* pltcl_SPI_prepare() - Builtin support for prepared plans
1692
1704
* The Tcl command SPI_prepare
1693
- * allways saves the plan using
1705
+ * always saves the plan using
1694
1706
* SPI_saveplan and returns a key for
1695
1707
* access. There is no chance to prepare
1696
1708
* and not save the plan currently.
@@ -1736,7 +1748,7 @@ pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
1736
1748
* Allocate the new querydesc structure
1737
1749
************************************************************/
1738
1750
qdesc = (pltcl_query_desc * ) malloc (sizeof (pltcl_query_desc ));
1739
- sprintf (qdesc -> qname , "%lx" , (long ) qdesc );
1751
+ snprintf (qdesc -> qname , sizeof ( qdesc -> qname ) , "%lx" , (long ) qdesc );
1740
1752
qdesc -> nargs = nargs ;
1741
1753
qdesc -> argtypes = (Oid * ) malloc (nargs * sizeof (Oid ));
1742
1754
qdesc -> arginfuncs = (FmgrInfo * ) malloc (nargs * sizeof (FmgrInfo ));
@@ -1815,7 +1827,7 @@ pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
1815
1827
break ;
1816
1828
1817
1829
default :
1818
- sprintf (buf , "unknown RC %d" , SPI_result );
1830
+ snprintf (buf , sizeof ( buf ) , "unknown RC %d" , SPI_result );
1819
1831
reason = buf ;
1820
1832
break ;
1821
1833
@@ -1846,7 +1858,7 @@ pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
1846
1858
break ;
1847
1859
1848
1860
default :
1849
- sprintf (buf , "unknown RC %d" , SPI_result );
1861
+ snprintf (buf , sizeof ( buf ) , "unknown RC %d" , SPI_result );
1850
1862
reason = buf ;
1851
1863
break ;
1852
1864
@@ -1897,6 +1909,7 @@ pltcl_SPI_execp(ClientData cdata, Tcl_Interp *interp,
1897
1909
int ntuples ;
1898
1910
HeapTuple * volatile tuples = NULL ;
1899
1911
volatile TupleDesc tupdesc = NULL ;
1912
+ SPITupleTable * tuptable ;
1900
1913
sigjmp_buf save_restart ;
1901
1914
Tcl_HashTable * query_hash ;
1902
1915
@@ -2116,14 +2129,16 @@ pltcl_SPI_execp(ClientData cdata, Tcl_Interp *interp,
2116
2129
{
2117
2130
case SPI_OK_UTILITY :
2118
2131
Tcl_SetResult (interp , "0" , TCL_VOLATILE );
2132
+ SPI_freetuptable (SPI_tuptable );
2119
2133
return TCL_OK ;
2120
2134
2121
2135
case SPI_OK_SELINTO :
2122
2136
case SPI_OK_INSERT :
2123
2137
case SPI_OK_DELETE :
2124
2138
case SPI_OK_UPDATE :
2125
- sprintf (buf , "%d" , SPI_processed );
2139
+ snprintf (buf , sizeof ( buf ) , "%d" , SPI_processed );
2126
2140
Tcl_SetResult (interp , buf , TCL_VOLATILE );
2141
+ SPI_freetuptable (SPI_tuptable );
2127
2142
return TCL_OK ;
2128
2143
2129
2144
case SPI_OK_SELECT :
@@ -2166,7 +2181,7 @@ pltcl_SPI_execp(ClientData cdata, Tcl_Interp *interp,
2166
2181
return TCL_ERROR ;
2167
2182
2168
2183
default :
2169
- sprintf (buf , "%d" , spi_rc );
2184
+ snprintf (buf , sizeof ( buf ) , "%d" , spi_rc );
2170
2185
Tcl_AppendResult (interp , "pltcl: SPI_exec() failed - " ,
2171
2186
"unknown RC " , buf , NULL );
2172
2187
return TCL_ERROR ;
@@ -2208,11 +2223,14 @@ pltcl_SPI_execp(ClientData cdata, Tcl_Interp *interp,
2208
2223
if (ntuples > 0 )
2209
2224
pltcl_set_tuple_values (interp , arrayname , 0 , tuples [0 ], tupdesc );
2210
2225
memcpy (& Warn_restart , & save_restart , sizeof (Warn_restart ));
2211
- sprintf (buf , "%d" , ntuples );
2226
+ snprintf (buf , sizeof ( buf ) , "%d" , ntuples );
2212
2227
Tcl_SetResult (interp , buf , TCL_VOLATILE );
2228
+ SPI_freetuptable (SPI_tuptable );
2213
2229
return TCL_OK ;
2214
2230
}
2215
2231
2232
+ tuptable = SPI_tuptable ;
2233
+
2216
2234
/************************************************************
2217
2235
* There is a loop body - process all tuples and evaluate
2218
2236
* the body on each
@@ -2229,20 +2247,24 @@ pltcl_SPI_execp(ClientData cdata, Tcl_Interp *interp,
2229
2247
continue ;
2230
2248
if (loop_rc == TCL_RETURN )
2231
2249
{
2250
+ SPI_freetuptable (tuptable );
2232
2251
memcpy (& Warn_restart , & save_restart , sizeof (Warn_restart ));
2233
2252
return TCL_RETURN ;
2234
2253
}
2235
2254
if (loop_rc == TCL_BREAK )
2236
2255
break ;
2256
+ SPI_freetuptable (tuptable );
2237
2257
memcpy (& Warn_restart , & save_restart , sizeof (Warn_restart ));
2238
2258
return TCL_ERROR ;
2239
2259
}
2240
2260
2261
+ SPI_freetuptable (tuptable );
2262
+
2241
2263
/************************************************************
2242
2264
* Finally return the number of tuples
2243
2265
************************************************************/
2244
2266
memcpy (& Warn_restart , & save_restart , sizeof (Warn_restart ));
2245
- sprintf (buf , "%d" , ntuples );
2267
+ snprintf (buf , sizeof ( buf ) , "%d" , ntuples );
2246
2268
Tcl_SetResult (interp , buf , TCL_VOLATILE );
2247
2269
return TCL_OK ;
2248
2270
}
@@ -2258,7 +2280,7 @@ pltcl_SPI_lastoid(ClientData cdata, Tcl_Interp *interp,
2258
2280
{
2259
2281
char buf [64 ];
2260
2282
2261
- sprintf (buf , "%u" , SPI_lastoid );
2283
+ snprintf (buf , sizeof ( buf ) , "%u" , SPI_lastoid );
2262
2284
Tcl_SetResult (interp , buf , TCL_VOLATILE );
2263
2285
return TCL_OK ;
2264
2286
}
@@ -2300,7 +2322,7 @@ pltcl_set_tuple_values(Tcl_Interp *interp, char *arrayname,
2300
2322
{
2301
2323
arrptr = & arrayname ;
2302
2324
nameptr = & attname ;
2303
- sprintf (buf , "%d" , tupno );
2325
+ snprintf (buf , sizeof ( buf ) , "%d" , tupno );
2304
2326
Tcl_SetVar2 (interp , arrayname , ".tupno" , buf , 0 );
2305
2327
}
2306
2328
0 commit comments