REBOL3 tracker
  0.9.12 beta
Ticket #0001514 User: anonymous

Project:



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

Summary TRY/except ignores the typespec of its function argument
Description When you pass a function as the exception handler of TRY/except, that function should have one regular argument, which will be the error that is being handled. However, you don't have to specify a typespec for that argument despite the fact that normally arguments without typespecs don't accept error! values. This is because TRY/except doesn't do the type testing that a normal function call does - it passes in the error! argument anyways.

For that matter, the function doesn't need to take an argument at all, or can take multiple arguments: APPLY/only semantics are used for the function arguments, so none is passed to any additional arguments. This wouldn't necessarily be a problem, but when combined with the lack of type checking it gives us an easy way to crash R3.

The advantage to the lack of type checking is that we don't need to write a verbose type spec in our handlers. The disadvantage is that you can easily crash R3 if you pass the wrong native function as a handler.

It would be a good idea to at least do the type checking when calling native functions, including natives, actions, ops and commands. You can't crash R3 with a regular function or closure in this case, so the type checking isn't as necessary. It might not be a good idea to disallow native functions altogether, but that could be an acceptable alternative to selective type checking if necessary.
Example code
; Some legit uses:
>> try/except [1 / 0] func [e] [print mold e]
make error! [
    code: 400
    type: 'Math
    id: 'zero-divide
    arg1: none
    arg2: none
    arg3: none
    near: [/ 0]
    where: [/ try]
]
>> try/except [1 / 0] func [e] [type? e]
== error!
; Note that the lack of typespec for the argument may not be technically correct, but looks better.

; Iffy but acceptable examples (don't crash)
>> try/except [1 / 0] func [e [integer!]] [type? e]
== error!
>> try/except [1 / 0] :load
** Script error: file-type? does not allow error! for its file argument
** Where: case applier try
** Near: case [
    block? source [
        return map-each x source ...
; Note that LOAD errors out on the inside, but doesn't crash R3

; Some correct and semi-useful natives that accept error args
>> try/except [1 / 0] :mold
== {make error! [
    code: 400
    type: 'Math
    id: 'zero-divide
    arg1: none
    arg2: none
    arg3: none
    near: [/ 0]
    where: [/ try]
]}
>> try/except [1 / 0] :form
== {** Math error: attempt to divide by zero
** Where: / try
** Near: / 0
}
>> try/except [1 / 0] :print
** Math error: attempt to divide by zero
** Where: / try
** Near: / 0
; If natives were excluded then these wouldn't work, but that might not be too bad.

; And here is the problem: A native that doesn't accept errors.
>> try/except [1 / 0] :add
; R3 crashes without even a system exception, the process just dies.

Assigned ton/a Fixed in- Last Update17-Apr-2015 11:33


Comments
(0004375)
abolka
23-Mar-2014 15:10

In the core-tests suite.
(0004489)
szeng
27-Aug-2014 03:08

"despite the fact that normally arguments without typespecs don't accept error! values"
This doesn't seem to be true (any more)

>> a: make error! [type: 'user id: 'message arg1: "hello"]
** user error: "hello"

>> ?? a
a: make error! [
code: 800
type: 'user
id: 'message
arg1: "hello"
arg2: none
arg3: none
near: none
where: none
]
** user error: "hello"
>> c: func [a][if error? a [print "a:" a]]
>> c a
a:
** user error: "hello"
(0004490)
szeng
27-Aug-2014 03:47

I think we should check the type of the argument as we do for other functions.
(0004491)
abolka
27-Aug-2014 03:54

> I think we should check the type of the argument as we do for other functions.

Agreed. I have a fix for that implemented which I'll push in a minute.
(0004492)
abolka
27-Aug-2014 03:58

Pull request submitted:
https://github.com/rebol/rebol/pull/227
(0004493)
szeng
27-Aug-2014 04:13

I was working on a similar fix. But yours is back compatiable, mine was more strictive on the number of arguments.
For the reference:
https://gist.github.com/zsx/9c8628db9bcded698b5a

Date User Field Action Change
17-Apr-2015 11:33 abolka Status Modified submitted => pending
27-Aug-2014 04:13 szeng Comment : 0004493 Added -
27-Aug-2014 03:58 abolka Comment : 0004492 Added -
27-Aug-2014 03:55 abolka Comment : 0004491 Modified -
27-Aug-2014 03:54 abolka Comment : 0004491 Added -
27-Aug-2014 03:47 szeng Comment : 0004490 Added -
27-Aug-2014 03:08 szeng Comment : 0004489 Added -
23-Mar-2014 15:10 abolka Comment : 0004375 Added -
9-Mar-2010 11:45 BrianH Description Modified -
9-Mar-2010 11:45 BrianH Code Modified -
9-Mar-2010 11:43 BrianH Ticket Added -