From 65e678ba1d47c9e23c6a75268d4499b8a18f08b6 Mon Sep 17 00:00:00 2001
From: Travis Cross <tc@traviscross.com>
Date: Thu, 25 Dec 2014 10:11:47 +0000
Subject: [PATCH] Fix mod_expr `clamp` function

The clamp(v,a,b) function wraps v around the interval [a,b).

However prior to this commit, `clamp` was ignoring the third argument
and using the second argument again in its place.  This resulted in a
division by zero.  Hence `clamp` didn't work at all.

Even if the arguments were treated correctly, `clamp` incorrectly
multiplied rather than added whenever v < a.  This would have produced
bogus results.  (Thanks to Shona McNeill for pointing this out.)

Note that as implemented, `clamp` is undefined for b >= a.

These errors are present in the last upstream C version, v2.7.
They've been corrected in the C++ version that upstream now maintains
instead.

Thanks-to: Shona McNeill <prufrax@googlemail.com>

FS-7070 #resolve
---
 src/mod/applications/mod_expr/exprilfs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/mod/applications/mod_expr/exprilfs.h b/src/mod/applications/mod_expr/exprilfs.h
index ad2b5515e0..7efc281c16 100644
--- a/src/mod/applications/mod_expr/exprilfs.h
+++ b/src/mod/applications/mod_expr/exprilfs.h
@@ -763,7 +763,7 @@ case EXPR_NODEFUNC_CLAMP:
 		err = exprEvalNode(obj, nodes->data.function.nodes, 1, &d1);
 
 	if (!err)
-		err = exprEvalNode(obj, nodes->data.function.nodes, 1, &d2);
+		err = exprEvalNode(obj, nodes->data.function.nodes, 2, &d2);
 
 	if (!err) {
 		EXPR_RESET_ERR();
@@ -771,7 +771,7 @@ case EXPR_NODEFUNC_CLAMP:
 		EXPR_CHECK_ERR();
 
 		if (tmp < 0.0)
-			*val = tmp * d2;
+			*val = tmp + d2;
 		else
 			*val = tmp + d1;
 	} else