bpo-43892: Validate the first term of complex literal value patterns (GH-25735)

This commit is contained in:
Brandt Bucher 2021-04-29 17:19:28 -07:00 committed by GitHub
parent 87655e2cf5
commit dbe60ee09d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 595 additions and 441 deletions

View file

@ -825,7 +825,7 @@ astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
break;
case MatchAs_kind:
if (node_->v.MatchAs.pattern) {
CALL(astfold_pattern, expr_ty, node_->v.MatchAs.pattern);
CALL(astfold_pattern, pattern_ty, node_->v.MatchAs.pattern);
}
break;
case MatchOr_kind:

View file

@ -5609,6 +5609,10 @@ compiler_slice(struct compiler *c, expr_ty s)
static int
pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
{
if (n == NULL) {
ADDOP(c, POP_TOP);
return 1;
}
if (forbidden_name(c, n, Store)) {
return 0;
}
@ -5782,42 +5786,23 @@ compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *p
return 1;
}
static int
compiler_pattern_capture(struct compiler *c, identifier n, pattern_context *pc)
{
RETURN_IF_FALSE(pattern_helper_store_name(c, n, pc));
ADDOP_LOAD_CONST(c, Py_True);
return 1;
}
static int
compiler_pattern_wildcard(struct compiler *c, pattern_ty p, pattern_context *pc)
{
assert(p->kind == MatchAs_kind);
if (!pc->allow_irrefutable) {
// Whoops, can't have a wildcard here!
const char *e = "wildcard makes remaining patterns unreachable";
return compiler_error(c, e);
}
ADDOP(c, POP_TOP);
ADDOP_LOAD_CONST(c, Py_True);
return 1;
}
static int
compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
{
assert(p->kind == MatchAs_kind);
if (p->v.MatchAs.name == NULL) {
return compiler_pattern_wildcard(c, p, pc);
}
if (p->v.MatchAs.pattern == NULL) {
// An irrefutable match:
if (!pc->allow_irrefutable) {
// Whoops, can't have a name capture here!
const char *e = "name capture %R makes remaining patterns unreachable";
return compiler_error(c, e, p->v.MatchAs.name);
if (p->v.MatchAs.name) {
const char *e = "name capture %R makes remaining patterns unreachable";
return compiler_error(c, e, p->v.MatchAs.name);
}
const char *e = "wildcard makes remaining patterns unreachable";
return compiler_error(c, e);
}
return compiler_pattern_capture(c, p->v.MatchAs.name, pc);
RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
ADDOP_LOAD_CONST(c, Py_True);
return 1;
}
basicblock *end, *fail_pop_1;
RETURN_IF_FALSE(end = compiler_new_block(c));
@ -5842,12 +5827,9 @@ static int
compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
{
assert(p->kind == MatchStar_kind);
if (!pc->allow_irrefutable) {
// Whoops, can't have a star capture here!
const char *e = "star captures are only allowed as part of sequence patterns";
return compiler_error(c, e);
}
return compiler_pattern_capture(c, p->v.MatchStar.name, pc);
RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc));
ADDOP_LOAD_CONST(c, Py_True);
return 1;
}
static int
@ -6206,7 +6188,7 @@ compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
}
static int
compiler_pattern_constant(struct compiler *c, pattern_ty p, pattern_context *pc)
compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
{
assert(p->kind == MatchSingleton_kind);
ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
@ -6222,7 +6204,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
case MatchValue_kind:
return compiler_pattern_value(c, p, pc);
case MatchSingleton_kind:
return compiler_pattern_constant(c, p, pc);
return compiler_pattern_singleton(c, p, pc);
case MatchSequence_kind:
return compiler_pattern_sequence(c, p, pc);
case MatchMapping_kind: