Skip to content

Commit 709938a

Browse files
Gumixsergepetrenko
authored andcommitted
box/lua: don't mask error object methods by payload fields
With commit eb2c6a4 ("box/lua: allow to set custom error payload fields") it is possible to add a field named "raise", but it will override the `error_object:raise()' method. Forbid it. The user can still access masked payload fields through the `unpack()' method. NO_DOC=unreleased NO_CHANGELOG=unreleased
1 parent 0e86fbd commit 709938a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/lua/error.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ local error_methods = {
174174
}
175175

176176
local function error_index(err, key)
177+
local method = error_methods[key]
178+
if method ~= nil then
179+
return method
180+
end
177181
local getter = error_fields[key]
178182
if getter ~= nil then
179183
return getter(err)
@@ -182,7 +186,6 @@ local function error_index(err, key)
182186
if f ~= nil then
183187
return mp_decode(f._data)
184188
end
185-
return error_methods[key]
186189
end
187190

188191
local function error_concat(lhs, rhs)

test/box-luatest/error_subsystem_improvements_test.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,24 @@ g.test_error_new_message_arg = function()
106106
t.assert_equals(box.error.new{message = msg, reason = 'other'}.message, msg)
107107
t.assert_equals(box.error.new{msg, reason = 'other'}.message, msg)
108108
end
109+
110+
-- Test that error's methods are not masked by the payload fields.
111+
g.test_methods_masking = function()
112+
local e = box.error.new({
113+
unpack = 'hack',
114+
raise = 'hack',
115+
match = 'hack',
116+
set_prev = 'hack',
117+
__serialize = 'hack',
118+
})
119+
t.assert_type(e.unpack, 'function')
120+
t.assert_type(e.raise, 'function')
121+
t.assert_type(e.match, 'function')
122+
t.assert_type(e.set_prev, 'function')
123+
t.assert_type(e.__serialize, 'function')
124+
t.assert_equals(e:unpack().unpack, 'hack')
125+
t.assert_equals(e:unpack().raise, 'hack')
126+
t.assert_equals(e:unpack().match, 'hack')
127+
t.assert_equals(e:unpack().set_prev, 'hack')
128+
t.assert_equals(e:unpack().__serialize, 'hack')
129+
end

0 commit comments

Comments
 (0)