• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / luajit / 10558865385

26 Aug 2024 11:25AM UTC coverage: 92.759% (-0.04%) from 92.796%
10558865385

push

github

Buristan
Fix limit check in narrow_conv_backprop().

Thanks to Sergey Kaplun.

(cherry picked from commit e45fd4cb7)

`narrow_conv_backprop()` misses the stack pointer (`nc->sp`) limit check
after a bunch of recursive calls that may change its value. As a result,
it leads to stack-buffer-overflow during the instruction narrowing. This
patch adds a missing check.

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

Part of tarantool/tarantool#10199

5676 of 6025 branches covered (94.21%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

16 existing lines in 5 files now uncovered.

21647 of 23431 relevant lines covered (92.39%)

2940097.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.96
/src/luajit.c
1
/*
2
** LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc.
3
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4
**
5
** Major portions taken verbatim or adapted from the Lua interpreter.
6
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7
*/
8

9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <string.h>
12

13
#define luajit_c
14

15
#include "lua.h"
16
#include "lauxlib.h"
17
#include "lualib.h"
18
#include "luajit.h"
19

20
#include "lj_arch.h"
21

22
#if LJ_TARGET_POSIX
23
#include <unistd.h>
24
#define lua_stdin_is_tty()        isatty(0)
25
#elif LJ_TARGET_WINDOWS
26
#include <io.h>
27
#ifdef __BORLANDC__
28
#define lua_stdin_is_tty()        isatty(_fileno(stdin))
29
#else
30
#define lua_stdin_is_tty()        _isatty(_fileno(stdin))
31
#endif
32
#else
33
#define lua_stdin_is_tty()        1
34
#endif
35

36
#if !LJ_TARGET_CONSOLE
37
#include <signal.h>
38
#endif
39

40
static lua_State *globalL = NULL;
41
static const char *progname = LUA_PROGNAME;
42

43
#if !LJ_TARGET_CONSOLE
44
static void lstop(lua_State *L, lua_Debug *ar)
×
45
{
46
  (void)ar;  /* unused arg. */
×
47
  lua_sethook(L, NULL, 0, 0);
×
48
  /* Avoid luaL_error -- a C hook doesn't add an extra frame. */
49
  luaL_where(L, 0);
×
50
  lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
×
51
  lua_error(L);
×
52
}
×
53

54
static void laction(int i)
×
55
{
56
  signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
×
57
                         terminate process (default action) */
58
  lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
×
59
}
×
60
#endif
61

62
static void print_usage(void)
5✔
63
{
64
  fputs("usage: ", stderr);
5✔
65
  fputs(progname, stderr);
5✔
66
  fputs(" [options]... [script [args]...].\n"
5✔
67
  "Available options are:\n"
68
  "  -e chunk  Execute string " LUA_QL("chunk") ".\n"
69
  "  -l name   Require library " LUA_QL("name") ".\n"
70
  "  -b ...    Save or list bytecode.\n"
71
  "  -j cmd    Perform LuaJIT control command.\n"
72
  "  -O[opt]   Control LuaJIT optimizations.\n"
73
  "  -i        Enter interactive mode after executing " LUA_QL("script") ".\n"
74
  "  -v        Show version information.\n"
75
  "  -t<cmd>   Execute tool.\n"
76
  "  -E        Ignore environment variables.\n"
77
  "  --        Stop handling options.\n"
78
  "  -         Execute stdin and stop handling options.\n", stderr);
79
  fflush(stderr);
5✔
80
}
5✔
81

82
static void print_tools_usage(void)
2✔
83
{
84
  fputs("usage: ", stderr);
2✔
85
  fputs(progname, stderr);
2✔
86
  fputs(" -t<cmd>\n"
2✔
87
  "Available tools are:\n"
88
  "  -m [--leak-only] input  Memprof profile data parser.\n"
89
  "  -s input                Sysprof profile data parser.\n", stderr);
90
  fflush(stderr);
2✔
91
}
2✔
92

93
static void l_message(const char *pname, const char *msg)
7✔
94
{
95
  if (pname) { fputs(pname, stderr); fputc(':', stderr); fputc(' ', stderr); }
7✔
96
  fputs(msg, stderr); fputc('\n', stderr);
7✔
97
  fflush(stderr);
7✔
98
}
7✔
99

100
static int report(lua_State *L, int status)
613✔
101
{
102
  if (status && !lua_isnil(L, -1)) {
613✔
103
    const char *msg = lua_tostring(L, -1);
6✔
104
    if (msg == NULL) msg = "(error object is not a string)";
6✔
105
    l_message(progname, msg);
6✔
106
    lua_pop(L, 1);
6✔
107
  }
108
  return status;
613✔
109
}
110

111
static int traceback(lua_State *L)
4✔
112
{
113
  if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */
4✔
114
    if (lua_isnoneornil(L, 1) ||
2✔
115
        !luaL_callmeta(L, 1, "__tostring") ||
1✔
UNCOV
116
        !lua_isstring(L, -1))
×
117
      return 1;  /* Return non-string error object. */
1✔
UNCOV
118
    lua_remove(L, 1);  /* Replace object by result of __tostring metamethod. */
×
119
  }
120
  luaL_traceback(L, L, lua_tostring(L, 1), 1);
3✔
121
  return 1;
3✔
122
}
123

