1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-26 07:03:22 +00:00

Prevent use of shell as a hashbang program

This doesn't do what you want (it'll infinite loop), so while special
casing it /is/ ugly, it's better than a confusing error.

Closes #1386.
This commit is contained in:
Jonathan Coates 2023-03-28 20:57:21 +01:00
parent 7535972a30
commit e655c6d302
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
2 changed files with 20 additions and 1 deletions

View File

@ -132,6 +132,13 @@ local function executeProgram(remainingRecursion, path, args)
if not resolvedHashbangProgram then
printError("Hashbang program not found: " .. originalHashbangPath)
return false
elseif resolvedHashbangProgram == "rom/programs/shell.lua" and #hashbangArgs == 0 then
-- If we try to launch the shell then our shebang expands to "shell <program>", which just does a
-- shell.run("<program>") again, resulting in an infinite loop. This may still happen (if the user
-- has a custom shell), but this reduces the risk.
-- It's a little ugly special-casing this, but it's probably worth warning about.
printError("Cannot use the shell as a hashbang program")
return false
end
-- Add the path and any arguments to the interpreter's arguments

View File

@ -21,14 +21,16 @@ describe("The shell", function()
expect(args):same { [0] = "/test-rom/data/dump-args", "arg1", "arg 2" }
end)
end)
describe("hashbangs", function()
local function make_hashbang_file(target, filename)
local tmp = fs.open(filename or "test-files/out.lua", "w")
tmp.write("#!" .. target)
tmp.close()
end
it("supports hashbangs", function()
it("has basic support", function()
make_hashbang_file("/test-rom/data/dump-args")
shell.execute("test-files/out.lua", "arg1", "arg2")
@ -82,6 +84,16 @@ describe("The shell", function()
make_hashbang_file("test-files/out.lua")
expect(shell.execute("test-files/out.lua")):eq(false)
end)
it("returns error for using the shell", function()
make_hashbang_file("shell")
expect(shell.execute("test-files/out.lua")):eq(false)
end)
it("allows running a shell with arguments", function()
make_hashbang_file("shell /test-rom/data/dump-args")
expect(shell.execute("test-files/out.lua")):eq(true)
end)
end)
describe("shell.run", function()