MightyPirates / OpenComputers

Home of the OpenComputers mod for Minecraft.
https://oc.cil.li
Other
1.59k stars 430 forks source link

[MC 1.12.2 OC 1.7.5.192] Calling event.pull when a variable named "type" is defined from .shrc crashes computer #3220

Closed Tacodiva closed 4 years ago

Tacodiva commented 4 years ago

Description When you call event.pull from a script run by .shrc when a variable named "type" is defined the computer blue screens with error /lib/process.lua:112: attempt to call global 'type' (a string value). As the error suggests if you change the name of the variable to something other than "type" it works as intended.

Steps to reproduce

  1. Set up a computer with the following code in /home/test.lua:
    event = require("event")
    type = "Hello World" --If you remove this line or change the variable's name this code works as intended.
    event = event.pull("key")
  2. Add test to /home/.shrc
  3. Reboot the computer
SquidDev commented 4 years ago

You should be declaring your variables as local. Doing type = ... on its own, without declaring a variable will pollute the global environment, hence the crash. The following code should work:

local modem = require("component").modem
local event = require("event")
modem.open(20)
local type = "Hello World"
event = event.pull("key")
LizzyTrickster commented 4 years ago

That's because type is a keyword in Lua and when you change it before the event.pull() it overwrites the global one. The solution @SquidDev suggested might work, but you'd be better off using a different variable name.

Tacodiva commented 4 years ago

@SquidDev @LizzyTrickster Thanks (I'm new to lua and don't really know the conventions) - this is still a bug though right? It doesn't seem like it should basically brick the computer if you do this and the fact that it only crashes when the script is called on startup seems weird.

LizzyTrickster commented 4 years ago

It's not a bug, the process.lua lib is one of the low-level libs of OpenOS. If you overwrite keywords of course it's going to crash out the computer.

So long as you local any variables, you shouldn't have issues with stuff outside of your script because the local keyword (so long as it's defined after the require() lines) keeps it local to your script and nothing more.

payonel commented 4 years ago

you've put a broken script in your shell start-up, so you're going to get repeated calls to an unhappy program. this is like saying openos has a bug because someone wrote this:

  echo reboot > /home/.shrc && reboot

And wabam -- endless reboot loop. openos bug? nah

Also, i would never recommend you change globals, and more importantly, don't use standard lua tables and method names for your own identifiers

this code would be better

local modem = require("component").modem
local event = require("event")
modem.open(20)
local txt = "Hello World"
local event_pull_response = event.pull("key")

and event.pull has a variable arg return, so I would pack it:

local event_pull_response = table.pack(event.pull("key"))

see https://www.lua.org/manual/5.3/manual.html#6.6 for more info