Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a04d47b

Browse files
committed
Don't use static buffers internally for formatstring().
1 parent 70d4478 commit a04d47b

1 file changed

Lines changed: 26 additions & 25 deletions

File tree

Objects/stringobject.c

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -580,18 +580,18 @@ getnextarg(args, arglen, p_argidx)
580580

581581
extern double fabs PROTO((double));
582582

583-
static char *
584-
formatfloat(flags, prec, type, v)
583+
static int
584+
formatfloat(buf, flags, prec, type, v)
585+
char *buf;
585586
int flags;
586587
int prec;
587588
int type;
588589
object *v;
589590
{
590591
char fmt[20];
591-
static char buf[120];
592592
double x;
593593
if (!getargs(v, "d;float argument required", &x))
594-
return NULL;
594+
return -1;
595595
if (prec < 0)
596596
prec = 6;
597597
if (prec > 50)
@@ -600,43 +600,43 @@ formatfloat(flags, prec, type, v)
600600
type = 'g';
601601
sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type);
602602
sprintf(buf, fmt, x);
603-
return buf;
603+
return strlen(buf);
604604
}
605605

606-
static char *
607-
formatint(flags, prec, type, v)
606+
static int
607+
formatint(buf, flags, prec, type, v)
608+
char *buf;
608609
int flags;
609610
int prec;
610611
int type;
611612
object *v;
612613
{
613614
char fmt[20];
614-
static char buf[50];
615615
long x;
616616
if (!getargs(v, "l;int argument required", &x))
617-
return NULL;
617+
return -1;
618618
if (prec < 0)
619619
prec = 1;
620620
sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type);
621621
sprintf(buf, fmt, x);
622-
return buf;
622+
return strlen(buf);
623623
}
624624

625-
static char *
626-
formatchar(v)
625+
static int
626+
formatchar(buf, v)
627+
char *buf;
627628
object *v;
628629
{
629-
static char buf[2];
630630
if (is_stringobject(v)) {
631631
if (!getargs(v, "c;%c requires int or char", &buf[0]))
632-
return NULL;
632+
return -1;
633633
}
634634
else {
635635
if (!getargs(v, "b;%c requires int or char", &buf[0]))
636-
return NULL;
636+
return -1;
637637
}
638638
buf[1] = '\0';
639-
return buf;
639+
return 1;
640640
}
641641

642642

@@ -698,6 +698,7 @@ formatstring(format, args)
698698
char *buf;
699699
int sign;
700700
int len;
701+
char tmpbuf[120]; /* For format{float,int,char}() */
701702
fmt++;
702703
if (*fmt == '(') {
703704
char *keystart;
@@ -849,10 +850,10 @@ formatstring(format, args)
849850
case 'X':
850851
if (c == 'i')
851852
c = 'd';
852-
buf = formatint(flags, prec, c, v);
853-
if (buf == NULL)
853+
buf = tmpbuf;
854+
len = formatint(buf, flags, prec, c, v);
855+
if (len < 0)
854856
goto error;
855-
len = strlen(buf);
856857
sign = (c == 'd');
857858
if (flags&F_ZERO)
858859
fill = '0';
@@ -862,19 +863,19 @@ formatstring(format, args)
862863
case 'f':
863864
case 'g':
864865
case 'G':
865-
buf = formatfloat(flags, prec, c, v);
866-
if (buf == NULL)
866+
buf = tmpbuf;
867+
len = formatfloat(buf, flags, prec, c, v);
868+
if (len < 0)
867869
goto error;
868-
len = strlen(buf);
869870
sign = 1;
870871
if (flags&F_ZERO)
871872
fill = '0';
872873
break;
873874
case 'c':
874-
buf = formatchar(v);
875-
if (buf == NULL)
875+
buf = tmpbuf;
876+
len = formatchar(buf, v);
877+
if (len < 0)
876878
goto error;
877-
len = 1;
878879
break;
879880
default:
880881
err_setstr(ValueError,

0 commit comments

Comments
 (0)