aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-06-24 15:25:10 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-06-24 15:25:10 -0300
commit5aabb7a89172b74097faa8f34a6a6520d839bde6 (patch)
tree6f24cc03577c4fc65b86b550d4d36d243de1b9f1 /lstrlib.c
parentcb1e451999cfed49a66859d8189a684839845e7f (diff)
downloadlua-5aabb7a89172b74097faa8f34a6a6520d839bde6.tar.gz
lua-5aabb7a89172b74097faa8f34a6a6520d839bde6.tar.bz2
lua-5aabb7a89172b74097faa8f34a6a6520d839bde6.zip
buffer size changed from size_t to int (it is always small) +
comments + assert that printf result fits in given buffer
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/lstrlib.c b/lstrlib.c
index e33e658e..712e55ee 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.229 2015/05/20 17:39:23 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.230 2015/06/18 14:26:05 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*/
@@ -830,7 +830,7 @@ static lua_Number adddigit (char *buff, int n, lua_Number x) {
830} 830}
831 831
832 832
833static int num2straux (char *buff, size_t sz, lua_Number x) { 833static int num2straux (char *buff, int sz, lua_Number x) {
834 if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */ 834 if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */
835 return l_sprintf(buff, sz, LUA_NUMBER_FMT, x); /* equal to '%g' */ 835 return l_sprintf(buff, sz, LUA_NUMBER_FMT, x); /* equal to '%g' */
836 else if (x == 0) { /* can be -0... */ 836 else if (x == 0) { /* can be -0... */
@@ -856,13 +856,13 @@ static int num2straux (char *buff, size_t sz, lua_Number x) {
856 } while (m > 0); 856 } while (m > 0);
857 } 857 }
858 n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */ 858 n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */
859 lua_assert((size_t)n < sz); 859 lua_assert(n < sz);
860 return n; 860 return n;
861 } 861 }
862} 862}
863 863
864 864
865static int lua_number2strx (lua_State *L, char *buff, size_t sz, 865static int lua_number2strx (lua_State *L, char *buff, int sz,
866 const char *fmt, lua_Number x) { 866 const char *fmt, lua_Number x) {
867 int n = num2straux(buff, sz, x); 867 int n = num2straux(buff, sz, x);
868 if (fmt[SIZELENMOD] == 'A') { 868 if (fmt[SIZELENMOD] == 'A') {
@@ -880,10 +880,12 @@ static int lua_number2strx (lua_State *L, char *buff, size_t sz,
880 880
881/* 881/*
882** Maximum size of each formatted item. This maximum size is produced 882** Maximum size of each formatted item. This maximum size is produced
883** by format('%.99f', minfloat), and is equal to 99 + 2 ('-' and '.') + 883** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.',
884** number of decimal digits to represent minfloat. 884** and '\0') + number of decimal digits to represent maxfloat (which
885** is maximum exponent + 1). (99+3+1 then rounded to 120 for "extra
886** expenses", such as locale-dependent stuff)
885*/ 887*/
886#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP)) 888#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP))
887 889
888 890
889/* valid flags in a format specification */ 891/* valid flags in a format specification */
@@ -1020,6 +1022,7 @@ static int str_format (lua_State *L) {
1020 *(strfrmt - 1)); 1022 *(strfrmt - 1));
1021 } 1023 }
1022 } 1024 }
1025 lua_assert(nb < MAX_ITEM);
1023 luaL_addsize(&b, nb); 1026 luaL_addsize(&b, nb);
1024 } 1027 }
1025 } 1028 }