Commit 8f6d868b authored by Barry Warsaw's avatar Barry Warsaw

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.
parent b97c969f
...@@ -120,8 +120,9 @@ code_repr(PyCodeObject *co) ...@@ -120,8 +120,9 @@ code_repr(PyCodeObject *co)
filename = PyString_AS_STRING(co->co_filename); filename = PyString_AS_STRING(co->co_filename);
if (co->co_name && PyString_Check(co->co_name)) if (co->co_name && PyString_Check(co->co_name))
name = PyString_AS_STRING(co->co_name); name = PyString_AS_STRING(co->co_name);
sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>", PyOS_snprintf(buf, sizeof(buf),
name, co, filename, lineno); "<code object %.100s at %p, file \"%.300s\", line %d>",
name, co, filename, lineno);
return PyString_FromString(buf); return PyString_FromString(buf);
} }
...@@ -1020,7 +1021,8 @@ com_addop_varname(struct compiling *c, int kind, char *name) ...@@ -1020,7 +1021,8 @@ com_addop_varname(struct compiling *c, int kind, char *name)
break; break;
case NAME_CLOSURE: { case NAME_CLOSURE: {
char buf[500]; char buf[500];
sprintf(buf, DEL_CLOSURE_ERROR, name); PyOS_snprintf(buf, sizeof(buf),
DEL_CLOSURE_ERROR, name);
com_error(c, PyExc_SyntaxError, buf); com_error(c, PyExc_SyntaxError, buf);
i = 255; i = 255;
break; break;
...@@ -1366,8 +1368,8 @@ static void ...@@ -1366,8 +1368,8 @@ static void
com_list_comprehension(struct compiling *c, node *n) com_list_comprehension(struct compiling *c, node *n)
{ {
/* listmaker: test list_for */ /* listmaker: test list_for */
char tmpname[12]; char tmpname[30];
sprintf(tmpname, "_[%d]", ++c->c_tmpname); PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
com_addoparg(c, BUILD_LIST, 0); com_addoparg(c, BUILD_LIST, 0);
com_addbyte(c, DUP_TOP); /* leave the result on the stack */ com_addbyte(c, DUP_TOP); /* leave the result on the stack */
com_push(c, 2); com_push(c, 2);
...@@ -3789,7 +3791,7 @@ com_arglist(struct compiling *c, node *n) ...@@ -3789,7 +3791,7 @@ com_arglist(struct compiling *c, node *n)
{ {
int nch, i, narg; int nch, i, narg;
int complex = 0; int complex = 0;
char nbuf[10]; char nbuf[30];
REQ(n, varargslist); REQ(n, varargslist);
/* varargslist: /* varargslist:
(fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
...@@ -3803,7 +3805,7 @@ com_arglist(struct compiling *c, node *n) ...@@ -3803,7 +3805,7 @@ com_arglist(struct compiling *c, node *n)
REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
fp = CHILD(ch, 0); fp = CHILD(ch, 0);
if (TYPE(fp) != NAME) { if (TYPE(fp) != NAME) {
sprintf(nbuf, ".%d", i); PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
complex = 1; complex = 1;
} }
narg++; narg++;
...@@ -4455,31 +4457,37 @@ symtable_check_unoptimized(struct compiling *c, ...@@ -4455,31 +4457,37 @@ symtable_check_unoptimized(struct compiling *c,
if (ste->ste_child_free) { if (ste->ste_child_free) {
if (ste->ste_optimized == OPT_IMPORT_STAR) if (ste->ste_optimized == OPT_IMPORT_STAR)
sprintf(buf, ILLEGAL_IMPORT_STAR, PyOS_snprintf(buf, sizeof(buf),
PyString_AS_STRING(ste->ste_name), ILLEGAL_IMPORT_STAR,
ILLEGAL_CONTAINS); PyString_AS_STRING(ste->ste_name),
ILLEGAL_CONTAINS);
else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
sprintf(buf, ILLEGAL_BARE_EXEC, PyOS_snprintf(buf, sizeof(buf),
PyString_AS_STRING(ste->ste_name), ILLEGAL_BARE_EXEC,
ILLEGAL_CONTAINS); PyString_AS_STRING(ste->ste_name),
ILLEGAL_CONTAINS);
else { else {
sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR, PyOS_snprintf(buf, sizeof(buf),
PyString_AS_STRING(ste->ste_name), ILLEGAL_EXEC_AND_IMPORT_STAR,
ILLEGAL_CONTAINS); PyString_AS_STRING(ste->ste_name),
ILLEGAL_CONTAINS);
} }
} else { } else {
if (ste->ste_optimized == OPT_IMPORT_STAR) if (ste->ste_optimized == OPT_IMPORT_STAR)
sprintf(buf, ILLEGAL_IMPORT_STAR, PyOS_snprintf(buf, sizeof(buf),
PyString_AS_STRING(ste->ste_name), ILLEGAL_IMPORT_STAR,
ILLEGAL_IS); PyString_AS_STRING(ste->ste_name),
ILLEGAL_IS);
else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
sprintf(buf, ILLEGAL_BARE_EXEC, PyOS_snprintf(buf, sizeof(buf),
PyString_AS_STRING(ste->ste_name), ILLEGAL_BARE_EXEC,
ILLEGAL_IS); PyString_AS_STRING(ste->ste_name),
ILLEGAL_IS);
else { else {
sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR, PyOS_snprintf(buf, sizeof(buf),
PyString_AS_STRING(ste->ste_name), ILLEGAL_EXEC_AND_IMPORT_STAR,
ILLEGAL_IS); PyString_AS_STRING(ste->ste_name),
ILLEGAL_IS);
} }
} }
...@@ -5231,8 +5239,8 @@ symtable_params(struct symtable *st, node *n) ...@@ -5231,8 +5239,8 @@ symtable_params(struct symtable *st, node *n)
if (TYPE(CHILD(c, 0)) == NAME) if (TYPE(CHILD(c, 0)) == NAME)
symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
else { else {
char nbuf[10]; char nbuf[30];
sprintf(nbuf, ".%d", i); PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
symtable_add_def(st, nbuf, DEF_PARAM); symtable_add_def(st, nbuf, DEF_PARAM);
complex = i; complex = i;
} }
...@@ -5318,10 +5326,12 @@ symtable_global(struct symtable *st, node *n) ...@@ -5318,10 +5326,12 @@ symtable_global(struct symtable *st, node *n)
} }
else { else {
if (flags & DEF_LOCAL) if (flags & DEF_LOCAL)
sprintf(buf, GLOBAL_AFTER_ASSIGN, PyOS_snprintf(buf, sizeof(buf),
name); GLOBAL_AFTER_ASSIGN,
name);
else else
sprintf(buf, GLOBAL_AFTER_USE, name); PyOS_snprintf(buf, sizeof(buf),
GLOBAL_AFTER_USE, name);
symtable_warn(st, buf); symtable_warn(st, buf);
} }
} }
...@@ -5332,9 +5342,9 @@ symtable_global(struct symtable *st, node *n) ...@@ -5332,9 +5342,9 @@ symtable_global(struct symtable *st, node *n)
static void static void
symtable_list_comprehension(struct symtable *st, node *n) symtable_list_comprehension(struct symtable *st, node *n)
{ {
char tmpname[12]; char tmpname[30];
sprintf(tmpname, "_[%d]", st->st_tmpname); PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname);
symtable_add_def(st, tmpname, DEF_LOCAL); symtable_add_def(st, tmpname, DEF_LOCAL);
symtable_assign(st, CHILD(n, 1), 0); symtable_assign(st, CHILD(n, 1), 0);
symtable_node(st, CHILD(n, 3)); symtable_node(st, CHILD(n, 3));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment