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

Commit295dd33

Browse files
committed
And while we are on it, I would like to submit minor
changes to make snprintf() vsnprintf() and printf()functions in src/port/snprintf.c thread-safe.Nicolai Tufar
1 parent4e89bae commit295dd33

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed

‎src/port/snprintf.c

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,22 @@ typedef unsigned long ulong_long;
8282
* for string length. This covers a nasty loophole.
8383
*
8484
* The other functions are there to prevent NULL pointers from
85-
* causingnast effects.
85+
* causingnasty effects.
8686
**************************************************************/
8787

88-
/*static char _id[] = "$PostgreSQL: pgsql/src/port/snprintf.c,v 1.7 2005/02/28 14:16:16 momjian Exp $";*/
89-
staticchar*end;
90-
staticintSnprfOverflow;
88+
/*static char _id[] = "$PostgreSQL: pgsql/src/port/snprintf.c,v 1.8 2005/03/01 00:38:11 momjian Exp $";*/
9189

9290
intsnprintf(char*str,size_tcount,constchar*fmt,...);
9391
intvsnprintf(char*str,size_tcount,constchar*fmt,va_listargs);
9492
intprintf(constchar*format, ...);
95-
staticvoiddopr(char*buffer,constchar*format,va_listargs);
93+
staticvoiddopr(char*buffer,constchar*format,va_listargs,char*end);
9694

9795
int
9896
printf(constchar*fmt,...)
9997
{
10098
intlen;
10199
va_listargs;
102-
staticchar*buffer[4096];
100+
char*buffer[4096];
103101
char*p;
104102

105103
va_start(args,fmt);
@@ -127,10 +125,10 @@ snprintf(char *str, size_t count, const char *fmt,...)
127125
int
128126
vsnprintf(char*str,size_tcount,constchar*fmt,va_listargs)
129127
{
128+
char*end;
130129
str[0]='\0';
131130
end=str+count-1;
132-
SnprfOverflow=0;
133-
dopr(str,fmt,args);
131+
dopr(str,fmt,args,end);
134132
if (count>0)
135133
end[0]='\0';
136134
returnstrlen(str);
@@ -140,11 +138,11 @@ vsnprintf(char *str, size_t count, const char *fmt, va_list args)
140138
* dopr(): poor man's version of doprintf
141139
*/
142140

143-
staticvoidfmtstr(char*value,intljust,intlen,intzpad,intmaxwidth);
144-
staticvoidfmtnum(long_longvalue,intbase,intdosign,intljust,intlen,intzpad);
145-
staticvoidfmtfloat(doublevalue,chartype,intljust,intlen,intprecision,intpointflag);
146-
staticvoiddostr(char*str,intcut);
147-
staticvoiddopr_outch(intc);
141+
staticvoidfmtstr(char*value,intljust,intlen,intzpad,intmaxwidth,char*end);
142+
staticvoidfmtnum(long_longvalue,intbase,intdosign,intljust,intlen,intzpad,char*end);
143+
staticvoidfmtfloat(doublevalue,chartype,intljust,intlen,intprecision,intpointflag,char*end);
144+
staticvoiddostr(char*str,intcut,char*end);
145+
staticvoiddopr_outch(intc,char*end);
148146

149147
staticchar*output;
150148

@@ -154,7 +152,7 @@ static char *output;
154152
#defineFMTCHAR4
155153

156154
staticvoid
157-
dopr(char*buffer,constchar*format,va_listargs)
155+
dopr(char*buffer,constchar*format,va_listargs,char*end)
158156
{
159157
intch;
160158
long_longvalue;
@@ -417,11 +415,11 @@ dopr(char *buffer, const char *format, va_list args)
417415
case'%':
418416
break;
419417
default:
420-
dostr("???????",0);
418+
dostr("???????",0,end);
421419
}
422420
break;
423421
default:
424-
dopr_outch(ch);
422+
dopr_outch(ch,end);
425423
break;
426424
}
427425
}
@@ -448,27 +446,28 @@ dopr(char *buffer, const char *format, va_list args)
448446
caseFMTSTR:
449447
fmtstr(fmtparptr[i]->value,fmtparptr[i]->ljust,
450448
fmtparptr[i]->len,fmtparptr[i]->zpad,
451-
fmtparptr[i]->maxwidth);
449+
fmtparptr[i]->maxwidth,end);
452450
break;
453451
caseFMTNUM:
454452
fmtnum(fmtparptr[i]->numvalue,fmtparptr[i]->base,
455453
fmtparptr[i]->dosign,fmtparptr[i]->ljust,
456-
fmtparptr[i]->len,fmtparptr[i]->zpad);
454+
fmtparptr[i]->len,fmtparptr[i]->zpad,end);
457455
break;
458456
caseFMTFLOAT:
459457
fmtfloat(fmtparptr[i]->fvalue,fmtparptr[i]->type,
460458
fmtparptr[i]->ljust,fmtparptr[i]->len,
461-
fmtparptr[i]->precision,fmtparptr[i]->pointflag);
459+
fmtparptr[i]->precision,fmtparptr[i]->pointflag,
460+
end);
462461
break;
463462
caseFMTCHAR:
464-
dopr_outch(fmtparptr[i]->charvalue);
463+
dopr_outch(fmtparptr[i]->charvalue,end);
465464
break;
466465
}
467466
format=fmtpar[i].fmtend;
468467
gotonochar;
469468
}
470469
}
471-
dopr_outch(ch);
470+
dopr_outch(ch,end);
472471
nochar:
473472
/* nothing */
474473
;/* semicolon required because a goto has to be attached to a statement */
@@ -477,7 +476,7 @@ dopr(char *buffer, const char *format, va_list args)
477476
}
478477

