Coverage Report

Created: 2025-07-18 07:03

/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
51.2k
#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
126k
#define MINSTRTABSIZE   128
38
#endif
39
40
41
/*
42
** equality for long strings
43
*/
44
9.87M
int luaS_eqlngstr (TString *a, TString *b) {
45
9.87M
  size_t len = a->u.lnglen;
46
9.87M
  lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR);
47
9.87M
  return (a == b) ||  /* same instance or... */
48
9.87M
    ((len == b->u.lnglen) &&  /* equal length and ... */
49
4.16M
     (memcmp(getlngstr(a), getlngstr(b), len) == 0));  /* equal contents */
50
9.87M
}
51
52
53
307M
unsigned luaS_hash (const char *str, size_t l, unsigned seed) {
54
307M
  unsigned int h = seed ^ cast_uint(l);
55
5.75G
  for (; l > 0; l--)
56
5.44G
    h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
57
307M
  return h;
58
307M
}
59
60
61
18.7M
unsigned luaS_hashlongstr (TString *ts) {
62
18.7M
  lua_assert(ts->tt == LUA_VLNGSTR);
63
18.7M
  if (ts->extra == 0) {  /* no hash? */
64
3.05M
    size_t len = ts->u.lnglen;
65
3.05M
    ts->hash = luaS_hash(getlngstr(ts), len, ts->hash);
66
0
    ts->extra = 1;  /* now it has its hash */
67
3.05M
  }
68
18.7M
  return ts->hash;
69
18.7M
}
70
71
72
127k
static void tablerehash (TString **vect, int osize, int nsize) {
73
127k
  int i;
74
34.0M
  for (i = osize; i < nsize; i++)  /* clear new elements */
75
33.9M
    vect[i] = NULL;
76
44.4M
  for (i = 0; i < osize; i++) {  /* rehash old part of the array */
77
44.2M
    TString *p = vect[i];
78
44.2M
    vect[i] = NULL;
79
73.3M
    while (p) {  /* for each string in the list */
80
29.0M
      TString *hnext = p->u.hnext;  /* save next */
81
29.0M
      unsigned int h = lmod(p->hash, nsize);  /* new position */
82
0
      p->u.hnext = vect[h];  /* chain it into array */
83
29.0M
      vect[h] = p;
84
29.0M
      p = hnext;
85
29.0M
    }
86
44.2M
  }
87
127k
}
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
63.9k
void luaS_resize (lua_State *L, int nsize) {
96
63.9k
  stringtable *tb = &G(L)->strt;
97
63.9k
  int osize = tb->size;
98
63.9k
  TString **newvect;
99
63.9k
  if (nsize < osize)  /* shrinking table? */
100
12.6k
    tablerehash(tb->hash, osize, nsize);  /* depopulate shrinking part */
101
63.9k
  newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
102
63.9k
  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
63.9k
  else {  /* allocation succeeded */
108
63.9k
    tb->hash = newvect;
109
63.9k
    tb->size = nsize;
110
63.9k
    if (nsize > osize)
111
51.2k
      tablerehash(newvect, osize, nsize);  /* rehash for new size */
112
63.9k
  }
113
63.9k
}
114
115
116
/*
117
** Clear API string cache. (Entries cannot be empty, so fill them with
118
** a non-collectable string.)
119
*/
120
1.42M
void luaS_clearcache (global_State *g) {
121
1.42M
  int i, j;
122
77.1M
  for (i = 0; i < STRCACHE_N; i++)
123
227M
    for (j = 0; j < STRCACHE_M; j++) {
124
151M
      if (iswhite(g->strcache[i][j]))  /* will entry be collected? */
125
5.40M
        g->strcache[i][j] = g->memerrmsg;  /* replace it with something fixed */
126
151M
    }
127
1.42M
}
128
129
130
/*
131
** Initialize the string table and the string cache
132
*/
133
63.4k
void luaS_init (lua_State *L) {
134
63.4k
  global_State *g = G(L);
135
63.4k
  int i, j;
136
63.4k
  stringtable *tb = &G(L)->strt;
137
63.4k
  tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*);
138
63.4k
  tablerehash(tb->hash, 0, MINSTRTABSIZE);  /* clear array */
139
63.4k
  tb->size = MINSTRTABSIZE;
140
  /* pre-create memory-error message */
141
63.4k
  g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
142
63.4k
  luaC_fix(L, obj2gco(g->memerrmsg));  /* it should never be collected */
143
3.42M
  for (i = 0; i < STRCACHE_N; i++)  /* fill cache with valid strings */
