bpo-18407: ast.c uses Py_ssize_t for asdl_seq_LEN() iterator (GH-10655)

When iterating using asdl_seq_LEN(), use 'Py_ssize_t' type instead of
'int' for the iterator variable, to avoid downcast on 64-bit platforms.

_Py_asdl_int_seq_new() now also ensures that the index is greater than
or equal to 0.
This commit is contained in:
Victor Stinner 2018-11-22 14:45:16 +01:00 committed by GitHub
parent c48ff73dd6
commit 4d73ae7761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 10 deletions

View File

@ -36,7 +36,7 @@ asdl_int_seq *_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena);
do { \ do { \
Py_ssize_t _asdl_i = (I); \ Py_ssize_t _asdl_i = (I); \
assert((S) != NULL); \ assert((S) != NULL); \
assert(_asdl_i < (S)->size); \ assert(0 <= _asdl_i && _asdl_i < (S)->size); \
(S)->elements[_asdl_i] = (V); \ (S)->elements[_asdl_i] = (V); \
} while (0) } while (0)
#else #else

View File

@ -22,7 +22,7 @@ static int validate_expr(expr_ty, expr_context_ty);
static int static int
validate_comprehension(asdl_seq *gens) validate_comprehension(asdl_seq *gens)
{ {
int i; Py_ssize_t i;
if (!asdl_seq_LEN(gens)) { if (!asdl_seq_LEN(gens)) {
PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
return 0; return 0;
@ -46,7 +46,7 @@ validate_slice(slice_ty slice)
(!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
(!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
case ExtSlice_kind: { case ExtSlice_kind: {
int i; Py_ssize_t i;
if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
return 0; return 0;
for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
@ -65,7 +65,7 @@ validate_slice(slice_ty slice)
static int static int
validate_keywords(asdl_seq *keywords) validate_keywords(asdl_seq *keywords)
{ {
int i; Py_ssize_t i;
for (i = 0; i < asdl_seq_LEN(keywords); i++) for (i = 0; i < asdl_seq_LEN(keywords); i++)
if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
return 0; return 0;
@ -75,7 +75,7 @@ validate_keywords(asdl_seq *keywords)
static int static int
validate_args(asdl_seq *args) validate_args(asdl_seq *args)
{ {
int i; Py_ssize_t i;
for (i = 0; i < asdl_seq_LEN(args); i++) { for (i = 0; i < asdl_seq_LEN(args); i++) {
arg_ty arg = asdl_seq_GET(args, i); arg_ty arg = asdl_seq_GET(args, i);
if (arg->annotation && !validate_expr(arg->annotation, Load)) if (arg->annotation && !validate_expr(arg->annotation, Load))
@ -348,7 +348,7 @@ validate_body(asdl_seq *body, const char *owner)
static int static int
validate_stmt(stmt_ty stmt) validate_stmt(stmt_ty stmt)
{ {
int i; Py_ssize_t i;
switch (stmt->kind) { switch (stmt->kind) {
case FunctionDef_kind: case FunctionDef_kind:
return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
@ -490,7 +490,7 @@ validate_stmt(stmt_ty stmt)
static int static int
validate_stmts(asdl_seq *seq) validate_stmts(asdl_seq *seq)
{ {
int i; Py_ssize_t i;
for (i = 0; i < asdl_seq_LEN(seq); i++) { for (i = 0; i < asdl_seq_LEN(seq); i++) {
stmt_ty stmt = asdl_seq_GET(seq, i); stmt_ty stmt = asdl_seq_GET(seq, i);
if (stmt) { if (stmt) {
@ -509,7 +509,7 @@ validate_stmts(asdl_seq *seq)
static int static int
validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
{ {
int i; Py_ssize_t i;
for (i = 0; i < asdl_seq_LEN(exprs); i++) { for (i = 0; i < asdl_seq_LEN(exprs); i++) {
expr_ty expr = asdl_seq_GET(exprs, i); expr_ty expr = asdl_seq_GET(exprs, i);
if (expr) { if (expr) {
@ -1060,7 +1060,7 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
context for all the contained elements. context for all the contained elements.
*/ */
if (s) { if (s) {
int i; Py_ssize_t i;
for (i = 0; i < asdl_seq_LEN(s); i++) { for (i = 0; i < asdl_seq_LEN(s); i++) {
if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n)) if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
@ -2355,7 +2355,7 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
by treating the sequence as a tuple literal if there are by treating the sequence as a tuple literal if there are
no slice features. no slice features.
*/ */
int j; Py_ssize_t j;
slice_ty slc; slice_ty slc;
expr_ty e; expr_ty e;
int simple = 1; int simple = 1;