diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-05-03 13:01:57 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-05-03 13:01:57 -0300 |
commit | ad2531a0eefb6950d589c0d508b1f959f3f58d8b (patch) | |
tree | 706eca04d13655367b3743bd6a4e8c544a2b0928 | |
parent | bc1c718cc02d4a0163110cb21bcbdd2985e4d28d (diff) | |
download | lua-ad2531a0eefb6950d589c0d508b1f959f3f58d8b.tar.gz lua-ad2531a0eefb6950d589c0d508b1f959f3f58d8b.tar.bz2 lua-ad2531a0eefb6950d589c0d508b1f959f3f58d8b.zip |
more complete (and hopefuly more correct) handling of 'sizeof(char)'
-rw-r--r-- | lauxlib.c | 10 | ||||
-rw-r--r-- | lobject.c | 16 | ||||
-rw-r--r-- | lobject.h | 6 | ||||
-rw-r--r-- | lstring.c | 7 | ||||
-rw-r--r-- | lstrlib.c | 10 | ||||
-rw-r--r-- | lua.c | 4 |
6 files changed, 29 insertions, 24 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.230 2011/04/08 19:17:36 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.231 2011/04/19 18:29:41 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -442,8 +442,10 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { | |||
442 | newsize = B->n + sz; | 442 | newsize = B->n + sz; |
443 | if (newsize < B->n || newsize - B->n < sz) | 443 | if (newsize < B->n || newsize - B->n < sz) |
444 | luaL_error(L, "buffer too large"); | 444 | luaL_error(L, "buffer too large"); |
445 | newbuff = (char *)lua_newuserdata(L, newsize); /* create larger buffer */ | 445 | /* create larger buffer */ |
446 | memcpy(newbuff, B->b, B->n); /* move content to new buffer */ | 446 | newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char)); |
447 | /* move content to new buffer */ | ||
448 | memcpy(newbuff, B->b, B->n * sizeof(char)); | ||
447 | if (buffonstack(B)) | 449 | if (buffonstack(B)) |
448 | lua_remove(L, -2); /* remove old buffer */ | 450 | lua_remove(L, -2); /* remove old buffer */ |
449 | B->b = newbuff; | 451 | B->b = newbuff; |
@@ -455,7 +457,7 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { | |||
455 | 457 | ||
456 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { | 458 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { |
457 | char *b = luaL_prepbuffsize(B, l); | 459 | char *b = luaL_prepbuffsize(B, l); |
458 | memcpy(b, s, l); | 460 | memcpy(b, s, l * sizeof(char)); |
459 | luaL_addsize(B, l); | 461 | luaL_addsize(B, l); |
460 | } | 462 | } |
461 | 463 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.c,v 2.46 2011/02/07 19:15:24 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.47 2011/04/05 18:32:06 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 | */ |
@@ -264,19 +264,20 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { | |||
264 | } | 264 | } |
265 | 265 | ||
266 | 266 | ||
267 | /* number of chars of a literal string without the ending \0 */ | ||
268 | #define LL(x) (sizeof(x)/sizeof(char) - 1) | ||
267 | 269 | ||
268 | #define LL(x) ((sizeof(x) - 1)/sizeof(char)) | ||
269 | #define RETS "..." | 270 | #define RETS "..." |
270 | #define PRE "[string \"" | 271 | #define PRE "[string \"" |
271 | #define POS "\"]" | 272 | #define POS "\"]" |
272 | 273 | ||
273 | #define addstr(a,b,l) ( memcpy(a,b,l), a += (l) ) | 274 | #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) |
274 | 275 | ||
275 | void luaO_chunkid (char *out, const char *source, size_t bufflen) { | 276 | void luaO_chunkid (char *out, const char *source, size_t bufflen) { |
276 | size_t l = strlen(source); | 277 | size_t l = strlen(source); |
277 | if (*source == '=') { /* 'literal' source */ | 278 | if (*source == '=') { /* 'literal' source */ |
278 | if (l <= bufflen) /* small enough? */ | 279 | if (l <= bufflen) /* small enough? */ |
279 | memcpy(out, source + 1, l); | 280 | memcpy(out, source + 1, l * sizeof(char)); |
280 | else { /* truncate it */ | 281 | else { /* truncate it */ |
281 | addstr(out, source + 1, bufflen - 1); | 282 | addstr(out, source + 1, bufflen - 1); |
282 | *out = '\0'; | 283 | *out = '\0'; |
@@ -284,11 +285,11 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) { | |||
284 | } | 285 | } |
285 | else if (*source == '@') { /* file name */ | 286 | else if (*source == '@') { /* file name */ |
286 | if (l <= bufflen) /* small enough? */ | 287 | if (l <= bufflen) /* small enough? */ |
287 | memcpy(out, source + 1, l); | 288 | memcpy(out, source + 1, l * sizeof(char)); |
288 | else { /* add '...' before rest of name */ | 289 | else { /* add '...' before rest of name */ |
289 | addstr(out, RETS, LL(RETS)); | 290 | addstr(out, RETS, LL(RETS)); |
290 | bufflen -= LL(RETS); | 291 | bufflen -= LL(RETS); |
291 | memcpy(out, source + 1 + l - bufflen, bufflen); | 292 | memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); |
292 | } | 293 | } |
293 | } | 294 | } |
294 | else { /* string; format as [string "source"] */ | 295 | else { /* string; format as [string "source"] */ |
@@ -304,6 +305,7 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) { | |||
304 | addstr(out, source, l); | 305 | addstr(out, source, l); |
305 | addstr(out, RETS, LL(RETS)); | 306 | addstr(out, RETS, LL(RETS)); |
306 | } | 307 | } |
307 | memcpy(out, POS, LL(POS) + 1); | 308 | memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); |
308 | } | 309 | } |
309 | } | 310 | } |
311 | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.h,v 2.48 2011/04/05 14:24:07 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 2.49 2011/04/07 16:11:57 roberto Exp roberto $ |
3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -249,7 +249,7 @@ typedef union TString { | |||
249 | CommonHeader; | 249 | CommonHeader; |
250 | lu_byte reserved; | 250 | lu_byte reserved; |
251 | unsigned int hash; | 251 | unsigned int hash; |
252 | size_t len; | 252 | size_t len; /* number of characters in string */ |
253 | } tsv; | 253 | } tsv; |
254 | } TString; | 254 | } TString; |
255 | 255 | ||
@@ -270,7 +270,7 @@ typedef union Udata { | |||
270 | CommonHeader; | 270 | CommonHeader; |
271 | struct Table *metatable; | 271 | struct Table *metatable; |
272 | struct Table *env; | 272 | struct Table *env; |
273 | size_t len; | 273 | size_t len; /* number of bytes */ |
274 | } uv; | 274 | } uv; |
275 | } Udata; | 275 | } Udata; |
276 | 276 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 2.17 2010/04/03 20:24:18 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 2.18 2010/05/10 18:23:45 roberto Exp roberto $ |
3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -84,8 +84,9 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
84 | o != NULL; | 84 | o != NULL; |
85 | o = gch(o)->next) { | 85 | o = gch(o)->next) { |
86 | TString *ts = rawgco2ts(o); | 86 | TString *ts = rawgco2ts(o); |
87 | if (h == ts->tsv.hash && ts->tsv.len == l && | 87 | if (h == ts->tsv.hash && |
88 | (memcmp(str, getstr(ts), l) == 0)) { | 88 | ts->tsv.len == l && |
89 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { | ||
89 | if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ | 90 | if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ |
90 | changewhite(o); /* resurrect it */ | 91 | changewhite(o); /* resurrect it */ |
91 | return ts; | 92 | return ts; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.165 2011/03/18 19:02:33 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.166 2011/04/20 16:36:28 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 | */ |
@@ -118,10 +118,10 @@ static int str_rep (lua_State *L) { | |||
118 | luaL_Buffer b; | 118 | luaL_Buffer b; |
119 | char *p = luaL_buffinitsize(L, &b, totallen); | 119 | char *p = luaL_buffinitsize(L, &b, totallen); |
120 | while (n-- > 1) { /* first n-1 copies (followed by separator) */ | 120 | while (n-- > 1) { /* first n-1 copies (followed by separator) */ |
121 | memcpy(p, s, l); p += l; | 121 | memcpy(p, s, l * sizeof(char)); p += l; |
122 | memcpy(p, sep, lsep); p += lsep; | 122 | memcpy(p, sep, lsep * sizeof(char)); p += lsep; |
123 | } | 123 | } |
124 | memcpy(p, s, l); /* last copy (not followed by separator) */ | 124 | memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ |
125 | luaL_pushresultsize(&b, totallen); | 125 | luaL_pushresultsize(&b, totallen); |
126 | } | 126 | } |
127 | return 1; | 127 | return 1; |
@@ -820,7 +820,7 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { | |||
820 | if (isdigit(uchar(*p))) | 820 | if (isdigit(uchar(*p))) |
821 | luaL_error(L, "invalid format (width or precision too long)"); | 821 | luaL_error(L, "invalid format (width or precision too long)"); |
822 | *(form++) = '%'; | 822 | *(form++) = '%'; |
823 | memcpy(form, strfrmt, p - strfrmt + 1); | 823 | memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char)); |
824 | form += p - strfrmt + 1; | 824 | form += p - strfrmt + 1; |
825 | *form = '\0'; | 825 | *form = '\0'; |
826 | return p; | 826 | return p; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.196 2011/02/07 12:27:13 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.197 2011/03/14 15:39:42 roberto Exp roberto $ |
3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -246,7 +246,7 @@ static const char *get_prompt (lua_State *L, int firstline) { | |||
246 | 246 | ||
247 | /* mark in error messages for incomplete statements */ | 247 | /* mark in error messages for incomplete statements */ |
248 | #define EOFMARK "<eof>" | 248 | #define EOFMARK "<eof>" |
249 | #define marklen (sizeof(EOFMARK) - 1) | 249 | #define marklen (sizeof(EOFMARK)/sizeof(char) - 1) |
250 | 250 | ||
251 | static int incomplete (lua_State *L, int status) { | 251 | static int incomplete (lua_State *L, int status) { |
252 | if (status == LUA_ERRSYNTAX) { | 252 | if (status == LUA_ERRSYNTAX) { |