Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 90dd992

Browse files
committed
[Bug 14889] com.livecode.math: Emit errors when converting to base 10.
1 parent 79c8b31 commit 90dd992

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

libscript/src/module-math.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,18 @@ extern "C" MC_DLLEXPORT void MCMathEvalConvertToBase10(MCStringRef p_operand, in
361361

362362
bool t_negative;
363363
uinteger_t t_result;
364-
bool t_error;
364+
bool t_error = false;
365365
if (MCMathConvertToBase10(p_operand, p_source_base, t_negative, t_result, t_error))
366366
{
367367
if ((t_negative && t_result > INTEGER_MAX) || (!t_negative && t_result > abs(INTEGER_MIN)))
368368
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("integer overflow"), nil);
369369
else
370370
r_output = t_negative ? -t_result : t_result;
371371
}
372+
else if (t_error)
373+
{
374+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("integer overflow, or invalid character in source"), nil);
375+
}
372376
}
373377

374378
extern "C" MC_DLLEXPORT void MCMathEvalConvertFromBase10(integer_t p_operand, integer_t p_dest_base, MCStringRef& r_output)

tests/lcb/stdlib/math.lcb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1818
module com.livecode.math.tests
1919

2020
use com.livecode.math
21+
use com.livecode.foreign
2122
use com.livecode.__INTERNAL._testlib
2223

2324
-- Empirically-chosen epsilon for floating-point comparisons
@@ -198,9 +199,14 @@ public handler TestMaxList()
198199
skip test "max (empty list)" because "Can't test for errors"
199200
end handler
200201

202+
handler TestBaseConvert_InvalidFormat() as LCUInt
203+
return "0xf004" converted from base 16 -- bug 14889
204+
end handler
201205
public handler TestBaseConvertFrom()
202206
test "convert from hex" when "6A" converted from base 16 is 106
203207
test "convert from binary" when "1001010" converted from base 2 is 74
208+
209+
MCUnitTestHandlerThrows(TestBaseConvert_InvalidFormat, "convert from hex (0x)")
204210
end handler
205211

206212
public handler TestBaseConvertTo()

0 commit comments

Comments
 (0)