more complete (and hopefuly more correct) handling of 'sizeof(char)'

This commit is contained in:
Roberto Ierusalimschy 2011-05-03 13:01:57 -03:00
parent bc1c718cc0
commit ad2531a0ee
6 changed files with 29 additions and 24 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.230 2011/04/08 19:17:36 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.231 2011/04/19 18:29:41 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -442,8 +442,10 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
newsize = B->n + sz; newsize = B->n + sz;
if (newsize < B->n || newsize - B->n < sz) if (newsize < B->n || newsize - B->n < sz)
luaL_error(L, "buffer too large"); luaL_error(L, "buffer too large");
newbuff = (char *)lua_newuserdata(L, newsize); /* create larger buffer */ /* create larger buffer */
memcpy(newbuff, B->b, B->n); /* move content to new buffer */ newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char));
/* move content to new buffer */
memcpy(newbuff, B->b, B->n * sizeof(char));
if (buffonstack(B)) if (buffonstack(B))
lua_remove(L, -2); /* remove old buffer */ lua_remove(L, -2); /* remove old buffer */
B->b = newbuff; B->b = newbuff;
@ -455,7 +457,7 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
char *b = luaL_prepbuffsize(B, l); char *b = luaL_prepbuffsize(B, l);
memcpy(b, s, l); memcpy(b, s, l * sizeof(char));
luaL_addsize(B, l); luaL_addsize(B, l);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 2.46 2011/02/07 19:15:24 roberto Exp roberto $ ** $Id: lobject.c,v 2.47 2011/04/05 18:32:06 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
*/ */
@ -264,19 +264,20 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
} }
/* number of chars of a literal string without the ending \0 */
#define LL(x) (sizeof(x)/sizeof(char) - 1)
#define LL(x) ((sizeof(x) - 1)/sizeof(char))
#define RETS "..." #define RETS "..."
#define PRE "[string \"" #define PRE "[string \""
#define POS "\"]" #define POS "\"]"
#define addstr(a,b,l) ( memcpy(a,b,l), a += (l) ) #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
void luaO_chunkid (char *out, const char *source, size_t bufflen) { void luaO_chunkid (char *out, const char *source, size_t bufflen) {
size_t l = strlen(source); size_t l = strlen(source);
if (*source == '=') { /* 'literal' source */ if (*source == '=') { /* 'literal' source */
if (l <= bufflen) /* small enough? */ if (l <= bufflen) /* small enough? */
memcpy(out, source + 1, l); memcpy(out, source + 1, l * sizeof(char));
else { /* truncate it */ else { /* truncate it */
addstr(out, source + 1, bufflen - 1); addstr(out, source + 1, bufflen - 1);
*out = '\0'; *out = '\0';
@ -284,11 +285,11 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) {
} }
else if (*source == '@') { /* file name */ else if (*source == '@') { /* file name */
if (l <= bufflen) /* small enough? */ if (l <= bufflen) /* small enough? */
memcpy(out, source + 1, l); memcpy(out, source + 1, l * sizeof(char));
else { /* add '...' before rest of name */ else { /* add '...' before rest of name */
addstr(out, RETS, LL(RETS)); addstr(out, RETS, LL(RETS));
bufflen -= LL(RETS); bufflen -= LL(RETS);
memcpy(out, source + 1 + l - bufflen, bufflen); memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
} }
} }
else { /* string; format as [string "source"] */ else { /* string; format as [string "source"] */
@ -304,6 +305,7 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) {
addstr(out, source, l); addstr(out, source, l);
addstr(out, RETS, LL(RETS)); addstr(out, RETS, LL(RETS));
} }
memcpy(out, POS, LL(POS) + 1); memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 2.48 2011/04/05 14:24:07 roberto Exp roberto $ ** $Id: lobject.h,v 2.49 2011/04/07 16:11:57 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -249,7 +249,7 @@ typedef union TString {
CommonHeader; CommonHeader;
lu_byte reserved; lu_byte reserved;
unsigned int hash; unsigned int hash;
size_t len; size_t len; /* number of characters in string */
} tsv; } tsv;
} TString; } TString;
@ -270,7 +270,7 @@ typedef union Udata {
CommonHeader; CommonHeader;
struct Table *metatable; struct Table *metatable;
struct Table *env; struct Table *env;
size_t len; size_t len; /* number of bytes */
} uv; } uv;
} Udata; } Udata;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.c,v 2.17 2010/04/03 20:24:18 roberto Exp roberto $ ** $Id: lstring.c,v 2.18 2010/05/10 18:23:45 roberto Exp roberto $
** String table (keeps all strings handled by Lua) ** String table (keeps all strings handled by Lua)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -84,8 +84,9 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
o != NULL; o != NULL;
o = gch(o)->next) { o = gch(o)->next) {
TString *ts = rawgco2ts(o); TString *ts = rawgco2ts(o);
if (h == ts->tsv.hash && ts->tsv.len == l && if (h == ts->tsv.hash &&
(memcmp(str, getstr(ts), l) == 0)) { ts->tsv.len == l &&
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
changewhite(o); /* resurrect it */ changewhite(o); /* resurrect it */
return ts; return ts;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstrlib.c,v 1.165 2011/03/18 19:02:33 roberto Exp roberto $ ** $Id: lstrlib.c,v 1.166 2011/04/20 16:36:28 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
*/ */
@ -118,10 +118,10 @@ static int str_rep (lua_State *L) {
luaL_Buffer b; luaL_Buffer b;
char *p = luaL_buffinitsize(L, &b, totallen); char *p = luaL_buffinitsize(L, &b, totallen);
while (n-- > 1) { /* first n-1 copies (followed by separator) */ while (n-- > 1) { /* first n-1 copies (followed by separator) */
memcpy(p, s, l); p += l; memcpy(p, s, l * sizeof(char)); p += l;
memcpy(p, sep, lsep); p += lsep; memcpy(p, sep, lsep * sizeof(char)); p += lsep;
} }
memcpy(p, s, l); /* last copy (not followed by separator) */ memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
luaL_pushresultsize(&b, totallen); luaL_pushresultsize(&b, totallen);
} }
return 1; return 1;
@ -820,7 +820,7 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
if (isdigit(uchar(*p))) if (isdigit(uchar(*p)))
luaL_error(L, "invalid format (width or precision too long)"); luaL_error(L, "invalid format (width or precision too long)");
*(form++) = '%'; *(form++) = '%';
memcpy(form, strfrmt, p - strfrmt + 1); memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
form += p - strfrmt + 1; form += p - strfrmt + 1;
*form = '\0'; *form = '\0';
return p; return p;

4
lua.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.196 2011/02/07 12:27:13 roberto Exp roberto $ ** $Id: lua.c,v 1.197 2011/03/14 15:39:42 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -246,7 +246,7 @@ static const char *get_prompt (lua_State *L, int firstline) {
/* mark in error messages for incomplete statements */ /* mark in error messages for incomplete statements */
#define EOFMARK "<eof>" #define EOFMARK "<eof>"
#define marklen (sizeof(EOFMARK) - 1) #define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
static int incomplete (lua_State *L, int status) { static int incomplete (lua_State *L, int status) {
if (status == LUA_ERRSYNTAX) { if (status == LUA_ERRSYNTAX) {