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 /lstrlib.c | |
| 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)
Diffstat (limited to 'lstrlib.c')
| -rw-r--r-- | lstrlib.c | 34 |
1 files changed, 18 insertions, 16 deletions
| @@ -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; |
