Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inconsistent errors with negative dimensions in Array constructor #28597

Closed
rfourquet opened this issue Aug 11, 2018 · 2 comments
Closed

inconsistent errors with negative dimensions in Array constructor #28597

rfourquet opened this issue Aug 11, 2018 · 2 comments
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior

Comments

@rfourquet
Copy link
Member

This was reported on slack:

julia> Array{Int, 2}(undef, 0, -10)
0×0 Array{Int64,2}

julia> Array{Int, 2}(undef, -10, 0)
ERROR: invalid Array dimensions
Stacktrace:
 [1] Array{Int64,2}(::UndefInitializer, ::Int64, ::Int64) at ./boot.jl:396
 [2] top-level scope at none:0

This comes from the _new_array_ function in "array.c", which computes the product of the dimensions: if 0 comes first, then the product is 0 and doesn't grow out of bounds.

@rfourquet rfourquet added the arrays [a, r, r, a, y, s] label Aug 11, 2018
@StefanKarpinski
Copy link
Member

Ah, good catch!

@StefanKarpinski StefanKarpinski added the bug Indicates an unexpected problem or unintended behavior label Aug 14, 2018
@StefanKarpinski StefanKarpinski added this to the 1.0.x milestone Aug 14, 2018
@andreasnoack
Copy link
Member

There is some int/uint conversion stuff going on here. The array allocators's size argument is size_t, see e.g.

JL_DLLEXPORT jl_array_t *jl_alloc_array_1d(jl_value_t *atype, size_t nr)
but we pass the value as an Int, see

julia/base/boot.jl

Lines 394 to 395 in 3fe2d08

Array{T,1}(::UndefInitializer, m::Int) where {T} =
ccall(:jl_alloc_array_1d, Array{T,1}, (Any, Int), Array{T,1}, m)

so e.g. -1 becomes a very large number which errors in

julia/src/array.c

Lines 63 to 68 in 3fe2d08

for(i=0; i < ndims; i++) {
wideint_t prod = (wideint_t)nel * (wideint_t)dims[i];
if (prod > (wideint_t) MAXINTVAL)
jl_error("invalid Array dimensions");
nel = prod;
}
unless the previous size was zero, in which case prod becomes zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants