Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Bronnikov <sergeyb@tarantool.org>,
	Evgeniy Temirgaleev <e.temirgaleev@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit] Add compatibility string coercion for fp:seek() argument.
Date: Tue, 21 Jul 2026 10:59:39 +0300	[thread overview]
Message-ID: <20260721075939.2894880-1-skaplun@tarantool.org> (raw)

From: Mike Pall <mike>

Reported by Magnus Wibeck.

(cherry picked from commit d508715ab657261e8437a52c1b1966c20ab1631d)

In LuaJIT the second `fp:seek()` argument (offset) doesn't coerce from a
string to an integer value, unlike PUC-Rio Lua 5.1 does.

This patch adds missing coercion.

Sergey Kaplun:
* added the description and the test for the problem

Part of tarantool/tarantool#12880
---

Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1343-fseek-offset-coercion
Related issues:
* https://github.com/LuaJIT/LuaJIT/issues/1343
* https://github.com/tarantool/tarantool/issues/12880

 src/Makefile.dep.original                     |  2 +-
 src/lib_io.c                                  |  4 +++-
 .../lj-1343-fseek-offset-coercion.test.lua    | 23 +++++++++++++++++++
 3 files changed, 27 insertions(+), 2 deletions(-)
 create mode 100644 test/tarantool-tests/lj-1343-fseek-offset-coercion.test.lua

diff --git a/src/Makefile.dep.original b/src/Makefile.dep.original
index 436685f5..acc6b3ba 100644
--- a/src/Makefile.dep.original
+++ b/src/Makefile.dep.original
@@ -21,7 +21,7 @@ lib_ffi.o: lib_ffi.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
 lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lmisclib.h lj_arch.h
 lib_io.o: lib_io.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
  lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_state.h \
- lj_strfmt.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h
+ lj_strfmt.h lj_ff.h lj_ffdef.h lj_lib.h lj_strscan.h lj_libdef.h
 lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
  lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h \
  lj_state.h lj_bc.h lj_ctype.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h \
diff --git a/src/lib_io.c b/src/lib_io.c
index ef39e535..2d0abf78 100644
--- a/src/lib_io.c
+++ b/src/lib_io.c
@@ -25,6 +25,7 @@
 #include "lj_strfmt.h"
 #include "lj_ff.h"
 #include "lj_lib.h"
+#include "lj_strscan.h"
 
 /* Userdata payload for I/O file. */
 typedef struct IOFileUD {
@@ -317,13 +318,14 @@ LJLIB_CF(io_method_seek)
   FILE *fp = io_tofile(L)->fp;
   int opt = lj_lib_checkopt(L, 2, 1, "\3set\3cur\3end");
   int64_t ofs = 0;
-  cTValue *o;
+  TValue *o;
   int res;
   if (opt == 0) opt = SEEK_SET;
   else if (opt == 1) opt = SEEK_CUR;
   else if (opt == 2) opt = SEEK_END;
   o = L->base+2;
   if (o < L->top) {
+    if (tvisstr(o)) lj_strscan_num(strV(o), o);
     if (tvisint(o))
       ofs = (int64_t)intV(o);
     else if (tvisnum(o))
diff --git a/test/tarantool-tests/lj-1343-fseek-offset-coercion.test.lua b/test/tarantool-tests/lj-1343-fseek-offset-coercion.test.lua
new file mode 100644
index 00000000..fdfc6ce5
--- /dev/null
+++ b/test/tarantool-tests/lj-1343-fseek-offset-coercion.test.lua
@@ -0,0 +1,23 @@
+local tap = require('tap')
+
+-- The test file to demonstrate LuaJIT's missing coercion for the
+-- `io.fseek()` offset argument.
+-- See also: https://github.com/LuaJIT/LuaJIT/issues/1343.
+
+local test = tap.test('lj-1343-fseek-offset-coercion')
+
+test:plan(2)
+
+local f = io.tmpfile()
+
+f:write('12345')
+
+f:seek('set', 0)
+f:seek('set', '1')
+test:is(f:read('*all'), '2345', 'base coercion test')
+
+f:seek('set', 0)
+f:seek('set', '1.9')
+test:is(f:read('*all'), '2345', 'coercion test for non-integer value')
+
+test:done(true)
-- 
2.55.0


                 reply	other threads:[~2026-07-21  7:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260721075939.2894880-1-skaplun@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=e.temirgaleev@tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit] Add compatibility string coercion for fp:seek() argument.' \
    /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