Jump to content




metatable variable protection


  • You cannot reply to this topic
5 replies to this topic

#1 Exerro

  • Members
  • 801 posts

Posted 13 March 2013 - 11:36 AM

Is it possible to make it so if you try and change a variable in a table it will call a metamethod or prevent it? I've been playing around with __index and __newindex but neither of them seem to work and I can't find any more metamethods related to indexing, just math.

I want to make it so if someone tries to delete/edit a table I have, it will return false and do nothing

#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 13 March 2013 - 11:44 AM

Even if you have a metamethod linked to __index, I think a hacker can simply rawset() your table to nil. (I'm not sure...)

#3 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 13 March 2013 - 11:50 AM

There's only those (__index and __newindex), wich only work when you get (__index) a value from the table, or set new key (__newindex).
So no, you can't do it through metatables. Also, like LBPHacker said, one could simply use rawget/rawset to bypass the metatable.

#4 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 13 March 2013 - 02:18 PM

Overwrite rawset/rawget? Haha.

#5 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 13 March 2013 - 02:25 PM

View PostKingdaro, on 13 March 2013 - 02:18 PM, said:

Overwrite rawset/rawget? Haha.
Sure, that could (if done right) stop you from bypassing the metatable, but there's still no way to protect against changes to the table.

#6 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 13 March 2013 - 04:29 PM

A table will access its metatable's __index and __newindex when someone tries to get or set a value to/from it that does not already exist.
You can use this to stop them adding things if you treat the table you are giving the person as a reference to a hidden table ie give the reference table a metatable that will return values from the real table when indexed, but make newindex either empty or make it throw an error so they can't actually add anything to the table.
Make sure the error is on a higher level so it doesn't tell them where in your code you're causing it to error.
They can still use rawget and rawset, but they will only affect the reference table.
Don't forget to make the metatable's __metatable value reference something that isn't the actual metatable so they can't use setmetatable to remove the metatable.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users