aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lmem.h3
-rw-r--r--lundump.c57
2 files changed, 27 insertions, 33 deletions
diff --git a/lmem.h b/lmem.h
index 08358592..dc714fb2 100644
--- a/lmem.h
+++ b/lmem.h
@@ -57,7 +57,8 @@
57#define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) 57#define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b)))
58 58
59#define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) 59#define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0))
60#define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) 60#define luaM_newvector(L,n,t) \
61 cast(t*, luaM_malloc_(L, cast_sizet(n)*sizeof(t), 0))
61#define luaM_newvectorchecked(L,n,t) \ 62#define luaM_newvectorchecked(L,n,t) \
62 (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) 63 (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t))
63 64
diff --git a/lundump.c b/lundump.c
index fd5a2ca6..d074a073 100644
--- a/lundump.c
+++ b/lundump.c
@@ -52,7 +52,7 @@ static l_noret error (LoadState *S, const char *why) {
52** All high-level loads go through loadVector; you can change it to 52** All high-level loads go through loadVector; you can change it to
53** adapt to the endianness of the input 53** adapt to the endianness of the input
54*/ 54*/
55#define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0])) 55#define loadVector(S,b,n) loadBlock(S,b,cast_sizet(n)*sizeof((b)[0]))
56 56
57static void loadBlock (LoadState *S, void *b, size_t size) { 57static void loadBlock (LoadState *S, void *b, size_t size) {
58 if (luaZ_read(S->Z, b, size) != 0) 58 if (luaZ_read(S->Z, b, size) != 0)
@@ -71,7 +71,7 @@ static void loadAlign (LoadState *S, unsigned align) {
71} 71}
72 72
73 73
74#define getaddr(S,n,t) cast(t *, getaddr_(S,(n) * sizeof(t))) 74#define getaddr(S,n,t) cast(t *, getaddr_(S,cast_sizet(n) * sizeof(t)))
75 75
76static const void *getaddr_ (LoadState *S, size_t size) { 76static const void *getaddr_ (LoadState *S, size_t size) {
77 const void *block = luaZ_getaddr(S->Z, size); 77 const void *block = luaZ_getaddr(S->Z, size);
@@ -113,13 +113,6 @@ static size_t loadSize (LoadState *S) {
113} 113}
114 114
115 115
116/*
117** Read an non-negative int */
118static unsigned loadUint (LoadState *S) {
119 return cast_uint(loadVarint(S, cast_sizet(INT_MAX)));
120}
121
122
123static int loadInt (LoadState *S) { 116static int loadInt (LoadState *S) {
124 return cast_int(loadVarint(S, cast_sizet(INT_MAX))); 117 return cast_int(loadVarint(S, cast_sizet(INT_MAX)));
125} 118}
@@ -188,15 +181,15 @@ static void loadString (LoadState *S, Proto *p, TString **sl) {
188 181
189 182
190static void loadCode (LoadState *S, Proto *f) { 183static void loadCode (LoadState *S, Proto *f) {
191 unsigned n = loadUint(S); 184 int n = loadInt(S);
192 loadAlign(S, sizeof(f->code[0])); 185 loadAlign(S, sizeof(f->code[0]));
193 if (S->fixed) { 186 if (S->fixed) {
194 f->code = getaddr(S, n, Instruction); 187 f->code = getaddr(S, n, Instruction);
195 f->sizecode = cast_int(n); 188 f->sizecode = n;
196 } 189 }
197 else { 190 else {
198 f->code = luaM_newvectorchecked(S->L, n, Instruction); 191 f->code = luaM_newvectorchecked(S->L, n, Instruction);
199 f->sizecode = cast_int(n); 192 f->sizecode = n;
200 loadVector(S, f->code, n); 193 loadVector(S, f->code, n);
201 } 194 }
202} 195}
@@ -206,10 +199,10 @@ static void loadFunction(LoadState *S, Proto *f);
206 199
207 200
208static void loadConstants (LoadState *S, Proto *f) { 201static void loadConstants (LoadState *S, Proto *f) {
209 unsigned i; 202 int i;
210 unsigned n = loadUint(S); 203 int n = loadInt(S);
211 f->k = luaM_newvectorchecked(S->L, n, TValue); 204 f->k = luaM_newvectorchecked(S->L, n, TValue);
212 f->sizek = cast_int(n); 205 f->sizek = n;
213 for (i = 0; i < n; i++) 206 for (i = 0; i < n; i++)
214 setnilvalue(&f->k[i]); 207 setnilvalue(&f->k[i]);
215 for (i = 0; i < n; i++) { 208 for (i = 0; i < n; i++) {
@@ -248,10 +241,10 @@ static void loadConstants (LoadState *S, Proto *f) {
248 241
249 242
250static void loadProtos (LoadState *S, Proto *f) { 243static void loadProtos (LoadState *S, Proto *f) {
251 unsigned i; 244 int i;
252 unsigned n = loadUint(S); 245 int n = loadInt(S);
253 f->p = luaM_newvectorchecked(S->L, n, Proto *); 246 f->p = luaM_newvectorchecked(S->L, n, Proto *);
254 f->sizep = cast_int(n); 247 f->sizep = n;
255 for (i = 0; i < n; i++) 248 for (i = 0; i < n; i++)
256 f->p[i] = NULL; 249 f->p[i] = NULL;
257 for (i = 0; i < n; i++) { 250 for (i = 0; i < n; i++) {
@@ -269,10 +262,10 @@ static void loadProtos (LoadState *S, Proto *f) {
269** in that case all prototypes must be consistent for the GC. 262** in that case all prototypes must be consistent for the GC.
270*/ 263*/
271static void loadUpvalues (LoadState *S, Proto *f) { 264static void loadUpvalues (LoadState *S, Proto *f) {
272 unsigned i; 265 int i;
273 unsigned n = loadUint(S); 266 int n = loadInt(S);
274 f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); 267 f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
275 f->sizeupvalues = cast_int(n); 268 f->sizeupvalues = n;
276 for (i = 0; i < n; i++) /* make array valid for GC */ 269 for (i = 0; i < n; i++) /* make array valid for GC */
277 f->upvalues[i].name = NULL; 270 f->upvalues[i].name = NULL;
278 for (i = 0; i < n; i++) { /* following calls can raise errors */ 271 for (i = 0; i < n; i++) { /* following calls can raise errors */
@@ -284,33 +277,33 @@ static void loadUpvalues (LoadState *S, Proto *f) {
284 277
285 278
286static void loadDebug (LoadState *S, Proto *f) { 279static void loadDebug (LoadState *S, Proto *f) {
287 unsigned i; 280 int i;
288 unsigned n = loadUint(S); 281 int n = loadInt(S);
289 if (S->fixed) { 282 if (S->fixed) {
290 f->lineinfo = getaddr(S, n, ls_byte); 283 f->lineinfo = getaddr(S, n, ls_byte);
291 f->sizelineinfo = cast_int(n); 284 f->sizelineinfo = n;
292 } 285 }
293 else { 286 else {
294 f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); 287 f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
295 f->sizelineinfo = cast_int(n); 288 f->sizelineinfo = n;
296 loadVector(S, f->lineinfo, n); 289 loadVector(S, f->lineinfo, n);
297 } 290 }
298 n = loadUint(S); 291 n = loadInt(S);
299 if (n > 0) { 292 if (n > 0) {
300 loadAlign(S, sizeof(int)); 293 loadAlign(S, sizeof(int));
301 if (S->fixed) { 294 if (S->fixed) {
302 f->abslineinfo = getaddr(S, n, AbsLineInfo); 295 f->abslineinfo = getaddr(S, n, AbsLineInfo);
303 f->sizeabslineinfo = cast_int(n); 296 f->sizeabslineinfo = n;
304 } 297 }
305 else { 298 else {
306 f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); 299 f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
307 f->sizeabslineinfo = cast_int(n); 300 f->sizeabslineinfo = n;
308 loadVector(S, f->abslineinfo, n); 301 loadVector(S, f->abslineinfo, n);
309 } 302 }
310 } 303 }
311 n = loadUint(S); 304 n = loadInt(S);
312 f->locvars = luaM_newvectorchecked(S->L, n, LocVar); 305 f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
313 f->sizelocvars = cast_int(n); 306 f->sizelocvars = n;
314 for (i = 0; i < n; i++) 307 for (i = 0; i < n; i++)
315 f->locvars[i].varname = NULL; 308 f->locvars[i].varname = NULL;
316 for (i = 0; i < n; i++) { 309 for (i = 0; i < n; i++) {
@@ -318,9 +311,9 @@ static void loadDebug (LoadState *S, Proto *f) {
318 f->locvars[i].startpc = loadInt(S); 311 f->locvars[i].startpc = loadInt(S);
319 f->locvars[i].endpc = loadInt(S); 312 f->locvars[i].endpc = loadInt(S);
320 } 313 }
321 n = loadUint(S); 314 n = loadInt(S);
322 if (n != 0) /* does it have debug information? */ 315 if (n != 0) /* does it have debug information? */
323 n = cast_uint(f->sizeupvalues); /* must be this many */ 316 n = f->sizeupvalues; /* must be this many */
324 for (i = 0; i < n; i++) 317 for (i = 0; i < n; i++)
325 loadString(S, f, &f->upvalues[i].name); 318 loadString(S, f, &f->upvalues[i].name);
326} 319}