Skip to content

map! resulting in undefined values #36235

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

Closed
goretkin opened this issue Jun 11, 2020 · 10 comments · Fixed by #56673
Closed

map! resulting in undefined values #36235

goretkin opened this issue Jun 11, 2020 · 10 comments · Fixed by #56673
Labels
bug Indicates an unexpected problem or unintended behavior correctness bug ⚠ Bugs that are likely to lead to incorrect results in user code without throwing

Comments

@goretkin
Copy link
Contributor

goretkin commented Jun 11, 2020

julia> VERSION
v"1.5.0-beta1.0"

julia> map!(+, [0,0,0], [1,2], [10,20,30], [100])
3-element Array{Int64,1}:
 111
  31
   0

julia> map!(+, [0,0,0], [1,2], [10,20,30], [100])
3-element Array{Int64,1}:
        111
 4616658790
          0

This also happened on v1.4.1

Pretty sure this commit removed the necessary bounds check: f9645ff

Probably, if @boundscheck LinearIndices(dest) == idxs1 && all(x -> LinearIndices(x) == idxs1, As) evaluates to false, then BoundsError should be thrown.

If I understand the intent correct, map!(+, [0,0,0], [1,2], [10,20,30], [100]) is supposed to error because the trailing arguments don't match the axes of the second argument. Or at the very least because the last argument is smaller than the second argument.

@JeffBezanson JeffBezanson added the bug Indicates an unexpected problem or unintended behavior label Jun 11, 2020
@jakobnissen
Copy link
Member

jakobnissen commented Jun 12, 2020

Could we do something like this instead of the existing map_n!?

function map_n!(f, dest::AbstractArray, As)
    idx = eachindex(As...)
    @boundscheck checkbounds(dest, idx)
    @inbounds for i in idx
        I = ith_all(i, As)
        val = f(I...)
        dest[i] = val
    end
    return dest
end

@goretkin
Copy link
Contributor Author

goretkin commented Jun 12, 2020

That seams reasonable. There's bound checking that happens in eachindex, too, which as far as I can tell don't get elided with @inbounds. It would be nice if they did, but that shouldn't block this bug fix.

@goretkin
Copy link
Contributor Author

goretkin commented Mar 9, 2021

I just tried the example on Julia 1.6-rc1, and I was not able to generate undefined values.

@StefanKarpinski StefanKarpinski added this to the 1.10 milestone May 5, 2023
@goretkin
Copy link
Contributor Author

julia> VERSION
v"1.8.2"

julia> map!(+, [0,0,0], [1,2], [10,20,30], [100])
3-element Vector{Int64}:
        111
 4622078430
          0

julia> map!(+, [0,0,0], [1,2], [10,20,30], [100])
3-element Vector{Int64}:
        111
 4622107614
          0

julia> map!(+, [0,0,0], [1,2], [10,20,30], [100])
3-element Vector{Int64}:
        111
 4622120478
          0

@maleadt
Copy link
Member

maleadt commented Jul 23, 2023

This also happened on 1.9, so shouldn't be on the 1.10 milestone?

@maleadt maleadt removed this from the 1.10 milestone Jul 23, 2023
@adienes
Copy link
Member

adienes commented Jul 23, 2023

@maleadt then add it to the 1.9 milestone? I don't think the right answer for a bug like this is to just forget about it for another 3 years

@maleadt
Copy link
Member

maleadt commented Jul 23, 2023

Milestones are generally used to keep track of regressions, or of bugs that should hold up a release. This bugs doesn't fit that description, so I don't think it should be on the 1.10 milestone.

As long as there's an open issue, it's not considered forgotten about though.

@adienes
Copy link
Member

adienes commented Jul 23, 2023

I think this bug (exposing undefined behavior to the user, with tools as simple as map on Arrays) should hold up the release

@maleadt
Copy link
Member

maleadt commented Jul 23, 2023

I don't think it needs to be release blocking, as the bug has been open for years without too many people noticing. I agree it's not a good look though; I'll see if this is easy to fix.

@LilithHafner LilithHafner added the correctness bug ⚠ Bugs that are likely to lead to incorrect results in user code without throwing label Nov 27, 2024
@LilithHafner
Copy link
Member

map! also does not check that the destination is large enough to accommodate the answer, resulting in segfaults when there are many args:

julia> map!(+, [], [1,2,3,4], rand(1000))
Any[]

julia> map!(+, [], [1,2,3,4], rand(1000), [])

[127897] signal 11 (1): Segmentation fault
[...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior correctness bug ⚠ Bugs that are likely to lead to incorrect results in user code without throwing
Projects
None yet
7 participants