aboutsummaryrefslogtreecommitdiff
path: root/lobject.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-05-03 13:01:57 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-05-03 13:01:57 -0300
commitad2531a0eefb6950d589c0d508b1f959f3f58d8b (patch)
tree706eca04d13655367b3743bd6a4e8c544a2b0928 /lobject.c
parentbc1c718cc02d4a0163110cb21bcbdd2985e4d28d (diff)
downloadlua-ad2531a0eefb6950d589c0d508b1f959f3f58d8b.tar.gz
lua-ad2531a0eefb6950d589c0d508b1f959f3f58d8b.tar.bz2
lua-ad2531a0eefb6950d589c0d508b1f959f3f58d8b.zip
more complete (and hopefuly more correct) handling of 'sizeof(char)'
Diffstat (limited to 'lobject.c')
-rw-r--r--lobject.c16
1 files changed, 9 insertions, 7 deletions
diff --git a/lobject.c b/lobject.c
index 0fd44626..bd541a1e 100644
--- a/lobject.c
+++ b/lobject.c
@@ -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
275void luaO_chunkid (char *out, const char *source, size_t bufflen) { 276void 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