Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 295dd33

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 parent 4e89bae commit 295dd33

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-
* causing nast effects.
85+
* causing nasty 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-
static char *end;
90-
static int SnprfOverflow;
88+
/*static char _id[] = "$PostgreSQL: pgsql/src/port/snprintf.c,v 1.8 2005/03/01 00:38:11 momjian Exp $";*/
9189

9290
int snprintf(char *str, size_t count, const char *fmt,...);
9391
int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
9492
int printf(const char *format, ...);
95-
static void dopr(char *buffer, const char *format, va_list args);
93+
static void dopr(char *buffer, const char *format, va_list args, char *end);
9694

9795
int
9896
printf(const char *fmt,...)
9997
{
10098
int len;
10199
va_list args;
102-
static char* 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_t count, const char *fmt, va_list args)
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
return strlen(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-
static void fmtstr(char *value, int ljust, int len, int zpad, int maxwidth);
144-
static void fmtnum(long_long value, int base, int dosign, int ljust, int len, int zpad);
145-
static void fmtfloat(double value, char type, int ljust, int len, int precision, int pointflag);
146-
static void dostr(char *str, int cut);
147-
static void dopr_outch(int c);
141+
static void fmtstr(char *value, int ljust, int len, int zpad, int maxwidth, char *end);
142+
static void fmtnum(long_long value, int base, int dosign, int ljust, int len, int zpad, char *end);
143+
static void fmtfloat(double value, char type, int ljust, int len, int precision, int pointflag, char *end);
144+
static void dostr(char *str, int cut, char *end);
145+
static void dopr_outch(int c, char *end);
148146

149147
static char *output;
150148

@@ -154,7 +152,7 @@ static char *output;
154152
#define FMTCHAR 4
155153

156154
static void
157-
dopr(char *buffer, const char *format, va_list args)
155+
dopr(char *buffer, const char *format, va_list args, char *end)
158156
{
159157
int ch;
160158
long_long value;
@@ -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
case FMTSTR:
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
case FMTNUM:
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
case FMTFLOAT:
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
case FMTCHAR:
464-
dopr_outch(fmtparptr[i]->charvalue);
463+
dopr_outch(fmtparptr[i]->charvalue, end);
465464
break;
466465
}
467466
format = fmtpar[i].fmtend;
468467
goto nochar;
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
static void
480-
fmtstr(char *value, int ljust, int len, int zpad, int maxwidth)
479+
fmtstr(char *value, int ljust, int len, int zpad, int maxwidth, char *end)
481480
{
482481
int padlen,
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
static void
509-
fmtnum(long_long value, int base, int dosign, int ljust, int len, int zpad)
508+
fmtnum(long_long value, int base, int dosign, int ljust, int len, int zpad, char *end)
510509
{
511510
int signvalue = 0;
512511
ulong_long uvalue;
@@ -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
static void
591-
fmtfloat(double value, char type, int ljust, int len, int precision, int pointflag)
590+
fmtfloat(double value, char type, int ljust, int len, int precision, int pointflag, char *end)
592591
{
593592
char fmt[32];
594593
char convert[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
static void
630-
dostr(char *str, int cut)
629+
dostr(char *str, int cut, 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
static void
645-
dopr_outch(int c)
644+
dopr_outch(int c, char *end)
646645
{
647646
#ifdef NOT_USED
648647
if (iscntrl((unsigned char) 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)