479478
staticvoid
480-
fmtstr(char*value,intljust,intlen,intzpad,intmaxwidth)
479+
fmtstr(char*value,intljust,intlen,intzpad,intmaxwidth,char*end)
481480
{
482481
intpadlen,
483482
strlen;/* amount to pad */
@@ -494,19 +493,19 @@ fmtstr(char *value, int ljust, int len, int zpad, int maxwidth)
494493
padlen=-padlen;
495494
while (padlen>0)
496495
{
497-
dopr_outch(' ');
496+
dopr_outch(' ',end);
498497
--padlen;
499498
}
500-
dostr(value,maxwidth);
499+
dostr(value,maxwidth,end);
501500
while (padlen<0)
502501
{
503-
dopr_outch(' ');
502+
dopr_outch(' ',end);
504503
++padlen;
505504
}
506505
}
507506

508507
staticvoid
509-
fmtnum(long_longvalue,intbase,intdosign,intljust,intlen,intzpad)
508+
fmtnum(long_longvalue,intbase,intdosign,intljust,intlen,intzpad,char*end)
510509
{
511510
intsignvalue=0;
512511
ulong_longuvalue;
@@ -561,34 +560,34 @@ fmtnum(long_long value, int base, int dosign, int ljust, int len, int zpad)
561560
{
562561
if (signvalue)
563562
{
564-
dopr_outch(signvalue);
563+
dopr_outch(signvalue,end);
565564
--padlen;
566565
signvalue=0;
567566
}
568567
while (padlen>0)
569568
{
570-
dopr_outch(zpad);
569+
dopr_outch(zpad,end);
571570
--padlen;
572571
}
573572
}
574573
while (padlen>0)
575574
{
576-
dopr_outch(' ');
575+
dopr_outch(' ',end);
577576
--padlen;
578577
}
579578
if (signvalue)
580-
dopr_outch(signvalue);
579+
dopr_outch(signvalue,end);
581580
while (place>0)
582-
dopr_outch(convert[--place]);
581+
dopr_outch(convert[--place],end);
583582
while (padlen<0)
584583
{
585-
dopr_outch(' ');
584+
dopr_outch(' ',end);
586585
++padlen;
587586
}
588587
}
589588

590589
staticvoid
591-
fmtfloat(doublevalue,chartype,intljust,intlen,intprecision,intpointflag)
590+
fmtfloat(doublevalue,chartype,intljust,intlen,intprecision,intpointflag,char*end)
592591
{
593592
charfmt[32];
594593
charconvert[512];
@@ -615,34 +614,34 @@ fmtfloat(double value, char type, int ljust, int len, int precision, int pointfl
615614

616615
while (padlen>0)
617616
{
618-
dopr_outch(' ');
617+
dopr_outch(' ',end);
619618
--padlen;
620619
}
621-
dostr(convert,0);
620+
dostr(convert,0,end);
622621
while (padlen<0)
623622
{
624-
dopr_outch(' ');
623+
dopr_outch(' ',end);
625624
++padlen;
626625
}
627626
}
628627

629628
staticvoid
630-
dostr(char*str,intcut)
629+
dostr(char*str,intcut,char*end)
631630
{
632631
if (cut)
633632
{
634633
while (*str&&cut-->0)
635-
dopr_outch(*str++);
634+
dopr_outch(*str++,end);
636635
}
637636
else
638637
{
639638
while (*str)
640-
dopr_outch(*str++);
639+
dopr_outch(*str++,end);
641640
}
642641
}
643642

644643
staticvoid
645-
dopr_outch(intc)
644+
dopr_outch(intc,char*end)
646645
{
647646
#ifdefNOT_USED
648647
if (iscntrl((unsignedchar)c)&&c!='\n'&&c!='\t')
@@ -654,6 +653,4 @@ dopr_outch(int c)
654653
#endif
655654
if (end==0||output<end)
656655
*output++=c;
657-
else
658-
SnprfOverflow++;
659656
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp