diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-06-18 11:26:05 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-06-18 11:26:05 -0300 |
commit | 19eb6ae5801a6e3c57031065c104f91de7f2c5e8 (patch) | |
tree | 0aa166d1cc94ea0093486f7b0f4223e16b8cf74e | |
parent | cbe05b48bbfe98b0aac3947a877a4d493f80ab63 (diff) | |
download | lua-19eb6ae5801a6e3c57031065c104f91de7f2c5e8.tar.gz lua-19eb6ae5801a6e3c57031065c104f91de7f2c5e8.tar.bz2 lua-19eb6ae5801a6e3c57031065c104f91de7f2c5e8.zip |
using 'snprintf' in C99 (both for documentation of buffer sizes
and some complains from tools)
-rw-r--r-- | lobject.c | 8 | ||||
-rw-r--r-- | lstrlib.c | 34 | ||||
-rw-r--r-- | luaconf.h | 23 |
3 files changed, 39 insertions, 26 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.c,v 2.103 2015/03/28 19:14:47 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.104 2015/04/11 18:30:08 roberto Exp roberto $ |
3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -333,9 +333,9 @@ void luaO_tostring (lua_State *L, StkId obj) { | |||
333 | size_t len; | 333 | size_t len; |
334 | lua_assert(ttisnumber(obj)); | 334 | lua_assert(ttisnumber(obj)); |
335 | if (ttisinteger(obj)) | 335 | if (ttisinteger(obj)) |
336 | len = lua_integer2str(buff, ivalue(obj)); | 336 | len = lua_integer2str(buff, sizeof(buff), ivalue(obj)); |
337 | else { | 337 | else { |
338 | len = lua_number2str(buff, fltvalue(obj)); | 338 | len = lua_number2str(buff, sizeof(buff), fltvalue(obj)); |
339 | #if !defined(LUA_COMPAT_FLOATSTRING) | 339 | #if !defined(LUA_COMPAT_FLOATSTRING) |
340 | if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ | 340 | if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ |
341 | buff[len++] = lua_getlocaledecpoint(); | 341 | buff[len++] = lua_getlocaledecpoint(); |
@@ -393,7 +393,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | |||
393 | } | 393 | } |
394 | case 'p': { | 394 | case 'p': { |
395 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ | 395 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ |
396 | int l = sprintf(buff, "%p", va_arg(argp, void *)); | 396 | int l = l_sprintf(buff, sizeof(buff), "%p", va_arg(argp, void *)); |
397 | pushstr(L, buff, l); | 397 | pushstr(L, buff, l); |
398 | break; | 398 | break; |
399 | } | 399 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.228 2015/04/03 18:41:57 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.229 2015/05/20 17:39:23 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,12 +830,12 @@ static lua_Number adddigit (char *buff, int n, lua_Number x) { | |||
830 | } | 830 | } |
831 | 831 | ||
832 | 832 | ||
833 | static int num2straux (char *buff, lua_Number x) { | 833 | static int num2straux (char *buff, size_t 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 sprintf(buff, 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... */ |
837 | sprintf(buff, LUA_NUMBER_FMT, x); | 837 | l_sprintf(buff, sz, LUA_NUMBER_FMT, x); /* create "0" or "-0" */ |
838 | strcat(buff, "x0p+0"); /* reuses '0/-0' from 'sprintf'... */ | 838 | strcat(buff, "x0p+0"); /* add exponent to that */ |
839 | return strlen(buff); | 839 | return strlen(buff); |
840 | } | 840 | } |
841 | else { | 841 | else { |
@@ -855,15 +855,16 @@ static int num2straux (char *buff, lua_Number x) { | |||
855 | m = adddigit(buff, n++, m * 16); | 855 | m = adddigit(buff, n++, m * 16); |
856 | } while (m > 0); | 856 | } while (m > 0); |
857 | } | 857 | } |
858 | n += sprintf(buff + 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 | return n; | 860 | return n; |
860 | } | 861 | } |
861 | } | 862 | } |
862 | 863 | ||
863 | 864 | ||
864 | static int lua_number2strx (lua_State *L, char *buff, const char *fmt, | 865 | static int lua_number2strx (lua_State *L, char *buff, size_t sz, |
865 | lua_Number x) { | 866 | const char *fmt, lua_Number x) { |
866 | int n = num2straux(buff, x); | 867 | int n = num2straux(buff, sz, x); |
867 | if (fmt[SIZELENMOD] == 'A') { | 868 | if (fmt[SIZELENMOD] == 'A') { |
868 | int i; | 869 | int i; |
869 | for (i = 0; i < n; i++) | 870 | for (i = 0; i < n; i++) |
@@ -906,9 +907,9 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { | |||
906 | else if (*s == '\0' || iscntrl(uchar(*s))) { | 907 | else if (*s == '\0' || iscntrl(uchar(*s))) { |
907 | char buff[10]; | 908 | char buff[10]; |
908 | if (!isdigit(uchar(*(s+1)))) | 909 | if (!isdigit(uchar(*(s+1)))) |
909 | sprintf(buff, "\\%d", (int)uchar(*s)); | 910 | l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); |
910 | else | 911 | else |
911 | sprintf(buff, "\\%03d", (int)uchar(*s)); | 912 | l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s)); |
912 | luaL_addstring(b, buff); | 913 | luaL_addstring(b, buff); |
913 | } | 914 | } |
914 | else | 915 | else |
@@ -975,24 +976,25 @@ static int str_format (lua_State *L) { | |||
975 | strfrmt = scanformat(L, strfrmt, form); | 976 | strfrmt = scanformat(L, strfrmt, form); |
976 | switch (*strfrmt++) { | 977 | switch (*strfrmt++) { |
977 | case 'c': { | 978 | case 'c': { |
978 | nb = sprintf(buff, form, (int)luaL_checkinteger(L, arg)); | 979 | nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg)); |
979 | break; | 980 | break; |
980 | } | 981 | } |
981 | case 'd': case 'i': | 982 | case 'd': case 'i': |
982 | case 'o': case 'u': case 'x': case 'X': { | 983 | case 'o': case 'u': case 'x': case 'X': { |
983 | lua_Integer n = luaL_checkinteger(L, arg); | 984 | lua_Integer n = luaL_checkinteger(L, arg); |
984 | addlenmod(form, LUA_INTEGER_FRMLEN); | 985 | addlenmod(form, LUA_INTEGER_FRMLEN); |
985 | nb = sprintf(buff, form, n); | 986 | nb = l_sprintf(buff, MAX_ITEM, form, n); |
986 | break; | 987 | break; |
987 | } | 988 | } |
988 | case 'a': case 'A': | 989 | case 'a': case 'A': |
989 | addlenmod(form, LUA_NUMBER_FRMLEN); | 990 | addlenmod(form, LUA_NUMBER_FRMLEN); |
990 | nb = lua_number2strx(L, buff, form, luaL_checknumber(L, arg)); | 991 | nb = lua_number2strx(L, buff, MAX_ITEM, form, |
992 | luaL_checknumber(L, arg)); | ||
991 | break; | 993 | break; |
992 | case 'e': case 'E': case 'f': | 994 | case 'e': case 'E': case 'f': |
993 | case 'g': case 'G': { | 995 | case 'g': case 'G': { |
994 | addlenmod(form, LUA_NUMBER_FRMLEN); | 996 | addlenmod(form, LUA_NUMBER_FRMLEN); |
995 | nb = sprintf(buff, form, luaL_checknumber(L, arg)); | 997 | nb = l_sprintf(buff, MAX_ITEM, form, luaL_checknumber(L, arg)); |
996 | break; | 998 | break; |
997 | } | 999 | } |
998 | case 'q': { | 1000 | case 'q': { |
@@ -1008,7 +1010,7 @@ static int str_format (lua_State *L) { | |||
1008 | luaL_addvalue(&b); | 1010 | luaL_addvalue(&b); |
1009 | } | 1011 | } |
1010 | else { | 1012 | else { |
1011 | nb = sprintf(buff, form, s); | 1013 | nb = l_sprintf(buff, MAX_ITEM, form, s); |
1012 | lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ | 1014 | lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ |
1013 | } | 1015 | } |
1014 | break; | 1016 | break; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: luaconf.h,v 1.250 2015/04/03 18:41:57 roberto Exp roberto $ | 2 | ** $Id: luaconf.h,v 1.251 2015/05/20 17:39:23 roberto Exp roberto $ |
3 | ** Configuration file for Lua | 3 | ** Configuration file for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -145,7 +145,7 @@ | |||
145 | 145 | ||
146 | #if !defined(LUA_FLOAT_TYPE) | 146 | #if !defined(LUA_FLOAT_TYPE) |
147 | #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE | 147 | #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE |
148 | #endif /* } */ | 148 | #endif |
149 | 149 | ||
150 | /* }================================================================== */ | 150 | /* }================================================================== */ |
151 | 151 | ||
@@ -470,7 +470,7 @@ | |||
470 | 470 | ||
471 | #define l_floor(x) (l_mathop(floor)(x)) | 471 | #define l_floor(x) (l_mathop(floor)(x)) |
472 | 472 | ||
473 | #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) | 473 | #define lua_number2str(s,sz,n) l_sprintf((s), sz, LUA_NUMBER_FMT, (n)) |
474 | 474 | ||
475 | 475 | ||
476 | /* | 476 | /* |
@@ -506,7 +506,7 @@ | |||
506 | /* The following definitions are good for most cases here */ | 506 | /* The following definitions are good for most cases here */ |
507 | 507 | ||
508 | #define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" | 508 | #define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" |
509 | #define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n)) | 509 | #define lua_integer2str(s,sz,n) l_sprintf((s), sz, LUA_INTEGER_FMT, (n)) |
510 | 510 | ||
511 | #define LUAI_UACINT LUA_INTEGER | 511 | #define LUAI_UACINT LUA_INTEGER |
512 | 512 | ||
@@ -578,13 +578,24 @@ | |||
578 | */ | 578 | */ |
579 | 579 | ||
580 | /* | 580 | /* |
581 | @@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. | ||
582 | ** (All uses in Lua have only one format item.) | ||
583 | */ | ||
584 | #if !defined(LUA_USE_C89) | ||
585 | #define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) | ||
586 | #else | ||
587 | #define l_sprintf(s,sz,f,i) sprintf(s,f,i) | ||
588 | #endif | ||
589 | |||
590 | |||
591 | /* | ||
581 | @@ lua_strx2number converts an hexadecimal numeric string to a number. | 592 | @@ lua_strx2number converts an hexadecimal numeric string to a number. |
582 | ** In C99, 'strtod' does that conversion. Otherwise, you can | 593 | ** In C99, 'strtod' does that conversion. Otherwise, you can |
583 | ** leave 'lua_strx2number' undefined and Lua will provide its own | 594 | ** leave 'lua_strx2number' undefined and Lua will provide its own |
584 | ** implementation. | 595 | ** implementation. |
585 | */ | 596 | */ |
586 | #if !defined(LUA_USE_C89) | 597 | #if !defined(LUA_USE_C89) |
587 | #define lua_strx2number(s,p) lua_str2number(s,p) | 598 | #define lua_strx2number(s,p) lua_str2number(s,p) |
588 | #endif | 599 | #endif |
589 | 600 | ||
590 | 601 | ||
@@ -595,7 +606,7 @@ | |||
595 | ** provide its own implementation. | 606 | ** provide its own implementation. |
596 | */ | 607 | */ |
597 | #if !defined(LUA_USE_C89) | 608 | #if !defined(LUA_USE_C89) |
598 | #define lua_number2strx(L,b,f,n) sprintf(b,f,n) | 609 | #define lua_number2strx(L,b,sz,f,n) l_sprintf(b,sz,f,n) |
599 | #endif | 610 | #endif |
600 | 611 | ||
601 | 612 | ||