Skip to content

Commit

Permalink
sql: properly check result of decimal parsing
Browse files Browse the repository at this point in the history
This patch fixes a crash that can occur when SQL parses a decimal
literal that represents a number greater than or equal to 10^38.

Closes tarantool#9469

NO_DOC=bugfix
  • Loading branch information
ImeevMA committed Dec 12, 2023
1 parent 59b817e commit 1fe1521
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelogs/unreleased/gh-9469-too-big-decimal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## bugfix/sql

* Fixed a crash when a decimal literal represented a decimal number greater
than or equal to 10^38 is parsed in SQL (gh-9469).
1 change: 1 addition & 0 deletions src/box/errcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ struct errcode_record {
/*274 */_(ER_UNCONFIGURED, "Please call box.cfg{} first") \
/*275 */_(ER_CREATE_DEFAULT_FUNC, "Failed to create field default function '%s': %s") \
/*276 */_(ER_DEFAULT_FUNC_FAILED, "Error calling field default function '%s': %s") \
/*277 */_(ER_INVALID_DEC, "Invalid decimal: '%s'") \

/*
* !IMPORTANT! Please follow instructions at start of the file
Expand Down
6 changes: 5 additions & 1 deletion src/box/sql/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,11 @@ sql_add_term_default(struct Parse *parser, struct ExprSpan *expr_span)
const char *str = tt_cstr(expr_span->zStart,
expr_span->zEnd - expr_span->zStart);
decimal_t val;
decimal_from_string(&val, str);
if (decimal_from_string(&val, str) == NULL) {
diag_set(ClientError, ER_INVALID_DEC, str);
parser->is_aborted = true;
break;
}
size = mp_sizeof_decimal(&val);
buf = xregion_alloc(region, size);
mp_encode_decimal(buf, &val);
Expand Down
1 change: 1 addition & 0 deletions src/box/sql/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -3159,6 +3159,7 @@ expr_code_dec(struct Parse *parser, struct Expr *expr, bool is_neg, int reg)
return;
error:
sql_xfree(value);
diag_set(ClientError, ER_INVALID_DEC, str);
parser->is_aborted = true;
}

Expand Down
34 changes: 34 additions & 0 deletions test/sql-luatest/gh_9469_too_big_decimals_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local server = require('luatest.server')
local t = require('luatest')

local g = t.group()

g.before_all(function()
g.server = server:new({alias = 'master'})
g.server:start()
end)

g.after_all(function()
g.server:stop()
end)

g.test_exists = function()
g.server:exec(function()
local dec = '111111111111111111111111111111111111111.0'
local sql = ([[SELECT %s;]]):format(dec)
local exp = ([[Invalid decimal: '%s']]):format(dec)
local res, err = box.execute(sql)
t.assert(res == nil)
t.assert_equals(err.message, exp)

sql = ([[SELECT -%s;]]):format(dec)
res, err = box.execute(sql)
t.assert(res == nil)
t.assert_equals(err.message, exp)

sql = ([[CREATE TABLE T(i DEC PRIMARY KEY DEFAULT(%s);]]):format(dec)
res, err = box.execute(sql)
t.assert(res == nil)
t.assert_equals(err.message, exp)
end)
end

0 comments on commit 1fe1521

Please sign in to comment.