From cc2b66c85687b095e68304c010b59851ca4093e1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Mar 2024 09:16:51 -0300 Subject: [PATCH] Removed type 'varint_t' size_t should be big enough to count the number of strings in a dump. (And, by definition, it is big enough to count the length of each string.) --- ldump.c | 22 +++++++++++++--------- lundump.c | 21 ++++++++++----------- lundump.h | 12 ------------ 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/ldump.c b/ldump.c index 34cfb576..0d20fb0a 100644 --- a/ldump.c +++ b/ldump.c @@ -30,7 +30,7 @@ typedef struct { int strip; int status; Table *h; /* table to track saved strings */ - lua_Unsigned nstr; /* counter to number saved strings */ + lua_Integer nstr; /* counter for counting saved strings */ } DumpState; @@ -86,12 +86,12 @@ static void dumpByte (DumpState *D, int y) { ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. ** (The "+6" rounds up the division.) */ -#define DIBS ((sizeof(varint_t) * CHAR_BIT + 6) / 7) +#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7) /* ** Dumps an unsigned integer using the MSB Varint encoding */ -static void dumpVarint (DumpState *D, varint_t x) { +static void dumpVarint (DumpState *D, size_t x) { lu_byte buff[DIBS]; int n = 1; buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ @@ -101,9 +101,13 @@ static void dumpVarint (DumpState *D, varint_t x) { } +static void dumpSize (DumpState *D, size_t sz) { + dumpVarint(D, sz); +} + static void dumpInt (DumpState *D, int x) { lua_assert(x >= 0); - dumpVarint(D, x); + dumpVarint(D, cast(size_t, x)); } @@ -126,22 +130,22 @@ static void dumpInteger (DumpState *D, lua_Integer x) { */ static void dumpString (DumpState *D, TString *ts) { if (ts == NULL) - dumpVarint(D, 0); + dumpSize(D, 0); else { TValue idx; if (luaH_getstr(D->h, ts, &idx) == HOK) { /* string already saved? */ - dumpVarint(D, 1); /* reuse a saved string */ - dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ + dumpSize(D, 1); /* reuse a saved string */ + dumpSize(D, cast_sizet(ivalue(&idx))); /* index of saved string */ } else { /* must write and save the string */ TValue key, value; /* to save the string in the hash */ size_t size; const char *s = getlstr(ts, size); - dumpVarint(D, size + 2); + dumpSize(D, size + 2); dumpVector(D, s, size + 1); /* include ending '\0' */ D->nstr++; /* one more saved string */ setsvalue(D->L, &key, ts); /* the string is the key */ - setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ + setivalue(&value, D->nstr); /* its index is the value */ luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ /* integer value does not need barrier */ } diff --git a/lundump.c b/lundump.c index d485f266..51d5dc66 100644 --- a/lundump.c +++ b/lundump.c @@ -36,8 +36,8 @@ typedef struct { ZIO *Z; const char *name; Table *h; /* list for string reuse */ - size_t offset; /* current position relative to beginning of dump */ - lua_Unsigned nstr; /* number of strings in the list */ + lu_mem offset; /* current position relative to beginning of dump */ + lua_Integer nstr; /* number of strings in the list */ lu_byte fixed; /* dump is fixed in memory */ } LoadState; @@ -94,13 +94,13 @@ static lu_byte loadByte (LoadState *S) { } -static varint_t loadVarint (LoadState *S, varint_t limit) { - varint_t x = 0; +static size_t loadVarint (LoadState *S, size_t limit) { + size_t x = 0; int b; limit >>= 7; do { b = loadByte(S); - if (x >= limit) + if (x > limit) error(S, "integer overflow"); x = (x << 7) | (b & 0x7f); } while ((b & 0x80) != 0); @@ -109,12 +109,12 @@ static varint_t loadVarint (LoadState *S, varint_t limit) { static size_t loadSize (LoadState *S) { - return cast_sizet(loadVarint(S, MAX_SIZET)); + return loadVarint(S, MAX_SIZET); } static int loadInt (LoadState *S) { - return cast_int(loadVarint(S, INT_MAX)); + return cast_int(loadVarint(S, cast_sizet(INT_MAX))); } @@ -148,10 +148,9 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { return; } else if (size == 1) { /* previously saved string? */ - /* get its index */ - lua_Unsigned idx = cast(lua_Unsigned, loadVarint(S, LUA_MAXUNSIGNED)); + lua_Integer idx = cast(lua_Integer, loadSize(S)); /* get its index */ TValue stv; - luaH_getint(S->h, l_castU2S(idx), &stv); /* get its value */ + luaH_getint(S->h, idx, &stv); /* get its value */ *sl = ts = tsvalue(&stv); luaC_objbarrier(L, p, ts); return; /* do not save it again */ @@ -175,7 +174,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { /* add string to list of saved strings */ S->nstr++; setsvalue(L, &sv, ts); - luaH_setint(L, S->h, l_castU2S(S->nstr), &sv); + luaH_setint(L, S->h, S->nstr, &sv); luaC_objbarrierback(L, obj2gco(S->h), ts); } diff --git a/lundump.h b/lundump.h index ff66d2e7..1d6e50ea 100644 --- a/lundump.h +++ b/lundump.h @@ -28,18 +28,6 @@ #define LUAC_FORMAT 0 /* this is the official format */ -/* -** Type to handle MSB Varint encoding: Try to get the largest unsigned -** integer available. (It was enough to be the largest between size_t and -** lua_Integer, but the C89 preprocessor knows nothing about size_t.) -*/ -#if !defined(LUA_USE_C89) && defined(LLONG_MAX) -typedef unsigned long long varint_t; -#else -typedef unsigned long varint_t; -#endif - - /* load one chunk; from lundump.c */ LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name, int fixed);