From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit 2/2] Fix edge cases when recording string.byte/sub.
Date: Fri, 6 Mar 2026 16:42:55 +0300 [thread overview]
Message-ID: <db6e2e30b34c1c43c9fcd51c1f54a3741a75c717.1772803880.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1772803880.git.skaplun@tarantool.org>
From: Mike Pall <mike>
Thanks to Sergey Kaplun.
(cherry picked from commit 89f268b3f745dba80da6350d3cbbb0964f3fdbee)
It is possible that the `len` (`end - start`) will underflow and become
positive in the `recff_string_range()` when the `end` is negative. For
`string.sub()` this is not crucial, since the trace will be valid
anyway. But for `string.byte()` it may lead to the assertion failure in
the `rec_check_slots()`.
This patch fixes those underflows by the correct comparison.
Sergey Kaplun:
* added the description and the test for the problem
Part of tarantool/tarantool#12134
---
src/lj_ffrecord.c | 6 ++---
.../lj-1443-stirng-byte-underflow.test.lua | 25 +++++++++++++++++++
2 files changed, 28 insertions(+), 3 deletions(-)
create mode 100644 test/tarantool-tests/lj-1443-stirng-byte-underflow.test.lua
diff --git a/src/lj_ffrecord.c b/src/lj_ffrecord.c
index d888e83e..aad1bd87 100644
--- a/src/lj_ffrecord.c
+++ b/src/lj_ffrecord.c
@@ -810,7 +810,7 @@ static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd)
}
trstart = recff_string_start(J, str, &start, trstart, trlen, tr0);
if (rd->data) { /* Return string.sub result. */
- if (end - start >= 0) {
+ if (start <= end) {
/* Also handle empty range here, to avoid extra traces. */
TRef trptr, trslen = emitir(IRTGI(IR_SUBOV), trend, trstart);
emitir(IRTGI(IR_GE), trslen, tr0);
@@ -821,8 +821,8 @@ static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd)
J->base[0] = lj_ir_kstr(J, &J2G(J)->strempty);
}
} else { /* Return string.byte result(s). */
- ptrdiff_t i, len = end - start;
- if (len > 0) {
+ if (start < end) {
+ ptrdiff_t i, len = end - start;
TRef trslen = emitir(IRTGI(IR_SUBOV), trend, trstart);
emitir(IRTGI(IR_EQ), trslen, lj_ir_kint(J, (int32_t)len));
if (J->baseslot + len > LJ_MAX_JSLOTS)
diff --git a/test/tarantool-tests/lj-1443-stirng-byte-underflow.test.lua b/test/tarantool-tests/lj-1443-stirng-byte-underflow.test.lua
new file mode 100644
index 00000000..9f91718c
--- /dev/null
+++ b/test/tarantool-tests/lj-1443-stirng-byte-underflow.test.lua
@@ -0,0 +1,25 @@
+local tap = require('tap')
+
+-- The test file to demonstrate integer underflow during recording
+-- for the `string.byte()` built-in.
+-- See also https://github.com/LuaJIT/LuaJIT/issues/1443.
+
+local test = tap.test('lj-1443-stirng-byte-underflow'):skipcond({
+ ['Test requires JIT enabled'] = not jit.status(),
+})
+
+test:plan(1)
+
+jit.opt.start('hotloop=1')
+
+local result
+local str = 'xxx'
+for _ = 1, 4 do
+ -- Failed assertion in `rec_check_slots()` due to incorrect
+ -- number of results after underflow.
+ result = (str):byte(0X7FFFFFFF, -0X7FFFFFFF)
+end
+
+test:is(result, nil, 'correct result on trace')
+
+test:done(true)
--
2.53.0
prev parent reply other threads:[~2026-03-06 13:43 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-06 13:42 [Tarantool-patches] [PATCH luajit 0/2] Fixes for recording of string built-ins Sergey Kaplun via Tarantool-patches
2026-03-06 13:42 ` [Tarantool-patches] [PATCH luajit 1/2] Fix edge cases when generating IR for string.byte/sub/find Sergey Kaplun via Tarantool-patches
2026-03-06 13:42 ` Sergey Kaplun via Tarantool-patches [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=db6e2e30b34c1c43c9fcd51c1f54a3741a75c717.1772803880.git.skaplun@tarantool.org \
--to=tarantool-patches@dev.tarantool.org \
--cc=sergeyb@tarantool.org \
--cc=skaplun@tarantool.org \
--subject='Re: [Tarantool-patches] [PATCH luajit 2/2] Fix edge cases when recording string.byte/sub.' \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox