aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-01-13 15:18:25 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-01-13 15:18:25 -0200
commit35099149165bd0120b0c46ce0bc5ca32210db56d (patch)
tree07029339917afc7758a83c0ad50f38ecbdc19f9a /lstrlib.c
parentae27be40c97635e2a191848d3180668ccd65ce80 (diff)
downloadlua-35099149165bd0120b0c46ce0bc5ca32210db56d.tar.gz
lua-35099149165bd0120b0c46ce0bc5ca32210db56d.tar.bz2
lua-35099149165bd0120b0c46ce0bc5ca32210db56d.zip
BUG (when compiled with long double): buffer overflow when formatting
string.format("%.99f", 1e4930)
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 7eb62f37..78f7c378 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.220 2014/12/11 13:40:40 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.221 2014/12/11 14:03:07 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -797,8 +797,15 @@ static int str_gsub (lua_State *L) {
797** ======================================================= 797** =======================================================
798*/ 798*/
799 799
800/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ 800/*
801#define MAX_ITEM 512 801** Maximum size of each formatted item. This maximum size is produced
802** by format('%.99f', minfloat), and is equal to 99 + 2 ('-' and '.') +
803** number of decimal digits to represent minfloat (which is ~308 for
804** a double and ~4932 for long double).
805*/
806#define MAX_ITEM \
807 (sizeof(lua_Number) <= 4 ? 150 : sizeof(lua_Number) <= 8 ? 450 : 5050)
808
802 809
803/* valid flags in a format specification */ 810/* valid flags in a format specification */
804#define FLAGS "-+ #0" 811#define FLAGS "-+ #0"
@@ -921,13 +928,12 @@ static int str_format (lua_State *L) {
921 /* no precision and string is too long to be formatted; 928 /* no precision and string is too long to be formatted;
922 keep original string */ 929 keep original string */
923 luaL_addvalue(&b); 930 luaL_addvalue(&b);
924 break;
925 } 931 }
926 else { 932 else {
927 nb = sprintf(buff, form, s); 933 nb = sprintf(buff, form, s);
928 lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ 934 lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
929 break;
930 } 935 }
936 break;
931 } 937 }
932 default: { /* also treat cases 'pnLlh' */ 938 default: { /* also treat cases 'pnLlh' */
933 return luaL_error(L, "invalid option '%%%c' to 'format'", 939 return luaL_error(L, "invalid option '%%%c' to 'format'",