mirror of
https://github.com/lua/lua
synced 2024-12-26 20:29:43 +03:00
using 'snprintf' in C99 (both for documentation of buffer sizes
and some complains from tools)
This commit is contained in:
parent
cbe05b48bb
commit
19eb6ae580
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lobject.c,v 2.103 2015/03/28 19:14:47 roberto Exp roberto $
|
** $Id: lobject.c,v 2.104 2015/04/11 18:30:08 roberto Exp roberto $
|
||||||
** Some generic functions over Lua objects
|
** Some generic functions over Lua objects
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -333,9 +333,9 @@ void luaO_tostring (lua_State *L, StkId obj) {
|
|||||||
size_t len;
|
size_t len;
|
||||||
lua_assert(ttisnumber(obj));
|
lua_assert(ttisnumber(obj));
|
||||||
if (ttisinteger(obj))
|
if (ttisinteger(obj))
|
||||||
len = lua_integer2str(buff, ivalue(obj));
|
len = lua_integer2str(buff, sizeof(buff), ivalue(obj));
|
||||||
else {
|
else {
|
||||||
len = lua_number2str(buff, fltvalue(obj));
|
len = lua_number2str(buff, sizeof(buff), fltvalue(obj));
|
||||||
#if !defined(LUA_COMPAT_FLOATSTRING)
|
#if !defined(LUA_COMPAT_FLOATSTRING)
|
||||||
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
|
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
|
||||||
buff[len++] = lua_getlocaledecpoint();
|
buff[len++] = lua_getlocaledecpoint();
|
||||||
@ -393,7 +393,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
|||||||
}
|
}
|
||||||
case 'p': {
|
case 'p': {
|
||||||
char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */
|
char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */
|
||||||
int l = sprintf(buff, "%p", va_arg(argp, void *));
|
int l = l_sprintf(buff, sizeof(buff), "%p", va_arg(argp, void *));
|
||||||
pushstr(L, buff, l);
|
pushstr(L, buff, l);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
34
lstrlib.c
34
lstrlib.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstrlib.c,v 1.228 2015/04/03 18:41:57 roberto Exp roberto $
|
** $Id: lstrlib.c,v 1.229 2015/05/20 17:39:23 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
|
||||||
*/
|
*/
|
||||||
@ -830,12 +830,12 @@ static lua_Number adddigit (char *buff, int n, lua_Number x) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int num2straux (char *buff, lua_Number x) {
|
static int num2straux (char *buff, size_t sz, lua_Number x) {
|
||||||
if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */
|
if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */
|
||||||
return sprintf(buff, LUA_NUMBER_FMT, x); /* equal to '%g' */
|
return l_sprintf(buff, sz, LUA_NUMBER_FMT, x); /* equal to '%g' */
|
||||||
else if (x == 0) { /* can be -0... */
|
else if (x == 0) { /* can be -0... */
|
||||||
sprintf(buff, LUA_NUMBER_FMT, x);
|
l_sprintf(buff, sz, LUA_NUMBER_FMT, x); /* create "0" or "-0" */
|
||||||
strcat(buff, "x0p+0"); /* reuses '0/-0' from 'sprintf'... */
|
strcat(buff, "x0p+0"); /* add exponent to that */
|
||||||
return strlen(buff);
|
return strlen(buff);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -855,15 +855,16 @@ static int num2straux (char *buff, lua_Number x) {
|
|||||||
m = adddigit(buff, n++, m * 16);
|
m = adddigit(buff, n++, m * 16);
|
||||||
} while (m > 0);
|
} while (m > 0);
|
||||||
}
|
}
|
||||||
n += sprintf(buff + n, "p%+d", e); /* add exponent */
|
n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */
|
||||||
|
lua_assert((size_t)n < sz);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int lua_number2strx (lua_State *L, char *buff, const char *fmt,
|
static int lua_number2strx (lua_State *L, char *buff, size_t sz,
|
||||||
lua_Number x) {
|
const char *fmt, lua_Number x) {
|
||||||
int n = num2straux(buff, x);
|
int n = num2straux(buff, sz, x);
|
||||||
if (fmt[SIZELENMOD] == 'A') {
|
if (fmt[SIZELENMOD] == 'A') {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
@ -906,9 +907,9 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
|||||||
else if (*s == '\0' || iscntrl(uchar(*s))) {
|
else if (*s == '\0' || iscntrl(uchar(*s))) {
|
||||||
char buff[10];
|
char buff[10];
|
||||||
if (!isdigit(uchar(*(s+1))))
|
if (!isdigit(uchar(*(s+1))))
|
||||||
sprintf(buff, "\\%d", (int)uchar(*s));
|
l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s));
|
||||||
else
|
else
|
||||||
sprintf(buff, "\\%03d", (int)uchar(*s));
|
l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s));
|
||||||
luaL_addstring(b, buff);
|
luaL_addstring(b, buff);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -975,24 +976,25 @@ static int str_format (lua_State *L) {
|
|||||||
strfrmt = scanformat(L, strfrmt, form);
|
strfrmt = scanformat(L, strfrmt, form);
|
||||||
switch (*strfrmt++) {
|
switch (*strfrmt++) {
|
||||||
case 'c': {
|
case 'c': {
|
||||||
nb = sprintf(buff, form, (int)luaL_checkinteger(L, arg));
|
nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd': case 'i':
|
case 'd': case 'i':
|
||||||
case 'o': case 'u': case 'x': case 'X': {
|
case 'o': case 'u': case 'x': case 'X': {
|
||||||
lua_Integer n = luaL_checkinteger(L, arg);
|
lua_Integer n = luaL_checkinteger(L, arg);
|
||||||
addlenmod(form, LUA_INTEGER_FRMLEN);
|
addlenmod(form, LUA_INTEGER_FRMLEN);
|
||||||
nb = sprintf(buff, form, n);
|
nb = l_sprintf(buff, MAX_ITEM, form, n);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'a': case 'A':
|
case 'a': case 'A':
|
||||||
addlenmod(form, LUA_NUMBER_FRMLEN);
|
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||||
nb = lua_number2strx(L, buff, form, luaL_checknumber(L, arg));
|
nb = lua_number2strx(L, buff, MAX_ITEM, form,
|
||||||
|
luaL_checknumber(L, arg));
|
||||||
break;
|
break;
|
||||||
case 'e': case 'E': case 'f':
|
case 'e': case 'E': case 'f':
|
||||||
case 'g': case 'G': {
|
case 'g': case 'G': {
|
||||||
addlenmod(form, LUA_NUMBER_FRMLEN);
|
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||||
nb = sprintf(buff, form, luaL_checknumber(L, arg));
|
nb = l_sprintf(buff, MAX_ITEM, form, luaL_checknumber(L, arg));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'q': {
|
case 'q': {
|
||||||
@ -1008,7 +1010,7 @@ static int str_format (lua_State *L) {
|
|||||||
luaL_addvalue(&b);
|
luaL_addvalue(&b);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
nb = sprintf(buff, 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;
|
||||||
|
23
luaconf.h
23
luaconf.h
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: luaconf.h,v 1.250 2015/04/03 18:41:57 roberto Exp roberto $
|
** $Id: luaconf.h,v 1.251 2015/05/20 17:39:23 roberto Exp roberto $
|
||||||
** Configuration file for Lua
|
** Configuration file for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -145,7 +145,7 @@
|
|||||||
|
|
||||||
#if !defined(LUA_FLOAT_TYPE)
|
#if !defined(LUA_FLOAT_TYPE)
|
||||||
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
||||||
#endif /* } */
|
#endif
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
@ -470,7 +470,7 @@
|
|||||||
|
|
||||||
#define l_floor(x) (l_mathop(floor)(x))
|
#define l_floor(x) (l_mathop(floor)(x))
|
||||||
|
|
||||||
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
|
#define lua_number2str(s,sz,n) l_sprintf((s), sz, LUA_NUMBER_FMT, (n))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -506,7 +506,7 @@
|
|||||||
/* The following definitions are good for most cases here */
|
/* The following definitions are good for most cases here */
|
||||||
|
|
||||||
#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
|
#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
|
||||||
#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n))
|
#define lua_integer2str(s,sz,n) l_sprintf((s), sz, LUA_INTEGER_FMT, (n))
|
||||||
|
|
||||||
#define LUAI_UACINT LUA_INTEGER
|
#define LUAI_UACINT LUA_INTEGER
|
||||||
|
|
||||||
@ -577,6 +577,17 @@
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89.
|
||||||
|
** (All uses in Lua have only one format item.)
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_USE_C89)
|
||||||
|
#define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i)
|
||||||
|
#else
|
||||||
|
#define l_sprintf(s,sz,f,i) sprintf(s,f,i)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ lua_strx2number converts an hexadecimal numeric string to a number.
|
@@ lua_strx2number converts an hexadecimal numeric string to a number.
|
||||||
** In C99, 'strtod' does that conversion. Otherwise, you can
|
** In C99, 'strtod' does that conversion. Otherwise, you can
|
||||||
@ -584,7 +595,7 @@
|
|||||||
** implementation.
|
** implementation.
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_USE_C89)
|
#if !defined(LUA_USE_C89)
|
||||||
#define lua_strx2number(s,p) lua_str2number(s,p)
|
#define lua_strx2number(s,p) lua_str2number(s,p)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@ -595,7 +606,7 @@
|
|||||||
** provide its own implementation.
|
** provide its own implementation.
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_USE_C89)
|
#if !defined(LUA_USE_C89)
|
||||||
#define lua_number2strx(L,b,f,n) sprintf(b,f,n)
|
#define lua_number2strx(L,b,sz,f,n) l_sprintf(b,sz,f,n)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user