You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
; Some legit uses:>>try/except [1/0] func [e] [printmold e]
make error! [
code:400type: '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.
"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:800type: '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"
Rebolbot commented on Aug 27, 2014:
Submitted by:szeng
I think we should check the type of the argument as we do for other functions.
Rebolbot commented on Aug 27, 2014:
Submitted by:abolka
> 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.
Rebolbot commented on Aug 27, 2014:
Submitted by:abolka
Pull request submitted:
https://github.com/rebol/rebol/pull/227
Rebolbot commented on Aug 27, 2014:
Submitted by:szeng
I was working on a similar fix. But yours is back compatiable, mine was more strictive on the number of arguments.
For the reference:
Submitted by: BrianH
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.
Imported from: CureCode [ Version: alpha 97 Type: Bug Platform: All Category: Native Reproduce: Always Fixed-in:none ]
Imported from: metaeducation#1514
Comments:
Submitted by: abolka
In the core-tests suite.
Submitted by: szeng
"despite the fact that normally arguments without typespecs don't accept error! values"
This doesn't seem to be true (any more)
Submitted by: szeng
I think we should check the type of the argument as we do for other functions.
Submitted by: abolka
Agreed. I have a fix for that implemented which I'll push in a minute.
Submitted by: abolka
Pull request submitted:
https://github.com/rebol/rebol/pull/227
Submitted by: szeng
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
The text was updated successfully, but these errors were encountered: