diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-01-31 12:27:11 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-01-31 12:27:11 -0200 |
commit | 1143bf92868a9ec47107a302db43a8e5275d8d80 (patch) | |
tree | 648f463b6ea6bbc8985fa7681d90630800cdb62c | |
parent | d9ecc135458a8538c00e12db654513662262d300 (diff) | |
download | lua-1143bf92868a9ec47107a302db43a8e5275d8d80.tar.gz lua-1143bf92868a9ec47107a302db43a8e5275d8d80.tar.bz2 lua-1143bf92868a9ec47107a302db43a8e5275d8d80.zip |
better check when converting from float to int, to avoid overflow
(on some machines it may result in run-time error)
-rw-r--r-- | lua.stx | 8 | ||||
-rw-r--r-- | opcode.c | 26 |
2 files changed, 19 insertions, 15 deletions
@@ -1,6 +1,6 @@ | |||
1 | %{ | 1 | %{ |
2 | 2 | ||
3 | char *rcs_luastx = "$Id: lua.stx,v 3.41 1996/11/08 12:49:35 roberto Exp roberto $"; | 3 | char *rcs_luastx = "$Id: lua.stx,v 3.42 1997/01/15 16:11:37 roberto Exp roberto $"; |
4 | 4 | ||
5 | #include <stdio.h> | 5 | #include <stdio.h> |
6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
@@ -163,9 +163,9 @@ static void add_varbuffer (Long var) | |||
163 | 163 | ||
164 | static void code_number (float f) | 164 | static void code_number (float f) |
165 | { | 165 | { |
166 | Word i = (Word)f; | 166 | Word i; |
167 | if (f == (float)i) /* f has an (short) integer value */ | 167 | if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) { |
168 | { | 168 | /* f has an (short) integer value */ |
169 | if (i <= 2) code_byte(PUSH0 + i); | 169 | if (i <= 2) code_byte(PUSH0 + i); |
170 | else if (i <= 255) | 170 | else if (i <= 255) |
171 | { | 171 | { |
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_opcode="$Id: opcode.c,v 3.77 1996/11/18 13:48:44 roberto Exp roberto $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 3.78 1996/11/22 13:08:28 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <setjmp.h> | 8 | #include <setjmp.h> |
9 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -157,16 +157,20 @@ static int lua_tonumber (Object *obj) | |||
157 | */ | 157 | */ |
158 | static int lua_tostring (Object *obj) | 158 | static int lua_tostring (Object *obj) |
159 | { | 159 | { |
160 | char s[256]; | 160 | if (tag(obj) != LUA_T_NUMBER) |
161 | if (tag(obj) != LUA_T_NUMBER) | 161 | return 1; |
162 | return 1; | 162 | else { |
163 | if ((int) nvalue(obj) == nvalue(obj)) | 163 | char s[60]; |
164 | sprintf (s, "%d", (int) nvalue(obj)); | 164 | real f = nvalue(obj); |
165 | else | 165 | int i; |
166 | sprintf (s, "%g", nvalue(obj)); | 166 | if ((real)(-MAX_INT) <= f && f <= (real)MAX_INT && (real)(i=(int)f) == f) |
167 | tsvalue(obj) = lua_createstring(s); | 167 | sprintf (s, "%d", i); |
168 | tag(obj) = LUA_T_STRING; | 168 | else |
169 | return 0; | 169 | sprintf (s, "%g", nvalue(obj)); |
170 | tsvalue(obj) = lua_createstring(s); | ||
171 | tag(obj) = LUA_T_STRING; | ||
172 | return 0; | ||
173 | } | ||
170 | } | 174 | } |
171 | 175 | ||
172 | 176 | ||