aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-01-31 12:27:11 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-01-31 12:27:11 -0200
commit1143bf92868a9ec47107a302db43a8e5275d8d80 (patch)
tree648f463b6ea6bbc8985fa7681d90630800cdb62c
parentd9ecc135458a8538c00e12db654513662262d300 (diff)
downloadlua-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.stx8
-rw-r--r--opcode.c26
2 files changed, 19 insertions, 15 deletions
diff --git a/lua.stx b/lua.stx
index 79b94e7f..61939628 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2 2
3char *rcs_luastx = "$Id: lua.stx,v 3.41 1996/11/08 12:49:35 roberto Exp roberto $"; 3char *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
164static void code_number (float f) 164static 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 {
diff --git a/opcode.c b/opcode.c
index 0df045a0..5c03d7b5 100644
--- a/opcode.c
+++ b/opcode.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 3.77 1996/11/18 13:48:44 roberto Exp roberto $"; 6char *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*/
158static int lua_tostring (Object *obj) 158static 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