format "%s" in 'string.format' accepts embedded zeros when it

has no modifiers
This commit is contained in:
Roberto Ierusalimschy 2015-11-25 14:28:17 -02:00
parent 5936eb16d8
commit c25d59a6bd
1 changed files with 12 additions and 9 deletions

View File

@ -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 ** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -1031,16 +1031,19 @@ static int str_format (lua_State *L) {
case 's': { case 's': {
size_t l; size_t l;
const char *s = luaL_tolstring(L, arg, &l); const char *s = luaL_tolstring(L, arg, &l);
luaL_argcheck(L, l == strlen(s), arg, "string cannot contain zeros"); if (form[2] == '\0') /* no modifiers? */
if (!strchr(form, '.') && l >= 100) { luaL_addvalue(&b); /* keep entire string */
/* no precision and string is too long to be formatted;
keep original string */
luaL_addvalue(&b);
}
else { else {
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); nb = l_sprintf(buff, MAX_ITEM, form, s);
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
} }
}
break; break;
} }
default: { /* also treat cases 'pnLlh' */ default: { /* also treat cases 'pnLlh' */