From ad2531a0eefb6950d589c0d508b1f959f3f58d8b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 3 May 2011 13:01:57 -0300 Subject: more complete (and hopefuly more correct) handling of 'sizeof(char)' --- lauxlib.c | 10 ++++++---- lobject.c | 16 +++++++++------- lobject.h | 6 +++--- lstring.c | 7 ++++--- lstrlib.c | 10 +++++----- lua.c | 4 ++-- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 3332b2ca..56700867 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -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 ** 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; if (newsize < B->n || newsize - B->n < sz) luaL_error(L, "buffer too large"); - newbuff = (char *)lua_newuserdata(L, newsize); /* create larger buffer */ - memcpy(newbuff, B->b, B->n); /* move content to new buffer */ + /* create larger 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)) lua_remove(L, -2); /* remove old buffer */ 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) { char *b = luaL_prepbuffsize(B, l); - memcpy(b, s, l); + memcpy(b, s, l * sizeof(char)); luaL_addsize(B, l); } diff --git a/lobject.c b/lobject.c index 0fd44626..bd541a1e 100644 --- a/lobject.c +++ b/lobject.c @@ -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 ** 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 PRE "[string \"" #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) { size_t l = strlen(source); if (*source == '=') { /* 'literal' source */ if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l); + memcpy(out, source + 1, l * sizeof(char)); else { /* truncate it */ addstr(out, source + 1, bufflen - 1); *out = '\0'; @@ -284,11 +285,11 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) { } else if (*source == '@') { /* file name */ if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l); + memcpy(out, source + 1, l * sizeof(char)); else { /* add '...' before rest of name */ addstr(out, RETS, 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"] */ @@ -304,6 +305,7 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) { addstr(out, source, l); addstr(out, RETS, LL(RETS)); } - memcpy(out, POS, LL(POS) + 1); + memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); } } + diff --git a/lobject.h b/lobject.h index bca3a6bb..8b000dc1 100644 --- a/lobject.h +++ b/lobject.h @@ -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 ** See Copyright Notice in lua.h */ @@ -249,7 +249,7 @@ typedef union TString { CommonHeader; lu_byte reserved; unsigned int hash; - size_t len; + size_t len; /* number of characters in string */ } tsv; } TString; @@ -270,7 +270,7 @@ typedef union Udata { CommonHeader; struct Table *metatable; struct Table *env; - size_t len; + size_t len; /* number of bytes */ } uv; } Udata; diff --git a/lstring.c b/lstring.c index 4144cb18..73c3c15f 100644 --- a/lstring.c +++ b/lstring.c @@ -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) ** 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 = gch(o)->next) { TString *ts = rawgco2ts(o); - if (h == ts->tsv.hash && ts->tsv.len == l && - (memcmp(str, getstr(ts), l) == 0)) { + if (h == ts->tsv.hash && + 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)? */ changewhite(o); /* resurrect it */ return ts; diff --git a/lstrlib.c b/lstrlib.c index 656953b8..90dba169 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -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 ** See Copyright Notice in lua.h */ @@ -118,10 +118,10 @@ static int str_rep (lua_State *L) { luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, totallen); while (n-- > 1) { /* first n-1 copies (followed by separator) */ - memcpy(p, s, l); p += l; - memcpy(p, sep, lsep); p += lsep; + memcpy(p, s, l * sizeof(char)); p += l; + 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); } return 1; @@ -820,7 +820,7 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { if (isdigit(uchar(*p))) luaL_error(L, "invalid format (width or precision too long)"); *(form++) = '%'; - memcpy(form, strfrmt, p - strfrmt + 1); + memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char)); form += p - strfrmt + 1; *form = '\0'; return p; diff --git a/lua.c b/lua.c index c41a32a6..46783fc2 100644 --- a/lua.c +++ b/lua.c @@ -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 ** 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 */ #define EOFMARK "" -#define marklen (sizeof(EOFMARK) - 1) +#define marklen (sizeof(EOFMARK)/sizeof(char) - 1) static int incomplete (lua_State *L, int status) { if (status == LUA_ERRSYNTAX) { -- cgit v1.2.3-55-g6feb