144
10.0M
    for (j = 0; j < STRCACHE_M; j++)
145
6.72M
      g->strcache[i][j] = g->memerrmsg;
146
63.4k
}
147
148
149
114M
size_t luaS_sizelngstr (size_t len, int kind) {
150
114M
  switch (kind) {
151
105M
    case LSTRREG:  /* regular long string */
152
      /* don't need 'falloc'/'ud', but need space for content */
153
105M
      return offsetof(TString, falloc) + (len + 1) * sizeof(char);
154
1.46M
    case LSTRFIX:  /* fixed external long string */
155
      /* don't need 'falloc'/'ud' */
156
1.46M
      return offsetof(TString, falloc);
157
6.69M
    default:  /* external long string with deallocation */
158
6.69M
      lua_assert(kind == LSTRMEM);
159
6.69M
      return sizeof(TString);
160
114M
  }
161
114M
}
162
163
164
/*
165
** creates a new string object
166
*/
167
static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag,
168
67.4M
                              unsigned h) {
169
67.4M
  TString *ts;
170
67.4M
  GCObject *o;
171
67.4M
  o = luaC_newobj(L, tag, totalsize);
172
67.4M
  ts = gco2ts(o);
173
0
  ts->hash = h;
174
67.4M
  ts->extra = 0;
175
67.4M
  return ts;
176
67.4M
}
177
178
179
28.3M
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
180
28.3M
  size_t totalsize = luaS_sizelngstr(l, LSTRREG);
181
28.3M
  TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed);
182
28.3M
  ts->u.lnglen = l;
183
28.3M
  ts->shrlen = LSTRREG;  /* signals that it is a regular long string */
184
28.3M
  ts->contents = cast_charp(ts) + offsetof(TString, falloc);
185
28.3M
  ts->contents[l] = '\0';  /* ending 0 */
186
28.3M
  return ts;
187
28.3M
}
188
189
190
37.7M
void luaS_remove (lua_State *L, TString *ts) {
191
37.7M
  stringtable *tb = &G(L)->strt;
192
37.7M
  TString **p = &tb->hash[lmod(ts->hash, tb->size)];
193
42.4M
  while (*p != ts)  /* find previous element */
194
4.69M
    p = &(*p)->u.hnext;
195
37.7M
  *p = (*p)->u.hnext;  /* remove element from its list */
196
37.7M
  tb->nuse--;
197
37.7M
}
198
199
200
51.2k
static void growstrtab (lua_State *L, stringtable *tb) {
201
51.2k
  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
51.2k
  if (tb->size <= MAXSTRTB / 2)  /* can grow string table? */
207
51.2k
    luaS_resize(L, tb->size * 2);
208
51.2k
}
209
210
211
/*
212
** Checks whether short string exists and reuses it or creates a new one.
213
*/
214
304M
static TString *internshrstr (lua_State *L, const char *str, size_t l) {
215
304M
  TString *ts;
216
304M
  global_State *g = G(L);
217
304M
  stringtable *tb = &g->strt;
218
304M
  unsigned int h = luaS_hash(str, l, g->seed);
219
304M
  TString **list = &tb->hash[lmod(h, tb->size)];
220
304M
  lua_assert(str != NULL);  /* otherwise 'memcmp'/'memcpy' are undefined */
221
381M
  for (ts = *list; ts != NULL; ts = ts->u.hnext) {
222
343M
    if (l == cast_uint(ts->shrlen) &&
223
343M
        (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) {
224
      /* found! */
225
266M
      if (isdead(g, ts))  /* dead (but not collected yet)? */
226
2.88M
        changewhite(ts);  /* resurrect it */
227
266M
      return ts;
228
266M
    }
229
343M
  }
230
  /* else must create a new string */
231
37.7M
  if (tb->nuse >= tb->size) {  /* need to grow string table? */
232
51.2k
    growstrtab(L, tb);
233
51.2k
    list = &tb->hash[lmod(h, tb->size)];  /* rehash with new size */
234
51.2k
  }
235
37.7M
  ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h);
236
37.7M
  ts->shrlen = cast(ls_byte, l);
237
37.7M
  getshrstr(ts)[l] = '\0';  /* ending 0 */
238
37.7M
  memcpy(getshrstr(ts), str, l * sizeof(char));
239
0
  ts->u.hnext = *list;
240
37.7M
  *list = ts;
241
37.7M
  tb->nuse++;
242
37.7M
  return ts;
243
37.7M
}
244
245
246
/*
247
** new string (with explicit length)
248
*/
249
330M
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
250
330M
  if (l <= LUAI_MAXSHORTLEN)  /* short string? */
251
304M
    return internshrstr(L, str, l);
252
25.9M
  else {
253
25.9M
    TString *ts;
254
25.9M
    if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString))))
255
0
      luaM_toobig(L);
256
25.9M
    ts = luaS_createlngstrobj(L, l);
257
25.9M
    memcpy(getlngstr(ts), str, l * sizeof(char));
258
0
    return ts;
259
25.9M
  }
260
330M
}
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
253M
TString *luaS_new (lua_State *L, const char *str) {
270
253M
  unsigned int i = point2uint(str) % STRCACHE_N;  /* hash */
271
253M
  int j;
272
253M
  TString **p = G(L)->strcache[i];
273
374M
  for (j = 0; j < STRCACHE_M; j++) {
274
353M
    if (strcmp(str, getstr(p[j])) == 0)  /* hit? */
275
232M
      return p[j];  /* that is it */
276
353M
  }
277
  /* normal route */
278
42.4M
  for (j = STRCACHE_M - 1; j > 0; j--)
279
21.2M
    p[j] = p[j - 1];  /* move out last element */
280
  /* new element is first in the list */
281
21.2M
  p[0] = luaS_newlstr(L, str, strlen(str));
282
21.2M
  return p[0];
283
253M
}
284
285
286
4.12M
Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) {
287
4.12M
  Udata *u;
288
4.12M
  int i;
289
4.12M
  GCObject *o;
290
4.12M
  if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
291
0
    luaM_toobig(L);
292
4.12M
  o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
293
4.12M
  u = gco2u(o);
294
0
  u->len = s;
295
4.12M
  u->nuvalue = nuvalue;
296
4.12M
  u->metatable = NULL;
297
4.12M
  for (i = 0; i < nuvalue; i++)
298
4.12M
    setnilvalue(&u->uv[i].uv);
299
4.12M
  return u;
300
4.12M
}
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
1.29M
static void f_newext (lua_State *L, void *ud) {
312
1.29M
  struct NewExt *ne = cast(struct NewExt *, ud);
313
1.29M
  size_t size = luaS_sizelngstr(0, ne->kind);
314
1.29M
  ne->ts = createstrobj(L, size, LUA_VLNGSTR, G(L)->seed);
315
1.29M
}
316
317
318
15
static void f_pintern (lua_State *L, void *ud) {
319
15
  struct NewExt *ne = cast(struct NewExt *, ud);
320
15
  ne->ts = internshrstr(L, ne->s, ne->len);
321
15
}
322
323
324
TString *luaS_newextlstr (lua_State *L,
325
1.29M
            const char *s, size_t len, lua_Alloc falloc, void *ud) {
326
1.29M
  struct NewExt ne;
327
1.29M
  if (len <= LUAI_MAXSHORTLEN) {  /* short string? */
328
15
    ne.s = s; ne.len = len;
329
15
    if (!falloc)
330
0
      f_pintern(L, &ne);  /* just internalize string */
331
15
    else {
332
15
      TStatus status = luaD_rawrunprotected(L, f_pintern, &ne);
333
15
      (*falloc)(ud, cast_voidp(s), len + 1, 0);  /* free external string */
334
15
      if (status != LUA_OK)  /* memory error? */
335
0
        luaM_error(L);  /* re-raise memory error */
336
15
    }
337
15
    return ne.ts;
338
15
  }
339
  /* "normal" case: long strings */
340
1.29M
  if (!falloc) {
341
63.9k
    ne.kind = LSTRFIX;
342
63.9k
    f_newext(L, &ne);  /* just create header */
343
63.9k
  }
344
1.23M
  else {
345
1.23M
    ne.kind = LSTRMEM;
346
1.23M
    if (luaD_rawrunprotected(L, f_newext, &ne) != LUA_OK) {  /* mem. error? */
347
0
      (*falloc)(ud, cast_voidp(s), len + 1, 0);  /* free external string */
348
0
      luaM_error(L);  /* re-raise memory error */
349
0
    }
350
1.23M
    ne.ts->falloc = falloc;
351
1.23M
    ne.ts->ud = ud;
352
1.23M
  }
353
1.29M
  ne.ts->shrlen = ne.kind;
354
1.29M
  ne.ts->u.lnglen = len;
355
1.29M
  ne.ts->contents = cast_charp(s);
356
1.29M
  return ne.ts;
357
1.29M
}
358
359