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 /opcode.c | |
| 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)
Diffstat (limited to 'opcode.c')
| -rw-r--r-- | opcode.c | 26 |
1 files changed, 15 insertions, 11 deletions
| @@ -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 | ||
