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

Commit 4ec2b88

Browse files
committed
[Bug 14679] com.livecode.math: Generate domain errors from pow().
1 parent 79c8b31 commit 4ec2b88

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

engine/src/modules.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,15 @@ extern "C"
134134
}
135135

136136
extern bool MCForeignModuleInitialize(void);
137+
extern bool MCMathModuleInitialize(void);
137138
extern bool MCCanvasModuleInitialize(void);
138139
extern bool MCEngineModuleInitialize(void);
139140
bool MCModulesInitialize(void)
140141
{
141142
if (!MCForeignModuleInitialize())
142143
return false;
144+
if (!MCMathModuleInitialize())
145+
return false;
143146
if (!MCCanvasModuleInitialize())
144147
return false;
145148
if (!MCEngineModuleInitialize())
@@ -148,11 +151,13 @@ bool MCModulesInitialize(void)
148151
}
149152

150153
extern void MCForeignModuleFinalize(void);
154+
extern void MCMathModuleFinalize(void);
151155
extern void MCCanvasModuleFinalize(void);
152156
extern void MCEngineModuleFinalize(void);
153157
void MCModulesFinalize(void)
154158
{
155159
MCEngineModuleFinalize();
156160
MCCanvasModuleFinalize();
161+
MCMathModuleFinalize();
157162
MCForeignModuleFinalize();
158163
}

libscript/src/module-math.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,28 @@
1919
#include <foundation-system.h>
2020

2121
#include <float.h>
22+
#include <errno.h>
2223

2324
// Older versions of MSVC don't supply "trunc"
2425
#ifdef _WIN32
2526
double trunc(double f) { return f < 0 ? ceil(f) : floor(f); }
2627
#endif
2728

29+
////////////////////////////////////////////////////////////////
30+
31+
MCTypeInfoRef kMCMathDomainErrorTypeInfo;
32+
33+
////////////////////////////////////////////////////////////////
34+
2835
extern "C" MC_DLLEXPORT void MCMathEvalRealToPowerOfReal(double p_left, double p_right, double& r_output)
2936
{
30-
if (p_right == 0)
31-
{
32-
r_output = 1.0;
33-
return;
34-
}
35-
37+
errno = 0;
3638
r_output = pow(p_left, p_right);
39+
40+
if (errno == EDOM)
41+
{
42+
MCErrorCreateAndThrow (kMCMathDomainErrorTypeInfo, nil);
43+
}
3744
}
3845

3946
extern "C" MC_DLLEXPORT void MCMathEvalNumberToPowerOfNumber(MCNumberRef p_left, MCNumberRef p_right, MCNumberRef& r_output)
@@ -413,4 +420,21 @@ extern "C" MC_DLLEXPORT void MCMathEvalConvertBase(MCStringRef p_operand, intege
413420
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("integer overflow, or invalid character in source"), nil);
414421
}
415422

423+
////////////////////////////////////////////////////////////////
424+
425+
bool
426+
MCMathModuleInitialize (void)
427+
{
428+
if (!MCNamedErrorTypeInfoCreate (MCNAME("com.livecode.math.DomainError"), MCNAME("math"), MCSTR("mathematical function domain error"), kMCMathDomainErrorTypeInfo))
429+
return false;
430+
431+
return true;
432+
}
433+
434+
void
435+
MCMathModuleFinalize (void)
436+
{
437+
MCValueRelease (kMCMathDomainErrorTypeInfo);
438+
}
439+
416440
////////////////////////////////////////////////////////////////////////////////////////////////////

tests/lcb/stdlib/math.lcb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public handler TestPow()
5656
test "pow (-ve, 0)" when (-2) ^ 0 is 1
5757
end handler
5858

59-
handler TestPowDomain_Imag() as undefined
60-
variable tVar
61-
put (-1) ^ 0.5 into tVar
59+
handler TestPowDomain_Imag()
60+
return (-1) ^ 0.5
6261
end handler
6362
public handler TestPowDomain()
64-
MCUnitTestHandlerThrowsBroken(TestPowDomain_Imag, "pow (imaginary)", "bug 14679")
63+
-- bug 14679
64+
MCUnitTestHandlerThrows(TestPowDomain_Imag, "pow (imaginary)")
6565
end handler
6666

6767
public handler TestPowPrecedence()

toolchain/lc-compile/src/module-helper.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,22 @@ void *g_builtin_ptrs[] =
130130
}
131131

132132
extern bool MCForeignModuleInitialize(void);
133+
extern bool MCMathModuleInitialize(void);
134+
133135
bool MCModulesInitialize(void)
134136
{
135137
if (!MCForeignModuleInitialize())
136138
return false;
139+
if (!MCMathModuleInitialize())
140+
return false;
137141
return true;
138142
}
139143

140144
extern void MCForeignModuleFinalize(void);
145+
extern void MCMathModuleFinalize(void);
146+
141147
void MCModulesFinalize(void)
142148
{
149+
MCMathModuleFinalize();
143150
MCForeignModuleFinalize();
144151
}

0 commit comments

Comments
 (0)