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

Skip to content

Commit 8f6d868

Browse files
committed
code_repr(), com_addop_varname(), com_list_comprehension(),
com_arglist(), symtable_check_unoptimized(), symtable_params(), symtable_global(), symtable_list_comprehension(): Conversion of sprintf() to PyOS_snprintf() for buffer overrun avoidance.
1 parent b97c969 commit 8f6d868

1 file changed

Lines changed: 42 additions & 32 deletions

File tree

Python/compile.c

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ code_repr(PyCodeObject *co)
120120
filename = PyString_AS_STRING(co->co_filename);
121121
if (co->co_name && PyString_Check(co->co_name))
122122
name = PyString_AS_STRING(co->co_name);
123-
sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
124-
name, co, filename, lineno);
123+
PyOS_snprintf(buf, sizeof(buf),
124+
"<code object %.100s at %p, file \"%.300s\", line %d>",
125+
name, co, filename, lineno);
125126
return PyString_FromString(buf);
126127
}
127128

@@ -1020,7 +1021,8 @@ com_addop_varname(struct compiling *c, int kind, char *name)
10201021
break;
10211022
case NAME_CLOSURE: {
10221023
char buf[500];
1023-
sprintf(buf, DEL_CLOSURE_ERROR, name);
1024+
PyOS_snprintf(buf, sizeof(buf),
1025+
DEL_CLOSURE_ERROR, name);
10241026
com_error(c, PyExc_SyntaxError, buf);
10251027
i = 255;
10261028
break;
@@ -1366,8 +1368,8 @@ static void
13661368
com_list_comprehension(struct compiling *c, node *n)
13671369
{
13681370
/* listmaker: test list_for */
1369-
char tmpname[12];
1370-
sprintf(tmpname, "_[%d]", ++c->c_tmpname);
1371+
char tmpname[30];
1372+
PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
13711373
com_addoparg(c, BUILD_LIST, 0);
13721374
com_addbyte(c, DUP_TOP); /* leave the result on the stack */
13731375
com_push(c, 2);
@@ -3789,7 +3791,7 @@ com_arglist(struct compiling *c, node *n)
37893791
{
37903792
int nch, i, narg;
37913793
int complex = 0;
3792-
char nbuf[10];
3794+
char nbuf[30];
37933795
REQ(n, varargslist);
37943796
/* varargslist:
37953797
(fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
@@ -3803,7 +3805,7 @@ com_arglist(struct compiling *c, node *n)
38033805
REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
38043806
fp = CHILD(ch, 0);
38053807
if (TYPE(fp) != NAME) {
3806-
sprintf(nbuf, ".%d", i);
3808+
PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
38073809
complex = 1;
38083810
}
38093811
narg++;
@@ -4455,31 +4457,37 @@ symtable_check_unoptimized(struct compiling *c,
44554457

44564458
if (ste->ste_child_free) {
44574459
if (ste->ste_optimized == OPT_IMPORT_STAR)
4458-
sprintf(buf, ILLEGAL_IMPORT_STAR,
4459-
PyString_AS_STRING(ste->ste_name),
4460-
ILLEGAL_CONTAINS);
4460+
PyOS_snprintf(buf, sizeof(buf),
4461+
ILLEGAL_IMPORT_STAR,
4462+
PyString_AS_STRING(ste->ste_name),
4463+
ILLEGAL_CONTAINS);
44614464
else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4462-
sprintf(buf, ILLEGAL_BARE_EXEC,
4463-
PyString_AS_STRING(ste->ste_name),
4464-
ILLEGAL_CONTAINS);
4465+
PyOS_snprintf(buf, sizeof(buf),
4466+
ILLEGAL_BARE_EXEC,
4467+
PyString_AS_STRING(ste->ste_name),
4468+
ILLEGAL_CONTAINS);
44654469
else {
4466-
sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4467-
PyString_AS_STRING(ste->ste_name),
4468-
ILLEGAL_CONTAINS);
4470+
PyOS_snprintf(buf, sizeof(buf),
4471+
ILLEGAL_EXEC_AND_IMPORT_STAR,
4472+
PyString_AS_STRING(ste->ste_name),
4473+
ILLEGAL_CONTAINS);
44694474
}
44704475
} else {
44714476
if (ste->ste_optimized == OPT_IMPORT_STAR)
4472-
sprintf(buf, ILLEGAL_IMPORT_STAR,
4473-
PyString_AS_STRING(ste->ste_name),
4474-
ILLEGAL_IS);
4477+
PyOS_snprintf(buf, sizeof(buf),
4478+
ILLEGAL_IMPORT_STAR,
4479+
PyString_AS_STRING(ste->ste_name),
4480+
ILLEGAL_IS);
44754481
else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4476-
sprintf(buf, ILLEGAL_BARE_EXEC,
4477-
PyString_AS_STRING(ste->ste_name),
4478-
ILLEGAL_IS);
4482+
PyOS_snprintf(buf, sizeof(buf),
4483+
ILLEGAL_BARE_EXEC,
4484+
PyString_AS_STRING(ste->ste_name),
4485+
ILLEGAL_IS);
44794486
else {
4480-
sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4481-
PyString_AS_STRING(ste->ste_name),
4482-
ILLEGAL_IS);
4487+
PyOS_snprintf(buf, sizeof(buf),
4488+
ILLEGAL_EXEC_AND_IMPORT_STAR,
4489+
PyString_AS_STRING(ste->ste_name),
4490+
ILLEGAL_IS);
44834491
}
44844492
}
44854493

@@ -5231,8 +5239,8 @@ symtable_params(struct symtable *st, node *n)
52315239
if (TYPE(CHILD(c, 0)) == NAME)
52325240
symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
52335241
else {
5234-
char nbuf[10];
5235-
sprintf(nbuf, ".%d", i);
5242+
char nbuf[30];
5243+
PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
52365244
symtable_add_def(st, nbuf, DEF_PARAM);
52375245
complex = i;
52385246
}
@@ -5318,10 +5326,12 @@ symtable_global(struct symtable *st, node *n)
53185326
}
53195327
else {
53205328
if (flags & DEF_LOCAL)
5321-
sprintf(buf, GLOBAL_AFTER_ASSIGN,
5322-
name);
5329+
PyOS_snprintf(buf, sizeof(buf),
5330+
GLOBAL_AFTER_ASSIGN,
5331+
name);
53235332
else
5324-
sprintf(buf, GLOBAL_AFTER_USE, name);
5333+
PyOS_snprintf(buf, sizeof(buf),
5334+
GLOBAL_AFTER_USE, name);
53255335
symtable_warn(st, buf);
53265336
}
53275337
}
@@ -5332,9 +5342,9 @@ symtable_global(struct symtable *st, node *n)
53325342
static void
53335343
symtable_list_comprehension(struct symtable *st, node *n)
53345344
{
5335-
char tmpname[12];
5345+
char tmpname[30];
53365346

5337-
sprintf(tmpname, "_[%d]", st->st_tmpname);
5347+
PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname);
53385348
symtable_add_def(st, tmpname, DEF_LOCAL);
53395349
symtable_assign(st, CHILD(n, 1), 0);
53405350
symtable_node(st, CHILD(n, 3));

0 commit comments

Comments
 (0)