Skip to content
Snippets Groups Projects
Commit ca79e1cf authored by Nikita Pettik's avatar Nikita Pettik
Browse files

box/error: don't allow overflow of error ref counter

There's no overflow check while incrementing error's reference counter
in error_ref(). Meanwhile, stubborn users still may achieve overflow:
each call of box.error.last() increments reference counter of error
residing in diagnostic area. As a result, 2^32 calls of box.error.last()
in a row will lead to counter overflow ergo - to unpredictable results.
Let's fix it and introduce dummy check in error_ref().
parent c44ed3c0
No related branches found
No related tags found
No related merge requests found
......@@ -80,6 +80,7 @@ error_set_prev(struct error *e, struct error *prev)
*/
error_unlink_effect(prev);
}
assert(prev->refs < INT32_MAX);
error_ref(prev);
prev->effect = e;
}
......
......@@ -112,10 +112,14 @@ struct error {
struct error *effect;
};
static inline void
static inline int
error_ref(struct error *e)
{
assert(e->refs >= 0);
if (e->refs >= INT32_MAX)
return -1;
e->refs++;
return 0;
}
void
......@@ -230,6 +234,7 @@ static inline void
diag_set_error(struct diag *diag, struct error *e)
{
assert(e != NULL);
assert(e->refs < INT32_MAX);
error_ref(e);
diag_clear(diag);
error_unlink_effect(e);
......@@ -254,6 +259,7 @@ diag_add_error(struct diag *diag, struct error *e)
*/
assert(e->effect == NULL);
assert(diag->last->effect == NULL);
assert(e->refs < INT32_MAX);
error_ref(e);
e->cause = diag->last;
diag->last->effect = e;
......
......@@ -84,7 +84,8 @@ luaT_pusherror(struct lua_State *L, struct error *e)
* It also important to reference the error first and only
* then set the finalizer.
*/
error_ref(e);
if (error_ref(e) != 0)
luaL_error(L, "Too many references to error object");
assert(CTID_CONST_STRUCT_ERROR_REF != 0);
struct error **ptr = (struct error **)
luaL_pushcdata(L, CTID_CONST_STRUCT_ERROR_REF);
......
......@@ -129,6 +129,10 @@ local function error_set_prev(err, prev)
if not ffi.istype('struct error', prev) and prev ~= nil then
error("Usage: error1:set_prev(error2)")
end
local INT32_MAX = 2147483647
if err._refs >= INT32_MAX then
error("Too many references to error object")
end
local ok = ffi.C.error_set_prev(err, prev);
if ok ~= 0 then
error("Cycles are not allowed")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment