This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mrpoopy345's profile picture

Loadfile error checking not working?

Started by mrpoopy345, 31 May 2015 - 05:53 PM
mrpoopy345 #1
Posted 31 May 2015 - 07:53 PM
I am trying to make a program that makes an error quine using the following algorithm:

Start out with a seed (A piece of code that errors)
Take the error the code produces and make that error the new program (Done with a remote program)
Continue step 2 until the source code of the program = the error it produces

This is my code in the main program (This is in golf.lua and the program the seed is in is newgolf.lua, I know the name of the programs are weird, they were just empty lua files I had)

r = 0
s = "newgolf.lua"
while true do
h = io.open(s, "r")
l = h:read("*all")
h.close()
tempfunc, errmsg = loadfile(s)
if errmsg == l then
	print(r)
	break
else
	r = r+1
	h = io.open(s, "w")
	h:write(errmsg)
	h:close()
	print(r..": "..errmsg)
end
end
If I make the seed in newgolf.lua something like oi;gzsjgd or anything where I just bang my head against my keyboard it works as expected, however if I put valid "looking" lua code that actually errors (Something like printfunction({}) ) it says that errmsg is nil even though tempfunc would error. Why is this, and how can I fix it?
valithor #2
Posted 31 May 2015 - 09:00 PM
The problem is how loadfile actually works. This is to say all loadfile is doing is converting a given file into a function, which can then be run at a later date.

So if the given file follows all the rules of how lua code is supposed to look, then it will compile. This does not mean it will run without erroring.
Example:

print("hello")
sleep(5)
thisisnotafunction()

This will run, but after the sleep it will error with attempt to call nil on line three. So it compiles, but there is still a error after running.

While this does not follow the rules:

print("hello")
sleep(5)
thisisnotafunction
This will error when loadstring is called on it (which is what loadfile is doing), and will result in a '=' expected on line 3 error.

The only thing I can think of to catch errors like this, would be to utilize pcall.


tempfunc, errmsg = loadfile(s)
if errmsg ~= nil then
  -- it errored
else
  ok,err = pcall(tempfunc) -- checking to see if the code itself errors
  if not ok then
	-- it errored
  else
	-- it didn't
  end
end

Granted you will most likely want to both sandbox the pcall, and implement some form of a timeout for it so it doesn't just run forever.
Edited on 31 May 2015 - 07:06 PM
flaghacker #3
Posted 31 May 2015 - 09:29 PM
until the source code of the program = the error it produces

When will that ever happen? Could you give some example code?
mrpoopy345 #4
Posted 01 June 2015 - 11:32 AM
If the seed is, say "abcdefghijklmnopqrstuvwxyz" It will go "newgolf.lua:syntax error near <eof>" which then compiles and gives the error "newgolf.lua:1: <name> expected near '1'"

Valithor, I am testing what you said right now, I'll edit my post when I have tried it.
mrpoopy345 #5
Posted 01 June 2015 - 11:55 AM
@Valithor It worked! Thank you so much! Follow up quesiton, can you ever have a seed that makes it take more than 2 steps to get to an error quine?
Bomb Bloke #6
Posted 02 June 2015 - 04:56 AM
Seems to me that the first error you get will always start with "<script>:1:", which'll always generate the same "<name> expected" errors from then on.