Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita12b4e2

Browse files
committed
I checked all the previous string handling errors and most of them were
already fixed by You. However there were a few left and attached patchshould fix the rest of them.I used StringInfo only in 2 places and both of them are inside debugifdefs. Only performance penalty will come from using strlen() like allthe other code does.I also modified some of the already patched parts by changingsnprintf(buf, 2 * BUFSIZE, ... style lines tosnprintf(buf, sizeof(buf), ... where buf is an array.Jukka Holappa
1 parent48e1a39 commita12b4e2

File tree

16 files changed

+107
-105
lines changed

16 files changed

+107
-105
lines changed

‎contrib/cube/cubeparse.y

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ int cube_yyerror ( char *msg ) {
164164

165165
position =parse_buffer_pos() >parse_buffer_size() ?parse_buffer_pos() -1 :parse_buffer_pos();
166166

167-
sprintf(
167+
snprintf(
168168
buf,
169+
256,
169170
"%s at or before position %d, character ('%c',\\%03o), input: '%s'\n",
170171
msg,
171172
position,

‎contrib/intarray/_int.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include"utils/array.h"
2323
#include"utils/builtins.h"
2424
#include"storage/bufpage.h"
25+
#include"lib/stringinfo.h"
2526

2627
/* number ranges for compression */
2728
#defineMAXNUMRANGE 100
@@ -99,20 +100,19 @@ typedef char *BITVECP;
99100
staticvoid
100101
printarr(ArrayType*a,intnum)
101102
{
102-
charbbb[16384];
103+
StringInfoDatabbb;
103104
char*cur;
104105
intl;
105106
int*d;
106107

107108
d=ARRPTR(a);
108-
*bbb='\0';
109-
cur=bbb;
109+
initStringInfo(&bbb);
110110
for (l=0;l<min(num,ARRNELEMS(a));l++)
111111
{
112-
sprintf(cur,"%d ",d[l]);
113-
cur=strchr(cur,'\0');
112+
appendStringInfo(&bbb,"%d ",d[l]);
114113
}
115-
elog(DEBUG3,"\t\t%s",bbb);
114+
elog(DEBUG3,"\t\t%s",bbb.data);
115+
pfree(bbb.data);
116116
}
117117
staticvoid
118118
printbitvec(BITVECbv)
@@ -1924,7 +1924,7 @@ bqarr_in(PG_FUNCTION_ARGS) {
19241924
NODE*tmp;
19251925
int4pos=0;
19261926
#ifdefBS_DEBUG
1927-
charpbuf[16384],*cur;
1927+
StringInfoDatapbuf;
19281928
#endif
19291929

19301930
state.buf=buf;
@@ -1955,16 +1955,15 @@ bqarr_in(PG_FUNCTION_ARGS) {
19551955
pos=query->size-1;
19561956
findoprnd(ptr,&pos );
19571957
#ifdefBS_DEBUG
1958-
cur=pbuf;
1959-
*cur='\0';
1958+
initStringInfo(&pbuf);
19601959
for(i=0;i<query->size;i++ ) {
19611960
if (ptr[i].type==OPR )
1962-
sprintf(cur,"%c(%d) ",ptr[i].val,ptr[i].left);
1961+
appendStringInfo(&pbuf,"%c(%d) ",ptr[i].val,ptr[i].left);
19631962
else
1964-
sprintf(cur,"%d ",ptr[i].val );
1965-
cur=strchr(cur,'\0');
1963+
appendStringInfo(&pbuf,"%d ",ptr[i].val );
19661964
}
1967-
elog(DEBUG3,"POR: %s",pbuf);
1965+
elog(DEBUG3,"POR: %s",pbuf.data);
1966+
pfree(pbuf.data);
19681967
#endif
19691968

19701969
PG_RETURN_POINTER(query );

‎contrib/seg/segparse.y

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ float seg_atof ( char *value ) {
144144
sscanf(value,"%f", &result);
145145

146146
if ( errno ) {
147-
sprintf(buf,"numeric value %s unrepresentable", value);
147+
snprintf(buf,256,"numeric value %s unrepresentable", value);
148148
reset_parse_buffer();
149149
elog(ERROR, buf);
150150
}
@@ -165,8 +165,9 @@ int seg_yyerror ( char *msg ) {
165165

166166
position =parse_buffer_pos() >parse_buffer_size() ?parse_buffer_pos() -1 :parse_buffer_pos();
167167

168-
sprintf(
168+
snprintf(
169169
buf,
170+
256,
170171
"%s at or near position %d, character ('%c',\\%03o), input: '%s'\n",
171172
msg,
172173
position,

‎contrib/spi/refint.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ check_primary_key(PG_FUNCTION_ARGS)
112112
* Construct ident string as TriggerName $ TriggeredRelationId and try
113113
* to find prepared execution plan.
114114
*/
115-
snprintf(ident,2*NAMEDATALEN,"%s$%u",trigger->tgname,rel->rd_id);
115+
snprintf(ident,sizeof(ident),"%s$%u",trigger->tgname,rel->rd_id);
116116
plan=find_plan(ident,&PPlans,&nPPlans);
117117

118118
/* if there is no plan then allocate argtypes for preparation */
@@ -160,10 +160,10 @@ check_primary_key(PG_FUNCTION_ARGS)
160160
* Construct query: SELECT 1 FROM _referenced_relation_ WHERE
161161
* Pkey1 = $1 [AND Pkey2 = $2 [...]]
162162
*/
163-
snprintf(sql,8192,"select 1 from %s where ",relname);
163+
snprintf(sql,sizeof(sql),"select 1 from %s where ",relname);
164164
for (i=0;i<nkeys;i++)
165165
{
166-
snprintf(sql+strlen(sql),8192-strlen(sql),"%s = $%d %s",
166+
snprintf(sql+strlen(sql),sizeof(sql)-strlen(sql),"%s = $%d %s",
167167
args[i+nkeys+1],i+1, (i<nkeys-1) ?"and " :"");
168168
}
169169

@@ -320,7 +320,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
320320
* Construct ident string as TriggerName $ TriggeredRelationId and try
321321
* to find prepared execution plan(s).
322322
*/
323-
snprintf(ident,2*NAMEDATALEN,"%s$%u",trigger->tgname,rel->rd_id);
323+
snprintf(ident,sizeof(ident),"%s$%u",trigger->tgname,rel->rd_id);
324324
plan=find_plan(ident,&FPlans,&nFPlans);
325325

326326
/* if there is no plan(s) then allocate argtypes for preparation */
@@ -411,7 +411,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
411411
*/
412412
if (action=='r')
413413

414-
snprintf(sql,8192,"select 1 from %s where ",relname);
414+
snprintf(sql,sizeof(sql),"select 1 from %s where ",relname);
415415

416416
/*---------
417417
* For 'C'ascade action we construct DELETE query
@@ -438,7 +438,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
438438
char*nv;
439439
intk;
440440

441-
snprintf(sql,8192,"update %s set ",relname);
441+
snprintf(sql,sizeof(sql),"update %s set ",relname);
442442
for (k=1;k <=nkeys;k++)
443443
{
444444
intis_char_type=0;
@@ -461,7 +461,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
461461
* is_char_type =1 i set ' ' for define a new
462462
* value
463463
*/
464-
snprintf(sql+strlen(sql),8192-strlen(sql),
464+
snprintf(sql+strlen(sql),sizeof(sql)-strlen(sql),
465465
" %s = %s%s%s %s ",
466466
args2[k], (is_char_type>0) ?"'" :"",
467467
nv, (is_char_type>0) ?"'" :"", (k<nkeys) ?", " :"");
@@ -472,7 +472,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
472472
}
473473
else
474474
/* DELETE */
475-
snprintf(sql,8192,"delete from %s where ",relname);
475+
snprintf(sql,sizeof(sql),"delete from %s where ",relname);
476476

477477
}
478478

@@ -484,10 +484,10 @@ check_foreign_key(PG_FUNCTION_ARGS)
484484
*/
485485
elseif (action=='s')
486486
{
487-
snprintf(sql,8192,"update %s set ",relname);
487+
snprintf(sql,sizeof(sql),"update %s set ",relname);
488488
for (i=1;i <=nkeys;i++)
489489
{
490-
snprintf(sql+strlen(sql),8192-strlen(sql),
490+
snprintf(sql+strlen(sql),sizeof(sql)-strlen(sql),
491491
"%s = null%s",
492492
args2[i], (i<nkeys) ?", " :"");
493493
}
@@ -497,7 +497,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
497497
/* Construct WHERE qual */
498498
for (i=1;i <=nkeys;i++)
499499
{
500-
snprintf(sql+strlen(sql),8192-strlen(sql),"%s = $%d %s",
500+
snprintf(sql+strlen(sql),sizeof(sql)-strlen(sql),"%s = $%d %s",
501501
args2[i],i, (i<nkeys) ?"and " :"");
502502
}
503503

@@ -547,7 +547,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
547547

548548
relname=args[0];
549549

550-
snprintf(ident,2*NAMEDATALEN,"%s$%u",trigger->tgname,rel->rd_id);
550+
snprintf(ident,sizeof(ident),"%s$%u",trigger->tgname,rel->rd_id);
551551
plan=find_plan(ident,&FPlans,&nFPlans);
552552
ret=SPI_execp(plan->splan[r],kvals,NULL,tcount);
553553
/* we have no NULLs - so we pass ^^^^ here */

‎contrib/spi/timetravel.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ timetravel(PG_FUNCTION_ARGS)
250250
* Construct ident string as TriggerName $ TriggeredRelationId and try
251251
* to find prepared execution plan.
252252
*/
253-
snprintf(ident,2*NAMEDATALEN,"%s$%u",trigger->tgname,rel->rd_id);
253+
snprintf(ident,sizeof(ident),"%s$%u",trigger->tgname,rel->rd_id);
254254
plan=find_plan(ident,&Plans,&nPlans);
255255

256256
/* if there is no plan ... */
@@ -266,10 +266,10 @@ timetravel(PG_FUNCTION_ARGS)
266266
/*
267267
* Construct query: INSERT INTO _relation_ VALUES ($1, ...)
268268
*/
269-
snprintf(sql,8192,"INSERT INTO %s VALUES (",relname);
269+
snprintf(sql,sizeof(sql),"INSERT INTO %s VALUES (",relname);
270270
for (i=1;i <=natts;i++)
271271
{
272-
snprintf(sql+strlen(sql),8192-strlen(sql),"$%d%s",
272+
snprintf(sql+strlen(sql),sizeof(sql)-strlen(sql),"$%d%s",
273273
i, (i<natts) ?", " :")");
274274
ctypes[i-1]=SPI_gettypeid(tupdesc,i);
275275
}

‎doc/src/sgml/spi.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/spi.sgml,v 1.22 2002/03/22 19:20:30 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/spi.sgml,v 1.23 2002/09/02 06:11:42 momjian Exp $
33
-->
44

55
<Chapter id="spi">
@@ -3815,7 +3815,7 @@ execq(text *sql, int cnt)
38153815
HeapTuple tuple = tuptable->vals[j];
38163816

38173817
for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
3818-
sprintf(buf + strlen (buf), " %s%s",
3818+
snprintf(buf + strlen (buf),sizeof(buf) - strlen(buf)," %s%s",
38193819
SPI_getvalue(tuple, tupdesc, i),
38203820
(i == tupdesc->natts) ? " " : " |");
38213821
elog (INFO, "EXECQ: %s", buf);

‎src/backend/parser/analyze.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
*$Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.246 2002/08/29 07:22:22 ishii Exp $
9+
*$Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.247 2002/09/02 06:11:42 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -2153,7 +2153,7 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt)
21532153
/*
21542154
* Make the leaf query be a subquery in the top-level rangetable.
21552155
*/
2156-
snprintf(selectName,32,"*SELECT* %d",length(pstate->p_rtable)+1);
2156+
snprintf(selectName,sizeof(selectName),"*SELECT* %d",length(pstate->p_rtable)+1);
21572157
rte=addRangeTableEntryForSubquery(pstate,
21582158
selectQuery,
21592159
makeAlias(selectName,NIL),

‎src/backend/storage/file/fd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.94 2002/09/0202:47:03 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.95 2002/09/0206:11:42 momjian Exp $
1111
*
1212
* NOTES:
1313
*
@@ -344,14 +344,14 @@ _dump_lru(void)
344344
Vfd*vfdP=&VfdCache[mru];
345345
charbuf[2048];
346346

347-
sprintf(buf,"LRU: MOST %d ",mru);
347+
snprintf(buf,sizeof(buf),"LRU: MOST %d ",mru);
348348
while (mru!=0)
349349
{
350350
mru=vfdP->lruLessRecently;
351351
vfdP=&VfdCache[mru];
352-
sprintf(buf+strlen(buf),"%d ",mru);
352+
snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),"%d ",mru);
353353
}
354-
sprintf(buf+strlen(buf),"LEAST");
354+
snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),"LEAST");
355355
elog(LOG,buf);
356356
}
357357
#endif/* FDDEBUG */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp