Skip to content

Commit

Permalink
Check type of argument before call comparator in sort
Browse files Browse the repository at this point in the history
This fixes CC#1516

>> sort/compare [1 2 #[unset!]] :>
; The R3 process crashes at this point without a system exception
  • Loading branch information
zsx committed Aug 27, 2014
1 parent 750cb0f commit cbfebfe
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/core/t-block.c
Expand Up @@ -387,11 +387,27 @@ static struct {
***********************************************************************/
{
REBVAL *val;

REBVAL *args = NULL;

const void *tmp = NULL;

if (!sort_flags.reverse) { /*swap v1 and v2 */
tmp = v1;
v1 = v2;
v2 = tmp;
}

args = BLK_SKIP(VAL_FUNC_ARGS(sort_flags.compare), 1);
if (NOT_END(args) && !TYPE_CHECK(args, VAL_TYPE((REBVAL*)v1))){
Trap3(RE_EXPECT_ARG, Of_Type(sort_flags.compare), args, Of_Type((REBVAL*)v1));
}
++ args;
if (NOT_END(args) && !TYPE_CHECK(args, VAL_TYPE((REBVAL*)v2))) {
Trap3(RE_EXPECT_ARG, Of_Type(sort_flags.compare), args, Of_Type((REBVAL*)v2));
}

if (sort_flags.reverse)
val = Apply_Func(0, sort_flags.compare, v1, v2, 0);
else
val = Apply_Func(0, sort_flags.compare, v2, v1, 0);
val = Apply_Func(0, sort_flags.compare, v1, v2, 0);

if (IS_LOGIC(val)) {
if (IS_TRUE(val)) return 1;
Expand Down

0 comments on commit cbfebfe

Please sign in to comment.