Skip to content

Commit 96838b6

Browse files
committed
Prevent infinite loop by adding a Call stack overflow
1 parent 17304f3 commit 96838b6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

index.js

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
var balanced = require("balanced-match")
55
var reduceFunctionCall = require("reduce-function-call")
66

7+
/**
8+
* Constantes
9+
*/
10+
var MAX_STACK = 100 // should be enough for a single calc()...
11+
12+
/**
13+
* Global variables
14+
*/
15+
var stack
16+
717
/**
818
* Expose reduceCSSCalc plugin
919
*
@@ -17,6 +27,7 @@ module.exports = reduceCSSCalc
1727
* @param {String} value css input
1828
*/
1929
function reduceCSSCalc(value) {
30+
stack = 0
2031
return reduceFunctionCall(value, /((?:\-[a-z]+\-)?calc)\(/, evaluateExpression)
2132
}
2233

@@ -29,6 +40,11 @@ function reduceCSSCalc(value) {
2940
*/
3041

3142
function evaluateExpression (expression, functionIdentifier, call) {
43+
if (stack++ > MAX_STACK) {
44+
stack = 0
45+
throw new Error("Call stack overflow for " + call)
46+
}
47+
3248
if (expression === "") {
3349
throw new Error(functionIdentifier + "(): '" + call + "' must contain a non-whitespace string")
3450
}

0 commit comments

Comments
 (0)