diff options
Diffstat (limited to 'lobject.c')
-rw-r--r-- | lobject.c | 27 |
1 files changed, 12 insertions, 15 deletions
@@ -473,45 +473,42 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { | |||
473 | } | 473 | } |
474 | 474 | ||
475 | 475 | ||
476 | /* number of chars of a literal string without the ending \0 */ | ||
477 | #define LL(x) (sizeof(x)/sizeof(char) - 1) | ||
478 | |||
479 | #define RETS "..." | 476 | #define RETS "..." |
480 | #define PRE "[string \"" | 477 | #define PRE "[string \"" |
481 | #define POS "\"]" | 478 | #define POS "\"]" |
482 | 479 | ||
483 | #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) | 480 | #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) |
484 | 481 | ||
485 | void luaO_chunkid (char *out, const char *source, size_t bufflen) { | 482 | void luaO_chunkid (char *out, const char *source, size_t srclen) { |
486 | size_t l = strlen(source); | 483 | size_t bufflen = LUA_IDSIZE; /* free space in buffer */ |
487 | if (*source == '=') { /* 'literal' source */ | 484 | if (*source == '=') { /* 'literal' source */ |
488 | if (l <= bufflen) /* small enough? */ | 485 | if (srclen <= bufflen) /* small enough? */ |
489 | memcpy(out, source + 1, l * sizeof(char)); | 486 | memcpy(out, source + 1, srclen * sizeof(char)); |
490 | else { /* truncate it */ | 487 | else { /* truncate it */ |
491 | addstr(out, source + 1, bufflen - 1); | 488 | addstr(out, source + 1, bufflen - 1); |
492 | *out = '\0'; | 489 | *out = '\0'; |
493 | } | 490 | } |
494 | } | 491 | } |
495 | else if (*source == '@') { /* file name */ | 492 | else if (*source == '@') { /* file name */ |
496 | if (l <= bufflen) /* small enough? */ | 493 | if (srclen <= bufflen) /* small enough? */ |
497 | memcpy(out, source + 1, l * sizeof(char)); | 494 | memcpy(out, source + 1, srclen * sizeof(char)); |
498 | else { /* add '...' before rest of name */ | 495 | else { /* add '...' before rest of name */ |
499 | addstr(out, RETS, LL(RETS)); | 496 | addstr(out, RETS, LL(RETS)); |
500 | bufflen -= LL(RETS); | 497 | bufflen -= LL(RETS); |
501 | memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); | 498 | memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char)); |
502 | } | 499 | } |
503 | } | 500 | } |
504 | else { /* string; format as [string "source"] */ | 501 | else { /* string; format as [string "source"] */ |
505 | const char *nl = strchr(source, '\n'); /* find first new line (if any) */ | 502 | const char *nl = strchr(source, '\n'); /* find first new line (if any) */ |
506 | addstr(out, PRE, LL(PRE)); /* add prefix */ | 503 | addstr(out, PRE, LL(PRE)); /* add prefix */ |
507 | bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ | 504 | bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ |
508 | if (l < bufflen && nl == NULL) { /* small one-line source? */ | 505 | if (srclen < bufflen && nl == NULL) { /* small one-line source? */ |
509 | addstr(out, source, l); /* keep it */ | 506 | addstr(out, source, srclen); /* keep it */ |
510 | } | 507 | } |
511 | else { | 508 | else { |
512 | if (nl != NULL) l = nl - source; /* stop at first newline */ | 509 | if (nl != NULL) srclen = nl - source; /* stop at first newline */ |
513 | if (l > bufflen) l = bufflen; | 510 | if (srclen > bufflen) srclen = bufflen; |
514 | addstr(out, source, l); | 511 | addstr(out, source, srclen); |
515 | addstr(out, RETS, LL(RETS)); | 512 | addstr(out, RETS, LL(RETS)); |
516 | } | 513 | } |
517 | memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); | 514 | memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); |