Coverage Report

Created: 2025-08-28 06:25

/src/testdir/build/lua-master/source/lstring.c
Line
Count
Source (jump to first uncovered line)
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
79
#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
738
#define MINSTRTABSIZE   128
38
#endif
39
40
41
/*
42
** generic equality for strings
43
*/
44
21.4k
int luaS_eqstr (TString *a, TString *b) {
45
21.4k
  size_t len1, len2;
46
21.4k
  const char *s1 = getlstr(a, len1);
47
21.4k
  const char *s2 = getlstr(b, len2);
48
21.4k
  return ((len1 == len2) &&  /* equal length and ... */
49
21.4k
          (memcmp(s1, s2, len1) == 0));  /* equal contents */
50
21.4k
}
51
52
53
21.9M
unsigned luaS_hash (const char *str, size_t l, unsigned seed) {
54
21.9M
  unsigned int h = seed ^ cast_uint(l);
55
63.9M
  for (; l > 0; l--)
56
42.0M
    h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
57
21.9M
  return h;
58
21.9M
}
59
60
61
24.9k
unsigned luaS_hashlongstr (TString *ts) {
62
24.9k
  lua_assert(ts->tt == LUA_VLNGSTR);
63
24.9k
  if (ts->extra == 0) {  /* no hash? */
64
5.13k
    size_t len = ts->u.lnglen;
65
5.13k
    ts->hash = luaS_hash(getlngstr(ts), len, ts->hash);
66
0
    ts->extra = 1;  /* now it has its hash */
67
5.13k
  }
68
24.9k
  return ts->hash;
69
24.9k
}
70
71
72
448
static void tablerehash (TString **vect, int osize, int nsize) {
73
448
  int i;
74
57.7k
  for (i = osize; i < nsize; i++)  /* clear new elements */
75
57.3k
    vect[i] = NULL;
76
10.5k
  for (i = 0; i < osize; i++) {  /* rehash old part of the array */
77
10.1k
    TString *p = vect[i];
78
10.1k
    vect[i] = NULL;
79
20.2k
    while (p) {  /* for each string in the list */
80
10.1k
      TString *hnext = p->u.hnext;  /* save next */
81
10.1k
      unsigned int h = lmod(p->hash, nsize);  /* new position */
82
0
      p->u.hnext = vect[h];  /* chain it into array */
83
10.1k
      vect[h] = p;
84
10.1k
      p = hnext;
85
10.1k
    }
86
10.1k
  }
87
448
}
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
79
void luaS_resize (lua_State *L, int nsize) {
96
79
  stringtable *tb = &G(L)->strt;
97
79
  int osize = tb->size;
98
79
  TString **newvect;
99
79
  if (nsize < osize)  /* shrinking table? */
100
0
    tablerehash(tb->hash, osize, nsize);  /* depopulate shrinking part */
101
79
  newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
102
79
  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
79
  else {  /* allocation succeeded */
108
79
    tb->hash = newvect;
109
79
    tb->size = nsize;
110
79
    if (nsize > osize)
111
79
      tablerehash(newvect, osize, nsize);  /* rehash for new size */
112
79
  }
113
79
}
114
115
116
/*
117
** Clear API string cache. (Entries cannot be empty, so fill them with
118
** a non-collectable string.)
119
*/
120
1.15k
void luaS_clearcache (global_State *g) {
121
1.15k
  int i, j;
122
62.1k
  for (i = 0; i < STRCACHE_N; i++)
123
182k
    for (j = 0; j < STRCACHE_M; j++) {
124
121k
      if (iswhite(g->strcache[i][j]))  /* will entry be collected? */
125
11
        g->strcache[i][j] = g->memerrmsg;  /* replace it with something fixed */
126
121k
    }
127
1.15k
}
128
129
130
/*
131
** Initialize the string table and the string cache
132
*/
133
369
void luaS_init (lua_State *L) {
134
369
  global_State *g = G(L);
135
369
  int i, j;
136
369
  stringtable *tb = &G(L)->strt;
137
369
  tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*);
138
369
  tablerehash(tb->hash, 0, MINSTRTABSIZE);  /* clear array */
139
369
  tb->size = MINSTRTABSIZE;
140
  /* pre-create memory-error message */
141
369
  g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
142
369
  luaC_fix(L, obj2gco(g->memerrmsg));  /* it should never be collected */
143
19.9k
  for (i = 0; i < STRCACHE_N; i++)  /* fill cache with valid strings */
144
58.6k
    for (j = 0; j < STRCACHE_M; j++)
145
39.1k
      g->strcache[i][j] = g->memerrmsg;
146
369
}
147
148
149
20.1k
size_t luaS_sizelngstr (size_t len, int kind) {
150
20.1k
  switch (kind) {
151
19.9k
    case LSTRREG:  /* regular long string */
152
      /* don't need 'falloc'/'ud', but need space for content */
153
19.9k
      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
139
    default:  /* external long string with deallocation */
158
139
      lua_assert(kind == LSTRMEM);
159
139
      return sizeof(TString);
160
20.1k
  }
161
20.1k
}
162
163
164
/*
165
** creates a new string object
166
*/
167
static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag,
168
41.0k
                              unsigned h) {
169
41.0k
  TString *ts;
170
41.0k
  GCObject *o;
171
41.0k
  o = luaC_newobj(L, tag, totalsize);
172
41.0k
  ts = gco2ts(o);
173
0
  ts->hash = h;
174
41.0k
  ts->extra = 0;
175
41.0k
  return ts;
176
41.0k
}
177
178
179
5.80k
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
180
5.80k
  size_t totalsize = luaS_sizelngstr(l, LSTRREG);
181
5.80k
  TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed);
182
5.80k
  ts->u.lnglen = l;
183
5.80k
  ts->shrlen = LSTRREG;  /* signals that it is a regular long string */
184
5.80k
  ts->contents = cast_charp(ts) + offsetof(TString, falloc);
185
5.80k
  ts->contents[l] = '\0';  /* ending 0 */
186
5.80k
  return ts;
187
5.80k
}
188
189
190
35.1k
void luaS_remove (lua_State *L, TString *ts) {
191
35.1k
  stringtable *tb = &G(L)->strt;
192
35.1k
  TString **p = &tb->hash[lmod(ts->hash, tb->size)];
193
37.5k
  while (*p != ts)  /* find previous element */
194
2.39k
    p = &(*p)->u.hnext;
195
35.1k
  *p = (*p)->u.hnext;  /* remove element from its list */
196
35.1k
  tb->nuse--;
197
35.1k
}
198
199
200
79
static void growstrtab (lua_State *L, stringtable *tb) {
201
79
  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
79
  if (tb->size <= MAXSTRTB / 2)  /* can grow string table? */
207
79
    luaS_resize(L, tb->size * 2);
208
79
}
209
210
211
/*
212
** Checks whether short string exists and reuses it or creates a new one.
213
*/
214
21.9M
static TString *internshrstr (lua_State *L, const char *str, size_t l) {
215
21.9M
  TString *ts;
216
21.9M
  global_State *g = G(L);
217
21.9M
  stringtable *tb = &g->strt;
218
21.9M
  unsigned int h = luaS_hash(str, l, g->seed);
219
21.9M
  TString **list = &tb->hash[lmod(h, tb->size)];
220
21.9M
  lua_assert(str != NULL);  /* otherwise 'memcmp'/'memcpy' are undefined */
221
25.9M
  for (ts = *list; ts != NULL; ts = ts->u.hnext) {
222
25.9M
    if (l == cast_uint(ts->shrlen) &&
223
25.9M
        (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) {
224
      /* found! */
225
21.9M
      if (isdead(g, ts))  /* dead (but not collected yet)? */
226
10
        changewhite(ts);  /* resurrect it */
227
21.9M
      return ts;
228
21.9M
    }
229
25.9M
  }
230
  /* else must create a new string */
231
35.1k
  if (tb->nuse >= tb->size) {  /* need to grow string table? */
232
79
    growstrtab(L, tb);
233
79
    list = &tb->hash[lmod(h, tb->size)];  /* rehash with new size */
234
79
  }
235
35.1k
  ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h);
236
35.1k
  ts->shrlen = cast(ls_byte, l);
237
35.1k
  getshrstr(ts)[l] = '\0';  /* ending 0 */
238
35.1k
  memcpy(getshrstr(ts), str, l * sizeof(char));
239
0
  ts->u.hnext = *list;
240
35.1k
  *list = ts;
241
35.1k
  tb->nuse++;
242
35.1k
  return ts;
243
35.1k
}
244
245
246
/*
247
** new string (with explicit length)
248
*/
249
21.9M
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
250
21.9M
  if (l <= LUAI_MAXSHORTLEN)  /* short string? */
251
21.9M
    return internshrstr(L, str, l);
252
5.78k
  else {
253
5.78k
    TString *ts;
254
5.78k
    if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString))))
255
0
      luaM_toobig(L);
256
5.78k
    ts = luaS_createlngstrobj(L, l);
257
5.78k
    memcpy(getlngstr(ts), str, l * sizeof(char));
258
0
    return ts;
259
5.78k
  }
260
21.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
18.4k
TString *luaS_new (lua_State *L, const char *str) {
270
18.4k
  unsigned int i = point2uint(str) % STRCACHE_N;  /* hash */
271
18.4k
  int j;
272
18.4k
  TString **p = G(L)->strcache[i];
273
55.0k
  for (j = 0; j < STRCACHE_M; j++) {
274
36.7k
    if (strcmp(str, getstr(p[j])) == 0)  /* hit? */
275
174
      return p[j];  /* that is it */
276
36.7k
  }
277
  /* normal route */
278
36.5k
  for (j = STRCACHE_M - 1; j > 0; j--)
279
18.2k
    p[j] = p[j - 1];  /* move out last element */
280
  /* new element is first in the list */
281
18.2k
  p[0] = luaS_newlstr(L, str, strlen(str));
282
18.2k
  return p[0];
283
18.4k
}
284
285
286
46
Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) {
287
46
  Udata *u;
288
46
  int i;
289
46
  GCObject *o;
290
46
  if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
291
0
    luaM_toobig(L);
292
46
  o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
293
46
  u = gco2u(o);
294
0
  u->len = s;
295
46
  u->nuvalue = nuvalue;
296
46
  u->metatable = NULL;
297
46
  for (i = 0; i < nuvalue; i++)
298
46
    setnilvalue(&u->uv[i].uv);
299
46
  return u;
300
46
}
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
46
static void f_newext (lua_State *L, void *ud) {
312
46
  struct NewExt *ne = cast(struct NewExt *, ud);
313
46
  size_t size = luaS_sizelngstr(0, ne->kind);
314
46
  ne->ts = createstrobj(L, size, LUA_VLNGSTR, G(L)->seed);
315
46
}
316
317
318
TString *luaS_newextlstr (lua_State *L,
319
46
            const char *s, size_t len, lua_Alloc falloc, void *ud) {
320
46
  struct NewExt ne;
321
46
  if (!falloc) {
322
0
    ne.kind = LSTRFIX;
323
0
    f_newext(L, &ne);  /* just create header */
324
0
  }
325
46
  else {
326
46
    ne.kind = LSTRMEM;
327
46
    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
46
    ne.ts->falloc = falloc;
332
46
    ne.ts->ud = ud;
333
46
  }
334
46
  ne.ts->shrlen = ne.kind;
335
46
  ne.ts->u.lnglen = len;
336
46
  ne.ts->contents = cast_charp(s);
337
46
  return ne.ts;
338
46
}
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