REBOL3 tracker
  0.9.12 beta
Ticket #0001516 User: anonymous

Project:



rss
TypeBug Statuspending Date9-Mar-2010 13:23
Versionalpha 97 CategoryNative Submitted byBrianH
PlatformAll Severitycrash Prioritynormal

Summary SORT/compare ignores the typespec of its function argument
Description When you pass a function as the comparator of SORT/compare, that function should have two regular arguments, which will be values that are being compared. However, SORT/compare of a block containing error! or unset! values will still sort if you don't specify a typespec for those arguments, despite the fact that normally arguments without typespecs don't accept error! or unset! values. This is because SORT/compare doesn't do the type testing that a normal function call does - it passes in the arguments anyways. For that matter, the function doesn't need to take an argument at all, or can take more than two arguments: APPLY/only semantics are used for the function call, so none is passed to any additional arguments.

The main advantage to the lack of type checking is speed, plus we don't need to write a verbose type spec in our function! comparators. The disadvantage is that you can easily crash R3 if you pass any native function that would make sense to use as a comparator.

We need to add type checking to the function call, at least for natives. You can't crash R3 with function! or closure! code because of this, so it might be acceptable to keep the current behavior for those types. We don't want to exclude natives, actions, ops and commands altogether because they are too useful in this case.

Note that this bug wouldn't apply to SORT/compare of strings or binaries (even if that worked: see #1100). It just applies to any-block! types.
Example code
>> sort/compare [1 2 3] func [a b] [a > b]
== [3 2 1]

>> sort/compare [1 2 #[unset!]] func [a b] [a > b]
** Script error: b has no value
** Where: applier sort
** Near: a > b
; Note that the unset! got into the function, but triggered an error
; during normal evaluation. This would be acceptable.

>> sort/compare [1 2 3] :>
== [3 2 1]

>> sort/compare [1 2 #[unset!]] :>
; The R3 process crashes at this point without a system exception

Assigned ton/a Fixed in- Last Update17-Apr-2015 06:18


Comments
(0004378)
abolka
23-Mar-2014 16:21

In the core-tests suite.
(0004495)
szeng
28-Aug-2014 00:24

Fixed by:
https://github.com/zsx/r3/commit/e23ae96beecf5612bbcc0da580415498c896416a
(0004496)
szeng
28-Aug-2014 01:16

An updated fix:
https://github.com/zsx/r3/commit/cbfebfeded7f32056afdb35f6a748603d0d5af57
(0004636)
abolka
17-Apr-2015 06:17

Wrapped up a basically identical fix in a PR:
https://github.com/rebol/rebol/pull/239

Date User Field Action Change
17-Apr-2015 06:18 abolka Comment : 0004636 Modified -
17-Apr-2015 06:18 abolka Status Modified submitted => pending
17-Apr-2015 06:17 abolka Comment : 0004636 Added -
28-Aug-2014 03:36 szeng Comment : 0004496 Modified -
28-Aug-2014 01:16 szeng Comment : 0004496 Modified -
28-Aug-2014 01:16 szeng Comment : 0004496 Added -
28-Aug-2014 00:24 szeng Comment : 0004495 Added -
23-Mar-2014 16:21 abolka Comment : 0004378 Added -
20-Feb-2014 00:27 BrianH Description Modified -
20-Feb-2014 00:27 BrianH Code Modified -
9-Mar-2010 13:23 BrianH Ticket Added -