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

BoundsError in sort! on ill-behaved user-specified comparisons #11429

Closed
rened opened this issue May 24, 2015 · 10 comments · Fixed by #45222
Closed

BoundsError in sort! on ill-behaved user-specified comparisons #11429

rened opened this issue May 24, 2015 · 10 comments · Fixed by #45222
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior sorting Put things in order

Comments

@rened
Copy link
Member

rened commented May 24, 2015

The following reduced example triggers a BoundsError in both 0.3.8 and latest master:

a = rand(1:5, 2, 1000)
ind = collect(1:size(a,2))
sort!(ind, lt = (x,y) -> a[1,x]<=a[1,y])
ERROR: LoadError: BoundsError: attempt to access 2x1000 Array{Int64,2}:
 2  5  5  3  5  2  2  5  5  3  4  3  2    3  1  1  2  5  4  1  4  5  1  4  3
 2  4  2  5  4  4  2  2  3  5  4  5  5     3  3  2  4  5  5  2  1  1  5  4  2
  at index [1,140649597000272]
 in getindex at array.jl:299
 in anonymous at /Users/rene/BTSync/code/git/sortrows/test.jl:3
 in sort! at sort.jl:297
 in sort! at sort.jl:306
 in sort! at sort.jl:359
 in include at ./boot.jl:252
 in include_from_node1 at loading.jl:133
 in process_options at ./client.jl:310
 in _start at ./client.jl:409
while loading /Users/rene/BTSync/code/git/sortrows/test.jl, in expression starting on line 3
@carnaval
Copy link
Contributor

Running with --check-bounds=yes says it's a logic error in the quicksort thing.

@garrison garrison added the bug Indicates an unexpected problem or unintended behavior label May 24, 2015
@ViralBShah ViralBShah added this to the 0.4.0 milestone May 25, 2015
@ViralBShah ViralBShah added the priority This should be addressed urgently label May 25, 2015
@ViralBShah
Copy link
Member

Cc: @StefanKarpinski @kmsquire

@malmaud
Copy link
Contributor

malmaud commented May 25, 2015

Worth keeping in mind is the lt argument passed in this example is actually "less than or equal", which violations the algorithm's reasonable implicit assumption that if lt(x,y), then !lt(y,x).

@rened
Copy link
Member Author

rened commented May 25, 2015

@malmaud great catch! this was exactly the problem, thanks a lot! it works just fine with < instead of <=.

What's the best way of helping the user here? Make the docs more explicit?

@JeffBezanson
Copy link
Member

I'm not sure it's possible to make the algorithm robust against a comparison that's not a strict total order. We might need a separate function with bounds checks for user-defined orders.

@StefanKarpinski
Copy link
Member

Of course, you can get the same kind of problem by sorting with isless on a user-defined type, so that doesn't solve the problem either. I feel like we may need to make this a "user beware" sort of situation.

@malmaud
Copy link
Contributor

malmaud commented May 28, 2015

Shall we just add something to the sort! doc and close this?

@StefanKarpinski
Copy link
Member

For now I think that's best. Do we have a label for closed issues that should maybe be revisited one day?

@JeffBezanson JeffBezanson removed the priority This should be addressed urgently label Jun 2, 2015
@JeffBezanson JeffBezanson removed this from the 0.4.0 milestone Jun 2, 2015
@JeffBezanson JeffBezanson added docs This change adds or pertains to documentation and removed bug Indicates an unexpected problem or unintended behavior labels Jun 2, 2015
@JeffBezanson JeffBezanson changed the title BoundsError in sort! BoundsError in sort! on ill-behaved user-specified comparisons Jun 2, 2015
@StefanKarpinski StefanKarpinski added help wanted Indicates that a maintainer wants help on an issue or pull request good first issue Indicates a good issue for first-time contributors to Julia labels Aug 24, 2016
@StefanKarpinski StefanKarpinski added this to the 0.5.x milestone Aug 24, 2016
@kshyatt kshyatt added the Hacktoberfest Good for Hacktoberfest participants label Oct 5, 2016
@StefanKarpinski StefanKarpinski added help wanted Indicates that a maintainer wants help on an issue or pull request and removed help wanted Indicates that a maintainer wants help on an issue or pull request labels Oct 27, 2016
@JeffBezanson JeffBezanson removed the Hacktoberfest Good for Hacktoberfest participants label May 21, 2018
@JeffBezanson JeffBezanson removed this from the 0.5.x milestone May 21, 2018
@JeffBezanson JeffBezanson added the arrays [a, r, r, a, y, s] label May 21, 2018
@KristofferC KristofferC added the bug Indicates an unexpected problem or unintended behavior label Mar 22, 2022
@KristofferC
Copy link
Member

I but back the bug label because the exposed functionality of Julia should not crash like this from erroneous usage.

LilithHafner pushed a commit to LilithHafner/julia that referenced this issue May 8, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this issue May 8, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this issue May 10, 2022
@LilithHafner LilithHafner added sorting Put things in order and removed docs This change adds or pertains to documentation help wanted Indicates that a maintainer wants help on an issue or pull request good first issue Indicates a good issue for first-time contributors to Julia labels Jul 19, 2022
@LilithHafner
Copy link
Member

If it's a bug, then the fix is not documenting it.

LilithHafner added a commit that referenced this issue Oct 15, 2022
* Change partitioning scheme to use scratch space

* Randomize pivot selection with a hash-based fallback for when `rand` is unavailable

* remove an unnecessary sorting operation in typealias construction in base/show.jl

* Seed rng before generating precompile statements

* Add presorted check to avoid performance regressions

* test invalid `lt` to close #11429 & #32675

* test that PartialQuickSort is stable

* update radix sort dispatch heuristics because quicksort is now faster and the primary competition

Co-authored-by: Petr Vana <[email protected]>
Co-authored-by: Oscar Smith <[email protected]>
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 sorting Put things in order
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants