Skip to content

Commit eb24708

Browse files
committed
fix: protect error messages in float.c with locks and added/expanded tests for the error messages.
1 parent fe34bd1 commit eb24708

File tree

2 files changed

+132
-27
lines changed

2 files changed

+132
-27
lines changed

check/features.frm

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,20 @@ assert result("F3") =~ expr("1/1000000*x^5")
15541554
assert result("F4") =~ expr("4.7e+00*x - 5.0e-05*x^4 + 1/1000000*x^5")
15551555
assert result("F5") =~ expr("1/1000000*x^5")
15561556
*--#] chop :
1557+
*--#[ chop_error :
1558+
Chop 10;
1559+
#StartFloat 10d
1560+
Chop;
1561+
Chop 1/0;
1562+
Chop 1e-10a;
1563+
.end
1564+
#pend_if wordsize == 2
1565+
assert compile_error?("Illegal attempt to chop a float_ without activating floating point numbers.")
1566+
assert compile_error?("Forgotten #startfloat instruction?")
1567+
assert compile_error?("Chop needs a number (float, rational or power) as an argument.")
1568+
assert compile_error?("Division by zero in chop statement.")
1569+
assert compile_error?("Illegal argument(s) in Chop statement: 'a'.")
1570+
*--#] chop_error :
15571571
*--#[ pattern_float :
15581572
#-
15591573
Off Statistics;
@@ -1875,17 +1889,33 @@ assert result("FloatZero") =~ expr("f(0.0e+00, - 1.23e+02,0.0e+00,0.0e+00,0.0e+0
18751889
0.0e+00)")
18761890
*--#] float_zero :
18771891
*--#[ float_error :
1892+
Local F = 1.0;
18781893
Evaluate;
18791894
ToFloat;
18801895
ToRat;
1896+
.sort
1897+
#StartFloat 10d
1898+
Evaluate x;
1899+
.sort
1900+
CFunction x;
1901+
Evaluate x;
1902+
ToFloat y;
1903+
ToRat z;
18811904
.end
18821905
#pend_if wordsize == 2
1906+
assert compile_error?("The floating point system has not been started: .0")
1907+
assert compile_error?("Illegal character at this position: .0")
1908+
assert compile_error?("Illegal position for 0")
18831909
assert compile_error?("Illegal attempt to evaluate a function without activating floating point numbers.")
18841910
assert compile_error?("Forgotten #startfloat instruction?")
18851911
assert compile_error?("Illegal attempt to convert to float_ without activating floating point numbers.")
18861912
assert compile_error?("Forgotten #startfloat instruction?")
18871913
assert compile_error?("Illegal attempt to convert from float_ without activating floating point numbers.")
18881914
assert compile_error?("Forgotten #startfloat instruction?")
1915+
assert compile_error?("should be a built in function that can be evaluated numerically.")
1916+
assert compile_error?("should be a built in function that can be evaluated numerically.")
1917+
assert compile_error?("Illegal argument(s) in ToFloat statement: 'y'")
1918+
assert compile_error?("Illegal argument(s) in ToRat statement: 'z'")
18891919
*--#] float_error :
18901920
*--#[ format_and_floats :
18911921
#-
@@ -2247,6 +2277,52 @@ EOF
22472277
#pend_if wordsize == 2
22482278
runtime_error?("Illegal parameter in #StartFloat: 100bd,MZV = 10")
22492279
*--#] startfloat_error :
2280+
*--#[ mzv_error_1:
2281+
#StartFloat 10d, MZV=2
2282+
Local F = mzv_(2,1);
2283+
Evaluate mzv_;
2284+
.end
2285+
#pend_if wordsize == 2
2286+
#pend_if mpi?
2287+
runtime_error?("Error: Weight of Euler/MZV sum greater than 2.")
2288+
runtime_error?("Please increase the maximum weight in #startfloat.")
2289+
*--#] mzv_error_1 :
2290+
*--#[ mzv_error_2:
2291+
#StartFloat 10d, MZV=3
2292+
Local F = mzv_(1,2);
2293+
Evaluate mzv_;
2294+
.end
2295+
#pend_if wordsize == 2
2296+
#pend_if mpi?
2297+
runtime_error?("Divergent MZV in CalculateMZV")
2298+
*--#] mzv_error_2 :
2299+
*--#[ mzv_error_3:
2300+
#StartFloat 10d, MZV=3
2301+
Local F = mzv_(-2,1);
2302+
Evaluate mzv_;
2303+
.end
2304+
#pend_if wordsize == 2
2305+
#pend_if mpi?
2306+
runtime_error?("Illegal index[0] in CalculateMZV: -2")
2307+
*--#] mzv_error_3 :
2308+
*--#[ mzv_error_4:
2309+
#StartFloat 10d, MZV=3
2310+
Local F = mzvhalf_(2,-1);
2311+
Evaluate mzvhalf_;
2312+
.end
2313+
#pend_if wordsize == 2
2314+
#pend_if mpi?
2315+
runtime_error?("Illegal index[1] in CalculateMZVhalf: -1")
2316+
*--#] mzv_error_4 :
2317+
*--#[ mzv_error_5:
2318+
#StartFloat 10d, MZV=3
2319+
Local F = euler_(1,-2);
2320+
Evaluate euler_;
2321+
.end
2322+
#pend_if wordsize == 2
2323+
#pend_if mpi?
2324+
runtime_error?("Divergent Euler sum in CalculateEuler")
2325+
*--#] mzv_error_5 :
22502326
*--#[ humanstats :
22512327
#-
22522328
On humanstats;

sources/float.c

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,10 @@ int UnpackFloat(mpf_t outfloat,WORD *fun)
374374
*/
375375
GETIDENTITY
376376
if ( AT.aux_ == 0 ) {
377+
MLOCK(ErrorMessageLock);
377378
MesPrint("Illegal attempt at using a float_ function without proper startup.");
378379
MesPrint("Please use %#StartFloat <options> first.");
380+
MUNLOCK(ErrorMessageLock);
379381
Terminate(-1);
380382
}
381383
/*
@@ -1355,7 +1357,7 @@ int CoChop(UBYTE *s)
13551357
return(1);
13561358
}
13571359
if ( *s == 0 ) {
1358-
MesPrint("&Chop needs a number (float or rational) as an argument.");
1360+
MesPrint("&Chop needs a number (float, rational or power) as an argument.");
13591361
return(1);
13601362
}
13611363
/* Create TYPECHOP header */
@@ -1422,7 +1424,7 @@ int CoChop(UBYTE *s)
14221424
}
14231425
}
14241426
if ( *s ) {
1425-
MesPrint("&Illegal argument(s) in Chop statement: '%s'",s);
1427+
MesPrint("&Illegal argument(s) in Chop statement: '%s'.",s);
14261428
return(1);
14271429
}
14281430
AT.WorkPointer = OldWork;
@@ -2006,8 +2008,10 @@ void SingleTable(mpf_t *tabl, int N, int m, int pow)
20062008
mpf_t jm,jjm;
20072009
mpf_init(jm); mpf_init(jjm);
20082010
if ( pow < 1 || pow > 2 ) {
2009-
printf("Wrong parameter pow in SingleTable: %d\n",pow);
2010-
exit(-1);
2011+
MLOCK(ErrorMessageLock);
2012+
MesPrint("Wrong parameter pow in SingleTable: %d\n",pow);
2013+
MUNLOCK(ErrorMessageLock);
2014+
Terminate(-1);
20112015
}
20122016
if ( m < 0 ) { m = -m; s = -1; }
20132017
mpf_set_si(auxsum,0L);
@@ -2047,8 +2051,10 @@ void DoubleTable(mpf_t *tabout, mpf_t *tabin, int N, int m, int pow)
20472051
mpf_t jm,jjm;
20482052
mpf_init(jm); mpf_init(jjm);
20492053
if ( pow < -1 || pow > 2 ) {
2050-
printf("Wrong parameter pow in SingleTable: %d\n",pow);
2051-
exit(-1);
2054+
MLOCK(ErrorMessageLock);
2055+
MesPrint("Wrong parameter pow in DoubleTable: %d\n",pow);
2056+
MUNLOCK(ErrorMessageLock);
2057+
Terminate(-1);
20522058
}
20532059
if ( m < 0 ) { m = -m; s = -1; }
20542060
mpf_set_ui(auxsum,0L);
@@ -2097,8 +2103,10 @@ void EndTable(mpf_t sum, mpf_t *tabin, int N, int m, int pow)
20972103
mpf_t jm,jjm;
20982104
mpf_init(jm); mpf_init(jjm);
20992105
if ( pow < -1 || pow > 2 ) {
2100-
printf("Wrong parameter pow in SingleTable: %d\n",pow);
2101-
exit(-1);
2106+
MLOCK(ErrorMessageLock);
2107+
MesPrint("Wrong parameter pow in EndTable: %d\n",pow);
2108+
MUNLOCK(ErrorMessageLock);
2109+
Terminate(-1);
21022110
}
21032111
if ( m < 0 ) { m = -m; s = -1; }
21042112
mpf_set_si(sum,0L);
@@ -2271,7 +2279,10 @@ void deltaEulerC(mpf_t result, WORD *indexes, int depth)
22712279
mpf_set_ui(result,0);
22722280
if ( depth == 1 ) {
22732281
if ( indexes[0] == 0 ) {
2274-
printf("Illegal index in depth=1 deltaEulerC: %d\n",indexes[0]);
2282+
MLOCK(ErrorMessageLock);
2283+
MesPrint("Illegal index in depth=1 deltaEulerC: %d\n",indexes[0]);
2284+
MUNLOCK(ErrorMessageLock);
2285+
Terminate(-1);
22752286
}
22762287
if ( indexes[0] < 0 ) SimpleDeltaC(result,indexes[0]);
22772288
else SimpleDelta(result,indexes[0]);
@@ -2332,13 +2343,17 @@ void CalculateMZVhalf(mpf_t result, WORD *indexes, int depth)
23322343
{
23332344
int i;
23342345
if ( depth < 0 ) {
2335-
printf("Illegal depth in CalculateMZVhalf: %d\n",depth);
2336-
exit(-1);
2346+
MLOCK(ErrorMessageLock);
2347+
MesPrint("Illegal depth in CalculateMZVhalf: %d",depth);
2348+
MUNLOCK(ErrorMessageLock);
2349+
Terminate(-1);
23372350
}
23382351
for ( i = 0; i < depth; i++ ) {
23392352
if ( indexes[i] <= 0 ) {
2340-
printf("Illegal index[%d] in CalculateMZVhalf: %d\n",i,indexes[i]);
2341-
exit(-1);
2353+
MLOCK(ErrorMessageLock);
2354+
MesPrint("Illegal index[%d] in CalculateMZVhalf: %d",i,indexes[i]);
2355+
MUNLOCK(ErrorMessageLock);
2356+
Terminate(-1);
23422357
}
23432358
}
23442359
deltaMZV(result,indexes,depth);
@@ -2354,18 +2369,24 @@ void CalculateMZV(mpf_t result, WORD *indexes, int depth)
23542369
GETIDENTITY
23552370
int num1, num2 = 0, i, j = 0;
23562371
if ( depth < 0 ) {
2357-
printf("Illegal depth in CalculateMZV: %d\n",depth);
2358-
exit(-1);
2372+
MLOCK(ErrorMessageLock);
2373+
MesPrint("Illegal depth in CalculateMZV: %d",depth);
2374+
MUNLOCK(ErrorMessageLock);
2375+
Terminate(-1);
23592376
}
23602377
if ( indexes[0] == 1 ) {
2361-
printf("Divergent MZV in CalculateMZV\n");
2362-
exit(-1);
2378+
MLOCK(ErrorMessageLock);
2379+
MesPrint("Divergent MZV in CalculateMZV");
2380+
MUNLOCK(ErrorMessageLock);
2381+
Terminate(-1);
23632382
}
23642383
/* MesPrint("calculateMZV(%a)",depth,indexes); */
23652384
for ( i = 0; i < depth; i++ ) {
23662385
if ( indexes[i] <= 0 ) {
2367-
printf("Illegal index[%d] in CalculateMZV: %d\n",i,indexes[i]);
2368-
exit(-1);
2386+
MLOCK(ErrorMessageLock);
2387+
MesPrint("Illegal index[%d] in CalculateMZV: %d",i,indexes[i]);
2388+
MUNLOCK(ErrorMessageLock);
2389+
Terminate(-1);
23692390
}
23702391
AT.indi1[i] = indexes[i];
23712392
}
@@ -2428,17 +2449,23 @@ void CalculateEuler(mpf_t result, WORD *Zindexes, int depth)
24282449
}
24292450

24302451
if ( depth < 0 ) {
2431-
printf("Illegal depth in CalculateEuler: %d\n",depth);
2432-
exit(-1);
2452+
MLOCK(ErrorMessageLock);
2453+
MesPrint("Illegal depth in CalculateEuler: %d\n",depth);
2454+
MUNLOCK(ErrorMessageLock);
2455+
Terminate(-1);
24332456
}
24342457
if ( indexes[0] == 1 ) {
2435-
printf("Divergent Euler sum in CalculateEuler\n");
2436-
exit(-1);
2458+
MLOCK(ErrorMessageLock);
2459+
MesPrint("Divergent Euler sum in CalculateEuler\n");
2460+
MUNLOCK(ErrorMessageLock);
2461+
Terminate(-1);
24372462
}
24382463
for ( i = 0, j = 0; i < depth; i++ ) {
24392464
if ( indexes[i] == 0 ) {
2440-
printf("Illegal index[%d] in CalculateEuler: %d\n",i,indexes[i]);
2441-
exit(-1);
2465+
MLOCK(ErrorMessageLock);
2466+
MesPrint("Illegal index[%d] in CalculateEuler: %d\n",i,indexes[i]);
2467+
MUNLOCK(ErrorMessageLock);
2468+
Terminate(-1);
24422469
}
24432470
if ( indexes[i] < 0 ) j = 1;
24442471
AT.indi1[i] = indexes[i];
@@ -2558,8 +2585,10 @@ int EvaluateEuler(PHEAD WORD *term, WORD level, WORD par)
25582585
/* euler sum without arguments, i.e. mzv_(), euler_() or mzvhalf_() */
25592586
if ( depth == 0) goto nextfun;
25602587
if ( sumweight > AC.MaxWeight ) {
2561-
MesPrint("Error: Weight of Euler/MZV sum greater than %d",sumweight);
2562-
MesPrint("Please increase MaxWeight in form.set.");
2588+
MLOCK(ErrorMessageLock);
2589+
MesPrint("Error: Weight of Euler/MZV sum greater than %d.",AC.MaxWeight);
2590+
MesPrint("Please increase the maximum weight in %#startfloat.");
2591+
MUNLOCK(ErrorMessageLock);
25632592
Terminate(-1);
25642593
}
25652594
/*

0 commit comments

Comments
 (0)