Skip to content

Commit 152030f

Browse files
Nabanita DashStefanKarpinski
authored andcommitted
Throw ArgumentError for negative array dimensions and added test (#32363)
1 parent 458b449 commit 152030f

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

src/array.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims,
6464
size_t di = dims[i];
6565
wideint_t prod = (wideint_t)nel * (wideint_t)di;
6666
if (prod > (wideint_t) MAXINTVAL || di > MAXINTVAL)
67-
jl_error("invalid Array dimensions");
67+
jl_exceptionf(jl_argumenterror_type, "invalid Array dimensions");
6868
nel = prod;
6969
}
7070
assert(atype == NULL || isunion == jl_is_uniontype(jl_tparam0(atype)));
@@ -252,7 +252,7 @@ JL_DLLEXPORT jl_array_t *jl_reshape_array(jl_value_t *atype, jl_array_t *data,
252252
adims[i] = dims[i];
253253
prod = (wideint_t)l * (wideint_t)adims[i];
254254
if (prod > (wideint_t) MAXINTVAL)
255-
jl_error("invalid Array dimensions");
255+
jl_exceptionf(jl_argumenterror_type, "invalid Array dimensions");
256256
l = prod;
257257
}
258258
#ifdef STORE_ARRAY_LEN
@@ -356,7 +356,7 @@ JL_DLLEXPORT jl_array_t *jl_ptr_to_array(jl_value_t *atype, void *data,
356356
for (size_t i = 0; i < ndims; i++) {
357357
prod = (wideint_t)nel * (wideint_t)dims[i];
358358
if (prod > (wideint_t) MAXINTVAL)
359-
jl_error("invalid Array dimensions");
359+
jl_exceptionf(jl_argumenterror_type, "invalid Array dimensions");
360360
nel = prod;
361361
}
362362
if (__unlikely(ndims == 1))

stdlib/Random/test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ let mta = MersenneTwister(42), mtb = MersenneTwister(42)
485485
@test sort!(randperm(10)) == sort!(shuffle(1:10)) == 1:10
486486
@test randperm(mta,big(10)) == randperm(mtb,big(10)) # cf. #16376
487487
@test randperm(0) == []
488-
@test_throws ErrorException randperm(-1)
488+
@test_throws ArgumentError randperm(-1)
489489

490490
let p = randperm(UInt16(12))
491491
@test typeof(p) Vector{UInt16}

test/arrayops.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,3 +2631,5 @@ end
26312631
# Fix oneunit bug for unitful arrays
26322632
@test oneunit([Second(1) Second(2); Second(3) Second(4)]) == [Second(1) Second(0); Second(0) Second(1)]
26332633

2634+
# Throws ArgumentError for negative dimensions in Array
2635+
@test_throws ArgumentError fill('a', -10)

test/core.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6841,9 +6841,9 @@ end
68416841
@test repackage28445()
68426842

68436843
# issue #28597
6844-
@test_throws ErrorException Array{Int, 2}(undef, 0, -10)
6845-
@test_throws ErrorException Array{Int, 2}(undef, -10, 0)
6846-
@test_throws ErrorException Array{Int, 2}(undef, -1, -1)
6844+
@test_throws ArgumentError Array{Int, 2}(undef, 0, -10)
6845+
@test_throws ArgumentError Array{Int, 2}(undef, -10, 0)
6846+
@test_throws ArgumentError Array{Int, 2}(undef, -1, -1)
68476847

68486848
# issue #28812
68496849
@test Tuple{Vararg{Array{T},3} where T} === Tuple{Array,Array,Array}

0 commit comments

Comments
 (0)