124
static int docall(lua_State *L, int narg, int clear)
609✔
125
{
126
  int status;
609✔
127
  int base = lua_gettop(L) - narg;  /* function index */
609✔
128
  lua_pushcfunction(L, traceback);  /* push traceback function */
609✔
129
  lua_insert(L, base);  /* put it under chunk and args */
609✔
130
#if !LJ_TARGET_CONSOLE
131
  signal(SIGINT, laction);
609✔
132
#endif
133
  status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
881✔
134
#if !LJ_TARGET_CONSOLE
135
  signal(SIGINT, SIG_DFL);
440✔
136
#endif
137
  lua_remove(L, base);  /* remove traceback function */
440✔
138
  /* force a complete garbage collection in case of errors */
139
  if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0);
440✔
140
  return status;
440✔
141
}
142

143
static void print_version(void)
8✔
144
{
145
  fputs(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n", stdout);
8✔
146
}
8✔
147

148
static void print_jit_status(lua_State *L)
4✔
149
{
150
  int n;
4✔
151
  const char *s;
4✔
152
  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
4✔
153
  lua_getfield(L, -1, "jit");  /* Get jit.* module table. */
4✔
154
  lua_remove(L, -2);
4✔
155
  lua_getfield(L, -1, "status");
4✔
156
  lua_remove(L, -2);
4✔
157
  n = lua_gettop(L);
4✔
158
  lua_call(L, 0, LUA_MULTRET);
4✔
159
  fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stdout);
4✔
160
  for (n++; (s = lua_tostring(L, n)); n++) {
60✔
161
    putc(' ', stdout);
56✔
162
    fputs(s, stdout);
56✔
163
  }
164
  putc('\n', stdout);
4✔
165
}
4✔
166

167
static void createargtable(lua_State *L, char **argv, int argc, int argf)
325✔
168
{
169
  int i;
325✔
170
  lua_createtable(L, argc - argf, argf);
325✔
171
  for (i = 0; i < argc; i++) {
2,093✔
172
    lua_pushstring(L, argv[i]);
1,443✔
173
    lua_rawseti(L, -2, i - argf);
1,443✔
174
  }
175
  lua_setglobal(L, "arg");
325✔
176
}
325✔
177

178
static int dofile(lua_State *L, const char *name)
1✔
179
{
180
  int status = luaL_loadfile(L, name) || docall(L, 0, 1);
1✔
181
  return report(L, status);
1✔
182
}
183

184
static int dostring(lua_State *L, const char *s, const char *name)
281✔
185
{
186
  int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
281✔
187
  return report(L, status);
278✔
188
}
189

190
static int dolibrary(lua_State *L, const char *name)
57✔
191
{
192
  lua_getglobal(L, "require");
57✔
193
  lua_pushstring(L, name);
57✔
194
  return report(L, docall(L, 1, 1));
57✔
195
}
196

197
static void write_prompt(lua_State *L, int firstline)
131✔
198
{
199
  const char *p;
131✔
200
  lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
240✔
201
  p = lua_tostring(L, -1);
131✔
202
  if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2;
131✔
203
  fputs(p, stdout);
131✔
204
  fflush(stdout);
131✔
205
  lua_pop(L, 1);  /* remove global */
131✔
206
}
131✔
207

208
static int incomplete(lua_State *L, int status)
127✔
209
{
210
  if (status == LUA_ERRSYNTAX) {
127✔
211
    size_t lmsg;
109✔
212
    const char *msg = lua_tolstring(L, -1, &lmsg);
109✔
213
    const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
109✔
214
    if (strstr(msg, LUA_QL("<eof>")) == tp) {
109✔
215
      lua_pop(L, 1);
109✔
216
      return 1;
109✔
217
    }
218
  }
219
  return 0;  /* else... */
220
}
221

222
static int pushline(lua_State *L, int firstline)
131✔
223
{
224
  char buf[LUA_MAXINPUT];
131✔
225
  write_prompt(L, firstline);
131✔
226
  if (fgets(buf, LUA_MAXINPUT, stdin)) {
131✔
227
    size_t len = strlen(buf);
127✔
228
    if (len > 0 && buf[len-1] == '\n')
127✔
229
      buf[len-1] = '\0';
125✔
230
    if (firstline && buf[0] == '=')
127✔
231
      lua_pushfstring(L, "return %s", buf+1);
5✔
232
    else
233
      lua_pushstring(L, buf);
122✔
234
    return 1;
127✔
235
  }
236
  return 0;
237
}
238

239
static int loadline(lua_State *L)
22✔
240
{
241
  int status;
22✔
242
  lua_settop(L, 0);
22✔
243
  if (!pushline(L, 1))
22✔
244
    return -1;  /* no input */
245
  for (;;) {  /* repeat until gets a complete line */
236✔
246
    status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
127✔
247
    if (!incomplete(L, status)) break;  /* cannot try to add lines? */
127✔
248
    if (!pushline(L, 0))  /* no more input? */
109✔
249
      return -1;
250
    lua_pushliteral(L, "\n");  /* add a new line... */
109✔
251
    lua_insert(L, -2);  /* ...between the two lines */
109✔
252
    lua_concat(L, 3);  /* join them */
109✔
253
  }
254
  lua_remove(L, 1);  /* remove line */
18✔
255
  return status;
18✔
256
}
257

258
static void dotty(lua_State *L)
4✔
259
{
260
  int status;
4✔
261
  const char *oldprogname = progname;
4✔
262
  progname = NULL;
4✔
263
  while ((status = loadline(L)) != -1) {
26✔
264
    if (status == LUA_OK) status = docall(L, 0, 0);
18✔
265
    report(L, status);
18✔
266
    if (status == LUA_OK && lua_gettop(L) > 0) {  /* any result to print? */
18✔
267
      lua_getglobal(L, "print");
5✔
268
      lua_insert(L, 1);
5✔
269
      if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
5✔
270
        l_message(progname,
×
271
          lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
272
                              lua_tostring(L, -1)));
273
    }
274
  }
275
  lua_settop(L, 0);  /* clear stack */
4✔
276
  fputs("\n", stdout);
4✔
277
  fflush(stdout);
4✔
278
  progname = oldprogname;
4✔
279
}
4✔
280

281
static int handle_script(lua_State *L, char **argx)
255✔
282
{
283
  int status;
255✔
284
  const char *fname = argx[0];
255✔
285
  if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0)
255✔
286
    fname = NULL;  /* stdin */
4✔
287
  status = luaL_loadfile(L, fname);
255✔
288
  if (status == LUA_OK) {
255✔
289
    /* Fetch args from arg table. LUA_INIT or -e might have changed them. */
290
    int narg = 0;
254✔
291
    lua_getglobal(L, "arg");
254✔
292
    if (lua_istable(L, -1)) {
254✔
293
      do {
313✔
294
        narg++;
313✔
295
        lua_rawgeti(L, -narg, narg);
313✔
296
      } while (!lua_isnil(L, -1));
313✔
297
      lua_pop(L, 1);
254✔
298
      lua_remove(L, -narg);
254✔
299
      narg--;
254✔
300
    } else {
301
      lua_pop(L, 1);
×
302
    }
303
    status = docall(L, narg, 0);
254✔
304
  }
305
  return report(L, status);
89✔
306
}
307

