Skip to content

Commit

Permalink
format "%s" in "string.format" accepts embedded zeros when it
Browse files Browse the repository at this point in the history
has no modifiers
  • Loading branch information
roberto-ieru committed Nov 25, 2015
1 parent 5936eb1 commit c25d59a
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lstrlib.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lstrlib.c,v 1.237 2015/10/29 15:11:41 roberto Exp roberto $
** $Id: lstrlib.c,v 1.238 2015/11/23 11:31:21 roberto Exp roberto $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -1031,15 +1031,18 @@ static int str_format (lua_State *L) {
case 's': {
size_t l;
const char *s = luaL_tolstring(L, arg, &l);
luaL_argcheck(L, l == strlen(s), arg, "string cannot contain zeros");
if (!strchr(form, '.') && l >= 100) {
/* no precision and string is too long to be formatted;
keep original string */
luaL_addvalue(&b);
}
if (form[2] == '\0') /* no modifiers? */
luaL_addvalue(&b); /* keep entire string */
else {
nb = l_sprintf(buff, MAX_ITEM, form, s);
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
if (!strchr(form, '.') && l >= 100) {
/* no precision and string is too long to be formatted */
luaL_addvalue(&b); /* keep entire string */
}
else { /* format the string into 'buff' */
nb = l_sprintf(buff, MAX_ITEM, form, s);
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
}
}
break;
}
Expand Down

0 comments on commit c25d59a

Please sign in to comment.