aboutsummaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-08-17 15:59:28 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-08-17 15:59:28 -0300
commit9b4f39ab14fb2e55345c3d23537d129dac23b091 (patch)
treec7e11448e4d6eb1fec6e0a6d58544f17b1a39e10 /lvm.c
parentf4211a5ea4e235ccfa8b8dfa46031c23e9e839e2 (diff)
downloadlua-9b4f39ab14fb2e55345c3d23537d129dac23b091.tar.gz
lua-9b4f39ab14fb2e55345c3d23537d129dac23b091.tar.bz2
lua-9b4f39ab14fb2e55345c3d23537d129dac23b091.zip
More disciplined use of 'getstr' and 'tsslen'
We may want to add other string variants in the future; this change documents better where the code may need to handle those variants.
Diffstat (limited to 'lvm.c')
-rw-r--r--lvm.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/lvm.c b/lvm.c
index a98aaceb..4d71cfff 100644
--- a/lvm.c
+++ b/lvm.c
@@ -91,8 +91,10 @@ static int l_strton (const TValue *obj, TValue *result) {
91 lua_assert(obj != result); 91 lua_assert(obj != result);
92 if (!cvt2num(obj)) /* is object not a string? */ 92 if (!cvt2num(obj)) /* is object not a string? */
93 return 0; 93 return 0;
94 else 94 else {
95 return (luaO_str2num(svalue(obj), result) == vslen(obj) + 1); 95 TString *st = tsvalue(obj);
96 return (luaO_str2num(getstr(st), result) == tsslen(st) + 1);
97 }
96} 98}
97 99
98 100
@@ -626,8 +628,9 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
626static void copy2buff (StkId top, int n, char *buff) { 628static void copy2buff (StkId top, int n, char *buff) {
627 size_t tl = 0; /* size already copied */ 629 size_t tl = 0; /* size already copied */
628 do { 630 do {
629 size_t l = vslen(s2v(top - n)); /* length of string being copied */ 631 TString *st = tsvalue(s2v(top - n));
630 memcpy(buff + tl, svalue(s2v(top - n)), l * sizeof(char)); 632 size_t l = tsslen(st); /* length of string being copied */
633 memcpy(buff + tl, getstr(st), l * sizeof(char));
631 tl += l; 634 tl += l;
632 } while (--n > 0); 635 } while (--n > 0);
633} 636}
@@ -653,11 +656,11 @@ void luaV_concat (lua_State *L, int total) {
653 } 656 }
654 else { 657 else {
655 /* at least two non-empty string values; get as many as possible */ 658 /* at least two non-empty string values; get as many as possible */
656 size_t tl = vslen(s2v(top - 1)); 659 size_t tl = tsslen(tsvalue(s2v(top - 1)));
657 TString *ts; 660 TString *ts;
658 /* collect total length and number of strings */ 661 /* collect total length and number of strings */
659 for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { 662 for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
660 size_t l = vslen(s2v(top - n - 1)); 663 size_t l = tsslen(tsvalue(s2v(top - n - 1)));
661 if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { 664 if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) {
662 L->top.p = top - total; /* pop strings to avoid wasting stack */ 665 L->top.p = top - total; /* pop strings to avoid wasting stack */
663 luaG_runerror(L, "string length overflow"); 666 luaG_runerror(L, "string length overflow");
@@ -671,7 +674,7 @@ void luaV_concat (lua_State *L, int total) {
671 } 674 }
672 else { /* long string; copy strings directly to final result */ 675 else { /* long string; copy strings directly to final result */
673 ts = luaS_createlngstrobj(L, tl); 676 ts = luaS_createlngstrobj(L, tl);
674 copy2buff(top, n, getstr(ts)); 677 copy2buff(top, n, getlngstr(ts));
675 } 678 }
676 setsvalue2s(L, top - n, ts); /* create result */ 679 setsvalue2s(L, top - n, ts); /* create result */
677 } 680 }