Skip to content

Commit 516067b

Browse files
authored
improve checking of Vararg parameters (#32056)
- make sure length var has full bounds - check for negative length earlier (inside Vararg, not just Tuple{Vararg{}})
1 parent 62a0a3f commit 516067b

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/jltypes.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,8 +1116,17 @@ static jl_value_t *inst_datatype_inner(jl_datatype_t *dt, jl_svec_t *p, jl_value
11161116

11171117
if (!istuple) {
11181118
if (jl_is_vararg_type((jl_value_t*)dt) && ntp == 2) {
1119-
if (!jl_is_long(iparams[1]) && !jl_is_typevar(iparams[1])) {
1120-
jl_type_error_rt("Vararg", "count", (jl_value_t*)jl_long_type, iparams[1]);
1119+
jl_value_t *lenparam = iparams[1];
1120+
if (jl_is_typevar(lenparam)) {
1121+
jl_tvar_t *N = (jl_tvar_t*)lenparam;
1122+
if (!(N->lb == jl_bottom_type && N->ub == (jl_value_t*)jl_any_type))
1123+
jl_error("TypeVar in Vararg length must have bounds Union{} and Any");
1124+
}
1125+
else if (!jl_is_long(lenparam)) {
1126+
jl_type_error_rt("Vararg", "count", (jl_value_t*)jl_long_type, lenparam);
1127+
}
1128+
else if (jl_unbox_long(lenparam) < 0) {
1129+
jl_errorf("Vararg length is negative: %zd", jl_unbox_long(lenparam));
11211130
}
11221131
}
11231132
// check parameters against bounds in type definition
@@ -1159,8 +1168,7 @@ static jl_value_t *inst_datatype_inner(jl_datatype_t *dt, jl_svec_t *p, jl_value
11591168
}
11601169
if (jl_is_long(va1)) {
11611170
ssize_t nt = jl_unbox_long(va1);
1162-
if (nt < 0)
1163-
jl_errorf("apply_type: Vararg length N is negative: %zd", nt);
1171+
assert(nt >= 0);
11641172
if (nt == 0 || !jl_has_free_typevars(va0)) {
11651173
if (cacheable) JL_UNLOCK(&typecache_lock); // Might GC
11661174
if (ntp == 1) {

test/core.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3488,6 +3488,11 @@ end
34883488
@test_throws ErrorException NTuple{-1, Int}
34893489
@test_throws TypeError Union{Int, 1}
34903490

3491+
@test_throws ErrorException Vararg{Any,-2}
3492+
@test_throws ErrorException Vararg{Int, N} where N<:T where T
3493+
@test_throws ErrorException Vararg{Int, N} where N<:Integer
3494+
@test_throws ErrorException Vararg{Int, N} where N>:Integer
3495+
34913496
mutable struct FooNTuple{N}
34923497
z::Tuple{Integer, Vararg{Int, N}}
34933498
end

0 commit comments

Comments
 (0)