308
/* Load add-on module. */
309
static int loadjitmodule(lua_State *L)
18✔
310
{
311
  lua_getglobal(L, "require");
18✔
312
  lua_pushliteral(L, "jit.");
18✔
313
  lua_pushvalue(L, -3);
18✔
314
  lua_concat(L, 2);
18✔
315
  if (lua_pcall(L, 1, 1, 0)) {
18✔
316
    const char *msg = lua_tostring(L, -1);
1✔
317
    if (msg && !strncmp(msg, "module ", 7))
1✔
318
      goto nomodule;
1✔
319
    return report(L, 1);
×
320
  }
321
  lua_getfield(L, -1, "start");
17✔
322
  if (lua_isnil(L, -1)) {
17✔
323
  nomodule:
×
324
    l_message(progname,
1✔
325
              "unknown luaJIT command or jit.* modules not installed");
326
    return 1;
1✔
327
  }
328
  lua_remove(L, -2);  /* Drop module table. */
17✔
329
  return 0;
17✔
330
}
331

332
/* Run command with options. */
333
static int runcmdopt(lua_State *L, const char *opt)
10✔
334
{
335
  int narg = 0;
10✔
336
  if (opt && *opt) {
10✔
337
    for (;;) {  /* Split arguments. */
10✔
338
      const char *p = strchr(opt, ',');
8✔
339
      narg++;
8✔
340
      if (!p) break;
8✔
341
      if (p == opt)
2✔
342
        lua_pushnil(L);
×
343
      else
344
        lua_pushlstring(L, opt, (size_t)(p - opt));
2✔
345
      opt = p + 1;
2✔
346
    }
347
    if (*opt)
6✔
348
      lua_pushstring(L, opt);
6✔
349
    else
350
      lua_pushnil(L);
×
351
  }
352
  return report(L, lua_pcall(L, narg, 0, 0));
10✔
353
}
354

355
/* JIT engine control command: try jit library first or load add-on module. */
356
static int dojitcmd(lua_State *L, const char *cmd)
4✔
357
{
358
  const char *opt = strchr(cmd, '=');
4✔
359
  lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd));
4✔
360
  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
4✔
361
  lua_getfield(L, -1, "jit");  /* Get jit.* module table. */
4✔
362
  lua_remove(L, -2);
4✔
363
  lua_pushvalue(L, -2);
4✔
364
  lua_gettable(L, -2);  /* Lookup library function. */
4✔
365
  if (!lua_isfunction(L, -1)) {
4✔
366
    lua_pop(L, 2);  /* Drop non-function and jit.* table, keep module name. */
1✔
367
    if (loadjitmodule(L))
1✔
368
      return 1;
369
  } else {
370
    lua_remove(L, -2);  /* Drop jit.* table. */
3✔
371
  }
372
  lua_remove(L, -2);  /* Drop module name. */
3✔
373
  return runcmdopt(L, opt ? opt+1 : opt);
3✔
374
}
375

376
static int runtoolcmd(lua_State *L, const char *tool_name)
18✔
377
{
378
  lua_getglobal(L, "require");
18✔
379
  lua_pushstring(L, tool_name);
18✔
380
  if (lua_pcall(L, 1, 1, 0)) {
18✔
381
    const char *msg = lua_tostring(L, -1);
×
382
    if (msg) {
×
383
      if (!strncmp(msg, "module ", 7))
×
384
        msg = "unknown luaJIT command or tools not installed";
×
385
      l_message(progname, msg);
×
386
    }
387
    return 1;
×
388
  }
389
  lua_getglobal(L, "arg");
18✔
390
  return report(L, lua_pcall(L, 1, 1, 0));
18✔
391
}
392

393
static int dotoolcmd(lua_State *L, const char *cmd)
394
{
395
  switch (cmd[0]) {
396
  case 'm':
397
    return runtoolcmd(L, "memprof");
398
  case 's':
399
    return runtoolcmd(L, "sysprof");
400
  default:
401
    print_tools_usage();
402
    break;
403
  }
404
  return -1;
405
}
406

407
/* Optimization flags. */
408
static int dojitopt(lua_State *L, const char *opt)
7✔
409
{
410
  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
7✔
411
  lua_getfield(L, -1, "jit.opt");  /* Get jit.opt.* module table. */
7✔
412
  lua_remove(L, -2);
7✔
413
  lua_getfield(L, -1, "start");
7✔
414
  lua_remove(L, -2);
7✔
415
  return runcmdopt(L, opt);
7✔
416
}
417

418
/* Save or list bytecode. */
419
static int dobytecode(lua_State *L, char **argv)
17✔
420
{
421
  int narg = 0;
17✔
422
  lua_pushliteral(L, "bcsave");
17✔
423
  if (loadjitmodule(L))
17✔
424
    return 1;
425
  if (argv[0][2]) {
17✔
426
    narg++;
4✔
427
    argv[0][1] = '-';
4✔
428
    lua_pushstring(L, argv[0]+1);
4✔
429
  }
430
  for (argv++; *argv != NULL; narg++, argv++)
67✔
431
    lua_pushstring(L, *argv);
50✔
432
  report(L, lua_pcall(L, narg, 0, 0));
17✔
433
  return -1;
17✔
434
}
435

436
/* check that argument has no extra characters at the end */
437
#define notail(x)        {if ((x)[2] != '\0') return -1;}
438

439
#define FLAGS_INTERACTIVE        1
440
#define FLAGS_VERSION                2
441
#define FLAGS_EXEC                4
442
#define FLAGS_OPTION                8
443
#define FLAGS_NOENV                16
444
#define FLAGS_TOOL                32
445

446
static int collectargs(char **argv, int *flags)
330✔
447
{
448
  int i;
330✔
449
  for (i = 1; argv[i] != NULL; i++) {
688✔
450
    if (argv[i][0] != '-')  /* Not an option? */
658✔
451
      return i;
252✔
452
    switch (argv[i][1]) {  /* Check option. */
406✔
453
    case '-':
3✔
454
      notail(argv[i]);
3✔
455
      return i+1;
2✔
456
    case '\0':
457
      return i;
458
    case 'i':
4✔
459
      notail(argv[i]);
4✔
460
      *flags |= FLAGS_INTERACTIVE;
4✔
461
      /* fallthrough */
462
    case 'v':
8✔
463
      notail(argv[i]);
8✔
464
      *flags |= FLAGS_VERSION;
8✔
465
      break;
8✔
466
    case 't':
20✔
467
      *flags |= FLAGS_TOOL;
20✔
468
      return i + 1;
20✔
469
    case 'e':
283✔
470
      *flags |= FLAGS_EXEC;
283✔
471
      /* fallthrough */
472
    case 'j':  /* LuaJIT extension */
344✔
473
    case 'l':
474
      *flags |= FLAGS_OPTION;
344✔
475
      if (argv[i][2] == '\0') {
344✔
476
        i++;
330✔
477
        if (argv[i] == NULL) return -1;
330✔
478
      }
479
      break;
480
    case 'O': break;  /* LuaJIT extension */
481
    case 'b':  /* LuaJIT extension */
17✔
482
      if (*flags) return -1;
17✔
483
      *flags |= FLAGS_EXEC;
17✔
484
      return i+1;
17✔
485
    case 'E':
1✔
486
      *flags |= FLAGS_NOENV;
1✔
487
      break;
1✔
488
    default: return -1;  /* invalid option */
489
    }
490
  }
491
  return i;
492
}
493

494
static int runargs(lua_State *L, char **argv, int argn)
325✔
495
{
496
  int i;
325✔
497
  for (i = 1; i < argn; i++) {
677✔
498
    if (argv[i] == NULL) continue;
397✔
499
    lua_assert(argv[i][0] == '-');
397✔
500
    switch (argv[i][1]) {
397✔
501
    case 'e': {
281✔
502
      const char *chunk = argv[i] + 2;
281✔
503
      if (*chunk == '\0') chunk = argv[++i];
281✔
504
      lua_assert(chunk != NULL);
281✔
505
      if (dostring(L, chunk, "=(command line)") != 0)
281✔
506
        return 1;
507
      break;
508
      }
509
    case 'l': {
57✔
510
      const char *filename = argv[i] + 2;
57✔
511
      if (*filename == '\0') filename = argv[++i];
57✔
512
      lua_assert(filename != NULL);
57✔
513
      if (dolibrary(L, filename))
57✔
514
        return 1;
515
      break;
516
      }
517
    case 'j': {  /* LuaJIT extension. */
4✔
518
      const char *cmd = argv[i] + 2;
4✔
519
      if (*cmd == '\0') cmd = argv[++i];
4✔
520
      lua_assert(cmd != NULL);
4✔
521
      if (dojitcmd(L, cmd))
4✔
522
        return 1;
523
      break;
524
      }
525
    case 't': { /* Tarantool's fork extension. */
20✔
526
      const char *cmd = argv[i] + 2;
20✔
527
      return dotoolcmd(L, cmd) != LUA_OK;
20✔
528
    }
529
    case 'O':  /* LuaJIT extension. */
7✔
530
      if (dojitopt(L, argv[i] + 2))
7✔
531
        return 1;
532
      break;
533
    case 'b':  /* LuaJIT extension. */
17✔
534
      return dobytecode(L, argv+i);
17✔
535
    default: break;
536
    }
537
  }
538
  return LUA_OK;
539
}
540

541
static int handle_luainit(lua_State *L)
324✔
542
{
543
#if LJ_TARGET_CONSOLE
544
  const char *init = NULL;
545
#else
546
  const char *init = getenv(LUA_INIT);
324✔
547
#endif
548
  if (init == NULL)
324✔
549
    return LUA_OK;
550
  else if (init[0] == '@')
×
551
    return dofile(L, init+1);
×
552
  else
553
    return dostring(L, init, "=" LUA_INIT);
×
554
}
555

556
static struct Smain {
557
  char **argv;
558
  int argc;
559
  int status;
560
} smain;
561

562
static int pmain(lua_State *L)
330✔
563
{
564
  struct Smain *s = &smain;
330✔
565
  char **argv = s->argv;
330✔
566
  int argn;
330✔
567
  int flags = 0;
330✔
568
  globalL = L;
330✔
569
  if (argv[0] && argv[0][0]) progname = argv[0];
330✔
570

571
  LUAJIT_VERSION_SYM();  /* Linker-enforced version check. */
330✔
572

573
  argn = collectargs(argv, &flags);
330✔
574
  if (argn < 0) {  /* Invalid args? */
330✔
575
    print_usage();
5✔
576
    s->status = 1;
5✔
577
    return 0;
5✔
578
  }
579

580
  if ((flags & FLAGS_NOENV)) {
325✔
581
    lua_pushboolean(L, 1);
1✔
582
    lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
1✔
583
  }
584

585
  /* Stop collector during library initialization. */
586
  lua_gc(L, LUA_GCSTOP, 0);
325✔
587
  luaL_openlibs(L);
325✔
588
  lua_gc(L, LUA_GCRESTART, -1);
325✔
589

590
  createargtable(L, argv, s->argc, (flags & FLAGS_TOOL) ? argn - 1 : argn);
325✔
591

592
  if (!(flags & FLAGS_NOENV)) {
325✔
593
    s->status = handle_luainit(L);
324✔
594
    if (s->status != LUA_OK) return 0;
324✔
595
  }
596

597
  if ((flags & FLAGS_VERSION)) print_version();
325✔
598

599
  s->status = runargs(L, argv, argn);
325✔
600
  if (s->status != LUA_OK) return 0;
304✔
601

602
  if (s->argc > argn) {
280✔
603
    s->status = handle_script(L, argv + argn);
255✔
604
    if (s->status != LUA_OK) return 0;
89✔
605
  }
606

607
  if ((flags & FLAGS_INTERACTIVE)) {
112✔
608
    print_jit_status(L);
4✔
609
    dotty(L);
4✔
610
  } else if (s->argc == argn && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) {
108✔
611
    if (lua_stdin_is_tty()) {
1✔
612
      print_version();
×
613
      print_jit_status(L);
×
614
      dotty(L);
×
615
    } else {
616
      dofile(L, NULL);  /* Executes stdin as a file. */
1✔
617
    }
618
  }
619
  return 0;
620
}
621

622
int main(int argc, char **argv)
330✔
623
{
624
  int status;
330✔
625
  lua_State *L = lua_open();
330✔
626
  if (L == NULL) {
330✔
627
    l_message(argv[0], "cannot create state: not enough memory");
×
628
    return EXIT_FAILURE;
×
629
  }
630
  smain.argc = argc;
330✔
631
  smain.argv = argv;
330✔
632
  status = lua_cpcall(L, pmain, NULL);
330✔
633
  report(L, status);
143✔
634
  lua_close(L);
143✔
635
  return (status || smain.status > 0) ? EXIT_FAILURE : EXIT_SUCCESS;
143✔
636
}
637

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc