Coverage Report

Created: 2026-02-26 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/testdir/build/lua-master/source/lstring.c
Line
Count
Source
1
/*
2
** $Id: lstring.c $
3
** String table (keeps all strings handled by Lua)
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lstring_c
8
#define LUA_CORE
9
10
#include "lprefix.h"
11
12
13
#include <string.h>
14
15
#include "lua.h"
16
17
#include "ldebug.h"
18
#include "ldo.h"
19
#include "lmem.h"
20
#include "lobject.h"
21
#include "lstate.h"
22
#include "lstring.h"
23
24
25
/*
26
** Maximum size for string table.
27
*/
28
107
#define MAXSTRTB  cast_int(luaM_limitN(INT_MAX, TString*))
29
30
/*
31
** Initial size for the string table (must be power of 2).
32
** The Lua core alone registers ~50 strings (reserved words +
33
** metaevent keys + a few others). Libraries would typically add
34
** a few dozens more.
35
*/
36
#if !defined(MINSTRTABSIZE)
37
978
#define MINSTRTABSIZE   128
38
#endif
39
40
41
/*
42
** generic equality for strings
43
*/
44
69.4k
int luaS_eqstr (TString *a, TString *b) {
45
69.4k
  size_t len1, len2;
46
69.4k
  const char *s1 = getlstr(a, len1);
47
69.4k
  const char *s2 = getlstr(b, len2);
48
69.4k
  return ((len1 == len2) &&  /* equal length and ... */
49
42.4k
          (memcmp(s1, s2, len1) == 0));  /* equal contents */
50
69.4k
}
51
52
53
27.9M
static unsigned luaS_hash (const char *str, size_t l, unsigned seed) {
54
27.9M
  unsigned int h = seed ^ cast_uint(l);
55
86.3M
  for (; l > 0; l--)
56
58.4M
    h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
57
27.9M
  return h;
58
27.9M
}
59
60
61
71.6k
unsigned luaS_hashlongstr (TString *ts) {
62
71.6k
  lua_assert(ts->tt == LUA_VLNGSTR);
63
71.6k
  if (ts->extra == 0) {  /* no hash? */
64
16.8k
    size_t len = ts->u.lnglen;
65
16.8k
    ts->hash = luaS_hash(getlngstr(ts), len, ts->hash);
66
16.8k
    ts->extra = 1;  /* now it has its hash */
67
16.8k
  }
68
71.6k
  return ts->hash;
69
71.6k
}
70
71
72
598
static void tablerehash (TString **vect, int osize, int nsize) {
73
598
  int i;
74
82.7k
  for (i = osize; i < nsize; i++)  /* clear new elements */
75
82.1k
    vect[i] = NULL;
76
28.3k
  for (i = 0; i < osize; i++) {  /* rehash old part of the array */
77
27.7k
    TString *p = vect[i];
78
27.7k
    vect[i] = NULL;
79
48.0k
    while (p) {  /* for each string in the list */
80
20.2k
      TString *hnext = p->u.hnext;  /* save next */
81
20.2k
      unsigned int h = lmod(p->hash, nsize);  /* new position */
82
20.2k
      p->u.hnext = vect[h];  /* chain it into array */
83
20.2k
      vect[h] = p;
84
20.2k
      p = hnext;
85
20.2k
    }
86
27.7k
  }
87
598
}
88
89
90
/*
91
** Resize the string table. If allocation fails, keep the current size.
92
** (This can degrade performance, but any non-zero size should work
93
** correctly.)
94
*/
95
109
void luaS_resize (lua_State *L, int nsize) {
96
109
  stringtable *tb = &G(L)->strt;
97
109
  int osize = tb->size;
98
109
  TString **newvect;
99
109
  if (nsize < osize)  /* shrinking table? */
100
2
    tablerehash(tb->hash, osize, nsize);  /* depopulate shrinking part */
101
109
  newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
102
109
  if (l_unlikely(newvect == NULL)) {  /* reallocation failed? */
103
0
    if (nsize < osize)  /* was it shrinking table? */
104
0
      tablerehash(tb->hash, nsize, osize);  /* restore to original size */
105
    /* leave table as it was */
106
0
  }
107
109
  else {  /* allocation succeeded */
108
109
    tb->hash = newvect;
109
109
    tb->size = nsize;
110
109
    if (nsize > osize)
111
107
      tablerehash(newvect, osize, nsize);  /* rehash for new size */
112
109
  }
113
109
}
114
115
116
/*
117
** Clear API string cache. (Entries cannot be empty, so fill them with
118
** a non-collectable string.)
119
*/
120
1.70k
void luaS_clearcache (global_State *g) {
121
1.70k
  int i, j;
122
91.9k
  for (i = 0; i < STRCACHE_N; i++)
123
270k
    for (j = 0; j < STRCACHE_M; j++) {
124
180k
      if (iswhite(g->strcache[i][j]))  /* will entry be collected? */
125
10
        g->strcache[i][j] = g->memerrmsg;  /* replace it with something fixed */
126
180k
    }
127
1.70k
}
128
129
130
/*
131
** Initialize the string table and the string cache
132
*/
133
489
void luaS_init (lua_State *L) {
134
489
  global_State *g = G(L);
135
489
  int i, j;
136
489
  stringtable *tb = &G(L)->strt;
137
489
  tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*);
138
489
  tablerehash(tb->hash, 0, MINSTRTABSIZE);  /* clear array */
139
489
  tb->size = MINSTRTABSIZE;
140
  /* pre-create memory-error message */
141
489
  g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
142
489
  luaC_fix(L, obj2gco(g->memerrmsg));  /* it should never be collected */
143
26.4k
  for (i = 0; i < STRCACHE_N; i++)  /* fill cache with valid strings */
144
77.7k
    for (j = 0; j < STRCACHE_M; j++)
145
51.8k
      g->strcache[i][j] = g->memerrmsg;
146
489
}
147
148
149
67.5k
size_t luaS_sizelngstr (size_t len, int kind) {
150
67.5k
  switch (kind) {
151
67.4k
    case LSTRREG:  /* regular long string */
152
      /* don't need 'falloc'/'ud', but need space for content */
153
67.4k
      return offsetof(TString, falloc) + (len + 1) * sizeof(char);
154
0
    case LSTRFIX:  /* fixed external long string */
155
      /* don't need 'falloc'/'ud' */
156
0
      return offsetof(TString, falloc);
157
186
    default:  /* external long string with deallocation */
158
186
      lua_assert(kind == LSTRMEM);
159
186
      return sizeof(TString);
160
67.5k
  }
161
67.5k
}
162
163
164
/*
165
** creates a new string object
166
*/
167
static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag,
168
71.7k
                              unsigned h) {
169
71.7k
  TString *ts;
170
71.7k
  GCObject *o;
171
71.7k
  o = luaC_newobj(L, tag, totalsize);
172
71.7k
  ts = gco2ts(o);
173
71.7k
  ts->hash = h;
174
71.7k
  ts->extra = 0;
175
71.7k
  return ts;
176
71.7k
}
177
178
179
19.9k
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
180
19.9k
  size_t totalsize = luaS_sizelngstr(l, LSTRREG);
181
19.9k
  TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed);
182
19.9k
  ts->u.lnglen = l;
183
19.9k
  ts->shrlen = LSTRREG;  /* signals that it is a regular long string */
184
19.9k
  ts->contents = cast_charp(ts) + offsetof(TString, falloc);
185
19.9k
  ts->contents[l] = '\0';  /* ending 0 */
186
19.9k
  return ts;
187
19.9k
}
188
189
190
51.7k
void luaS_remove (lua_State *L, TString *ts) {
191
51.7k
  stringtable *tb = &G(L)->strt;
192
51.7k
  TString **p = &tb->hash[lmod(ts->hash, tb->size)];
193
55.4k
  while (*p != ts)  /* find previous element */
194
3.77k
    p = &(*p)->u.hnext;
195
51.7k
  *p = (*p)->u.hnext;  /* remove element from its list */
196
51.7k
  tb->nuse--;
197
51.7k
}
198
199
200
107
static void growstrtab (lua_State *L, stringtable *tb) {
201
107
  if (l_unlikely(tb->nuse == INT_MAX)) {  /* too many strings? */
202
0
    luaC_fullgc(L, 1);  /* try to free some... */
203
0
    if (tb->nuse == INT_MAX)  /* still too many? */
204
0
      luaM_error(L);  /* cannot even create a message... */
205
0
  }
206
107
  if (tb->size <= MAXSTRTB / 2)  /* can grow string table? */
207
107
    luaS_resize(L, tb->size * 2);
208
107
}
209
210
211
/*
212
** Checks whether short string exists and reuses it or creates a new one.
213
*/
214
27.9M
static TString *internshrstr (lua_State *L, const char *str, size_t l) {
215
27.9M
  TString *ts;
216
27.9M
  global_State *g = G(L);
217
27.9M
  stringtable *tb = &g->strt;
218
27.9M
  unsigned int h = luaS_hash(str, l, g->seed);
219
27.9M
  TString **list = &tb->hash[lmod(h, tb->size)];
220
27.9M
  lua_assert(str != NULL);  /* otherwise 'memcmp'/'memcpy' are undefined */
221
31.4M
  for (ts = *list; ts != NULL; ts = ts->u.hnext) {
222
31.3M
    if (l == cast_uint(ts->shrlen) &&
223
27.8M
        (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) {
224
      /* found! */
225
27.8M
      if (isdead(g, ts))  /* dead (but not collected yet)? */
226
76
        changewhite(ts);  /* resurrect it */
227
27.8M
      return ts;
228
27.8M
    }
229
31.3M
  }
230
  /* else must create a new string */
231
51.7k
  if (tb->nuse >= tb->size) {  /* need to grow string table? */
232
107
    growstrtab(L, tb);
233
107
    list = &tb->hash[lmod(h, tb->size)];  /* rehash with new size */
234
107
  }
235
51.7k
  ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h);
236
51.7k
  ts->shrlen = cast(ls_byte, l);
237
51.7k
  getshrstr(ts)[l] = '\0';  /* ending 0 */
238
51.7k
  memcpy(getshrstr(ts), str, l * sizeof(char));
239
51.7k
  ts->u.hnext = *list;
240
51.7k
  *list = ts;
241
51.7k
  tb->nuse++;
242
51.7k
  return ts;
243
51.7k
}
244
245
246
/*
247
** new string (with explicit length)
248
*/
249
27.9M
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
250
27.9M
  if (l <= LUAI_MAXSHORTLEN)  /* short string? */
251
27.9M
    return internshrstr(L, str, l);
252
17.7k
  else {
253
17.7k
    TString *ts;
254
17.7k
    if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString))))
255
0
      luaM_toobig(L);
256
17.7k
    ts = luaS_createlngstrobj(L, l);
257
17.7k
    memcpy(getlngstr(ts), str, l * sizeof(char));
258
17.7k
    return ts;
259
17.7k
  }
260
27.9M
}
261
262
263
/*
264
** Create or reuse a zero-terminated string, first checking in the
265
** cache (using the string address as a key). The cache can contain
266
** only zero-terminated strings, so it is safe to use 'strcmp' to
267
** check hits.
268
*/
269
24.5k
TString *luaS_new (lua_State *L, const char *str) {
270
24.5k
  unsigned int i = point2uint(str) % STRCACHE_N;  /* hash */
271
24.5k
  int j;
272
24.5k
  TString **p = G(L)->strcache[i];
273
73.0k
  for (j = 0; j < STRCACHE_M; j++) {
274
48.7k
    if (strcmp(str, getstr(p[j])) == 0)  /* hit? */
275
305
      return p[j];  /* that is it */
276
48.7k
  }
277
  /* normal route */
278
48.4k
  for (j = STRCACHE_M - 1; j > 0; j--)
279
24.2k
    p[j] = p[j - 1];  /* move out last element */
280
  /* new element is first in the list */
281
24.2k
  p[0] = luaS_newlstr(L, str, strlen(str));
282
24.2k
  return p[0];
283
24.5k
}
284
285
286
61
Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) {
287
61
  Udata *u;
288
61
  int i;
289
61
  GCObject *o;
290
61
  if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
291
0
    luaM_toobig(L);
292
61
  o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
293
61
  u = gco2u(o);
294
61
  u->len = s;
295
61
  u->nuvalue = nuvalue;
296
61
  u->metatable = NULL;
297
61
  for (i = 0; i < nuvalue; i++)
298
61
    setnilvalue(&u->uv[i].uv);
299
61
  return u;
300
61
}
301
302
303
struct NewExt {
304
  ls_byte kind;
305
  const char *s;
306
   size_t len;
307
  TString *ts;  /* output */
308
};
309
310
311
61
static void f_newext (lua_State *L, void *ud) {
312
61
  struct NewExt *ne = cast(struct NewExt *, ud);
313
61
  size_t size = luaS_sizelngstr(0, ne->kind);
314
61
  ne->ts = createstrobj(L, size, LUA_VLNGSTR, G(L)->seed);
315
61
}
316
317
318
TString *luaS_newextlstr (lua_State *L,
319
61
            const char *s, size_t len, lua_Alloc falloc, void *ud) {
320
61
  struct NewExt ne;
321
61
  if (!falloc) {
322
0
    ne.kind = LSTRFIX;
323
0
    f_newext(L, &ne);  /* just create header */
324
0
  }
325
61
  else {
326
61
    ne.kind = LSTRMEM;
327
61
    if (luaD_rawrunprotected(L, f_newext, &ne) != LUA_OK) {  /* mem. error? */
328
0
      (*falloc)(ud, cast_voidp(s), len + 1, 0);  /* free external string */
329
0
      luaM_error(L);  /* re-raise memory error */
330
0
    }
331
61
    ne.ts->falloc = falloc;
332
61
    ne.ts->ud = ud;
333
61
  }
334
61
  ne.ts->shrlen = ne.kind;
335
61
  ne.ts->u.lnglen = len;
336
61
  ne.ts->contents = cast_charp(s);
337
61
  return ne.ts;
338
61
}
339
340
341
/*
342
** Normalize an external string: If it is short, internalize it.
343
*/
344
0
TString *luaS_normstr (lua_State *L, TString *ts) {
345
0
  size_t len = ts->u.lnglen;
346
0
  if (len > LUAI_MAXSHORTLEN)
347
0
    return ts;  /* long string; keep the original */
348
0
  else {
349
0
    const char *str = getlngstr(ts);
350
0
    return internshrstr(L, str, len);
351
0
  }
352
0
}
353