Skip to content

Commit ed3bc65

Browse files
committed
fix Lua 5.4 CLI loadstring crashes
1 parent 5db664f commit ed3bc65

2 files changed

Lines changed: 40 additions & 15 deletions

File tree

src/cli.lua

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ local function lines_from(file)
4545
return lines
4646
end
4747

48+
local function load_chunk(content, chunkName, environment)
49+
if _VERSION == "Lua 5.1" then
50+
local func, err = loadstring(content, chunkName)
51+
if not func then
52+
return nil, err
53+
end
54+
if environment then
55+
setfenv(func, environment)
56+
end
57+
return func
58+
end
59+
60+
return load(content, chunkName, "t", environment)
61+
end
62+
4863
-- CLI
4964
local config, sourceFile, outFile, luaVersion, prettyPrint
5065

@@ -76,9 +91,10 @@ while i <= #arg do
7691

7792
local content = table.concat(lines_from(filename), "\n")
7893
-- Load Config from File
79-
local func = loadstring(content)
80-
-- Sandboxing
81-
setfenv(func, {})
94+
local func, err = load_chunk(content, "@" .. filename, {})
95+
if not func then
96+
Prometheus.Logger:error(string.format('Failed to parse config file "%s": %s', filename, tostring(err)))
97+
end
8298
config = func()
8399
elseif curr == "--out" or curr == "--o" then
84100
i = i + 1

src/prometheus/compiler/expressions/boolean.lua

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@
55
-- This Script contains the expression handler for the BooleanExpression.
66

77
local Ast = require("prometheus.ast");
8+
9+
local expressionEvaluators = {
10+
[Ast.GreaterThanExpression] = function(left, right)
11+
return left > right
12+
end,
13+
[Ast.LessThanExpression] = function(left, right)
14+
return left < right
15+
end,
16+
[Ast.GreaterThanOrEqualsExpression] = function(left, right)
17+
return left >= right
18+
end,
19+
[Ast.LessThanOrEqualsExpression] = function(left, right)
20+
return left <= right
21+
end,
22+
[Ast.NotEqualsExpression] = function(left, right)
23+
return left ~= right
24+
end,
25+
}
26+
827
local function createRandomASTCFlowExpression(resultBool)
928
local expTB = {
1029
Ast.GreaterThanExpression,
@@ -14,21 +33,12 @@ local function createRandomASTCFlowExpression(resultBool)
1433
Ast.NotEqualsExpression
1534
}
1635

17-
local expLookup = {
18-
[Ast.GreaterThanExpression] = ">";
19-
[Ast.LessThanExpression] = "<";
20-
[Ast.GreaterThanOrEqualsExpression] = ">=";
21-
[Ast.LessThanOrEqualsExpression] = "<=";
22-
[Ast.NotEqualsExpression] = "~=";
23-
}
24-
25-
local leftInt, rightInt, boolResult, r3, randomExp
36+
local leftInt, rightInt, boolResult, randomExp
2637
repeat
2738
randomExp = expTB[math.random(1, #expTB)]
2839
leftInt = Ast.NumberExpression(math.random(1, 2^24))
2940
rightInt = Ast.NumberExpression(math.random(1, 2^24))
30-
r3 = "return " .. leftInt.value .. expLookup[randomExp] .. rightInt.value
31-
boolResult = loadstring(r3)()
41+
boolResult = expressionEvaluators[randomExp](leftInt.value, rightInt.value)
3242
until boolResult == resultBool
3343

3444
return randomExp(leftInt, rightInt, false)
@@ -47,4 +57,3 @@ return function(self, expression, _, numReturns)
4757
end
4858
return regs;
4959
end;
50-

0 commit comments

Comments
 (0)