Hi,
Here new assorted suggestions for Lua 5.4.2.
1. Standardization strncmp: (src/lauxlib.c)
We know the size of the "method" constant in the function luaL_argerror.
diff --git a/lauxlib.c b/lauxlib.c
index 73504389..a4be6298 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -177,7 +177,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
lua_getinfo(L, "n", &ar);
- if (strcmp(ar.namewhat, "method") == 0) {
+ if (strncmp(ar.namewhat, "method", 6) == 0) {
arg--; /* do not count 'self' */
if (arg == 0) /* error is in the self argument itself? */
return luaL_error(L, "calling '%s' on bad self (%s)",
2. Standardization strncmp: (src/lauxlib.c)
Is possible to get the size of the variable name, with the standard Lua functions.
@@ -359,11 +359,16 @@ LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
const char *const lst[]) {
- const char *name = (def) ? luaL_optstring(L, arg, def) :
- luaL_checkstring(L, arg);
+ const char *name;
+ size_t l;
int i;
+ if (def) {
+ name = luaL_optlstring(L, arg, def, &l);
+ } else {
+ name = luaL_checklstring(L, arg, &l);
+ }
for (i=0; lst[i]; i++)
- if (strcmp(lst[i], name) == 0)
+ if (strncmp(lst[i], name, l) == 0)
return i;
return luaL_argerror(L, arg,
lua_pushfstring(L, "invalid option '%s'", name));
3.
Standardization strncmp: (src/lauxlib.c)
We know the sizes of the "on" and "off" constants in the function
checkcontrol
.
@@ -1024,9 +1029,9 @@ static int checkcontrol (lua_State *L, const char *message, int tocont) {
if (tocont || *(message++) != '@') /* not a control message? */
return 0;
else {
- if (strcmp(message, "off") == 0)
+ if (strncmp(message, "off", 3) == 0)
lua_setwarnf(L, warnfoff, L); /* turn warnings off */
- else if (strcmp(message, "on") == 0)
+ else if (strncmp(message, "on", 2) == 0)
lua_setwarnf(L, warnfon, L); /* turn warnings on */
return 1; /* it was a control message */
}
4. Avoid strlen at lua_pushstring, with a constant "k".
diff --git a/ldblib.c b/ldblib.c
index 5a326ade..4330e00c 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -377,7 +377,7 @@ static int db_sethook (lua_State *L) {
}
if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
/* table just created; initialize it */
- lua_pushstring(L, "k");
+ lua_pushlstring(L, "k", 1);
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
lua_pushvalue(L, -1);
lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
5. fgets returns char * pointer which needs to be compared to NULL, not 0.
We know the size of the "cont\n" constant.
@@ -418,10 +418,10 @@ static int db_gethook (lua_State *L) {
static int db_debug (lua_State *L) {
for (;;) {
- char buffer[250];
+ char buffer[256];
lua_writestringerror("%s", "lua_debug> ");
- if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
- strcmp(buffer, "cont\n") == 0)
+ if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
+ strncmp(buffer, "cont\n", 5) == 0)
return 0;
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
lua_pcall(L, 0, 0, 0))
6. Standardization strncmp.
We know the size of the "*t" constant.
diff --git a/loslib.c b/loslib.c
index e65e188b..a664a607 100644
--- a/loslib.c
+++ b/loslib.c
@@ -316,7 +316,7 @@ static int os_date (lua_State *L) {
if (stm == NULL) /* invalid date? */
return luaL_error(L,
"date result cannot be represented in this installation");
- if (strcmp(s, "*t") == 0) {
+ if (strncmp(s, "*t", 2) == 0) {
lua_createtable(L, 0, 9); /* 9 = number of fields */
setallfields(L, stm);
}
7.
Standardization strncmp.
We know the sizes of the "const" and "close" constants.
diff --git a/lparser.c b/lparser.c
index 77813a74..e14fff03 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1691,9 +1691,9 @@ static int getlocalattribute (LexState *ls) {
if (testnext(ls, '<')) {
const char *attr = getstr(str_checkname(ls));
checknext(ls, '>');
- if (strcmp(attr, "const") == 0)
+ if (strncmp(attr, "const", 5) == 0)
return RDKCONST; /* read-only variable */
- else if (strcmp(attr, "close") == 0)
+ else if (strncmp(attr, "close", 5) == 0)
return RDKTOCLOSE; /* to-be-closed variable */
else
luaK_semerror(ls,
8. Avoid loop test against '\0' at strcpy.
We know the size of the var lenmod.
diff --git a/lstrlib.c b/lstrlib.c
index 940a14ca..3a09f88c 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1215,7 +1215,8 @@ static void addlenmod (char *form, const char *lenmod) {
size_t l = strlen(form);
size_t lm = strlen(lenmod);
char spec = form[l - 1];
- strcpy(form + l - 1, lenmod);
+ memcpy(form + l - 1, lenmod, lm);
form[l + lm - 1] = spec;
form[l + lm] = '\0';
}
9.
Standardization strncmp.
We know the size of the EOFMARK constant.
Strchr is it's more efficient than strcmp.
diff --git a/lua.c b/lua.c
index b5b884b6..857e98cb 100644
--- a/lua.c
+++ b/lua.c
@@ -220,7 +220,7 @@ static int pushargs (lua_State *L) {
static int handle_script (lua_State *L, char **argv) {
int status;
const char *fname = argv[0];
- if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
+ if ((strchr(fname, '-') != NULL) && strncmp(argv[-1], "--", 2) != 0)
fname = NULL; /* stdin */
status = luaL_loadfile(L, fname);
if (status == LUA_OK) {
@@ -444,7 +444,7 @@ static int incomplete (lua_State *L, int status) {
if (status == LUA_ERRSYNTAX) {
size_t lmsg;
const char *msg = lua_tolstring(L, -1, &lmsg);
- if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
+ if (lmsg >= marklen && strncmp(msg + lmsg - marklen, EOFMARK, sizeof(EOFMARK) - 1) == 0) {
lua_pop(L, 1);
return 1;
}
regards,
Ranier Vilela