sagpant / simpleai

Other
0 stars 1 forks source link

Potential issue in src/libs/lua/lauxlib.c: Unchecked return from initialization function #1

Open monocle-ai opened 4 years ago

monocle-ai commented 4 years ago

What is a Conditionally Uninitialized Variable? The return value of a function that is potentially used to initialize a local variable is not checked. Therefore, reading the local variable may result in undefined behavior.

4 instances of this defect were found in the following locations:

Instance 1 File : src/libs/lua/lauxlib.c Function: lua_getinfo https://github.com/sagpant/simpleai/blob/db3acd891f46f67ae70c271bf88a46e6e6b28b0f/src/libs/lua/lauxlib.c#L142 Issue in: ar

Code extract:

      level = last - LEVELS2 + 1;  /* and skip to last ones */
    }
    else {
      lua_getinfo(L1, "Slnt", &ar); <------ HERE
      lua_pushfstring(L, "\n\t%s:", ar.short_src);
      if (ar.currentline > 0)

How can I fix it? Correct reference usage found in src/libs/lua/ldblib.c at line 162. https://github.com/sagpant/simpleai/blob/db3acd891f46f67ae70c271bf88a46e6e6b28b0f/src/libs/lua/ldblib.c#L162 Code extract:

      return 1;
    }
  }
  if (!lua_getinfo(L1, options, &ar)) <------ HERE
    return luaL_argerror(L, arg+2, "invalid option");
  lua_newtable(L);  /* table to collect results */

Instance 2 File : src/libs/lua/lauxlib.c Function: lua_getinfo https://github.com/sagpant/simpleai/blob/db3acd891f46f67ae70c271bf88a46e6e6b28b0f/src/libs/lua/lauxlib.c#L169 Issue in: ar

Code extract:

  lua_Debug ar;
  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
    return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
  lua_getinfo(L, "n", &ar); <------ HERE
  if (strcmp(ar.namewhat, "method") == 0) {
    arg--;  /* do not count 'self' */

How can I fix it? Correct reference usage found in src/libs/lua/ldblib.c at line 162. https://github.com/sagpant/simpleai/blob/db3acd891f46f67ae70c271bf88a46e6e6b28b0f/src/libs/lua/ldblib.c#L162 Code extract:

      return 1;
    }
  }
  if (!lua_getinfo(L1, options, &ar)) <------ HERE
    return luaL_argerror(L, arg+2, "invalid option");
  lua_newtable(L);  /* table to collect results */

Instance 3 File : src/libs/lua/lauxlib.c Function: lua_getinfo https://github.com/sagpant/simpleai/blob/db3acd891f46f67ae70c271bf88a46e6e6b28b0f/src/libs/lua/lauxlib.c#L209 Issue in: ar

Code extract:

LUALIB_API void luaL_where (lua_State *L, int level) {
  lua_Debug ar;
  if (lua_getstack(L, level, &ar)) {  /* check function at level */
    lua_getinfo(L, "Sl", &ar);  /* get info about it */ <------ HERE
    if (ar.currentline > 0) {  /* is there info? */
      lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);

How can I fix it? Correct reference usage found in src/libs/lua/ldblib.c at line 162. https://github.com/sagpant/simpleai/blob/db3acd891f46f67ae70c271bf88a46e6e6b28b0f/src/libs/lua/ldblib.c#L162 Code extract:

      return 1;
    }
  }
  if (!lua_getinfo(L1, options, &ar)) <------ HERE
    return luaL_argerror(L, arg+2, "invalid option");
  lua_newtable(L);  /* table to collect results */

Instance 4 File : src/libs/lua/lauxlib.c Function: skipcomment https://github.com/sagpant/simpleai/blob/db3acd891f46f67ae70c271bf88a46e6e6b28b0f/src/libs/lua/lauxlib.c#L724 Issue in: c

Code extract:

  if (c == LUA_SIGNATURE[0] && filename) {  /* binary file? */
    lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
    if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
    skipcomment(&lf, &c);  /* re-read initial portion */ <------ HERE
  }
  if (c != EOF)

How can I fix it? Correct reference usage found in src/libs/lua/lauxlib.c at line 719. https://github.com/sagpant/simpleai/blob/db3acd891f46f67ae70c271bf88a46e6e6b28b0f/src/libs/lua/lauxlib.c#L719 Code extract:

    lf.f = fopen(filename, "r");
    if (lf.f == NULL) return errfile(L, "open", fnameindex);
  }
  if (skipcomment(&lf, &c))  /* read initial portion */ <------ HERE
    lf.buff[lf.n++] = '\n';  /* add line to correct line numbers */
  if (c == LUA_SIGNATURE[0] && filename) {  /* binary file? */
siva-msft commented 4 years ago

May not be a valid bug, but getinfo exhibits a behavior that might warrant checking. Okay to address.

siva-msft commented 4 years ago

likely fp.