另一个问题(Lua中字符串转换为表格)问如何将格式化为表格的字符串转换为表格,所给出的答案是使用
我也有一个程序,它使用
loadstring
或load
将字符串转换为代码块,然后执行它。我也有一个程序,它使用
http.request
下载一个格式化为lua表格的文件,就像其他问题一样:yourTable = http.request("http://www.somesite.com/table.txt")
print(yourTable)
--yourTable is a string that is formatted like a lua table, but not a table:
a={
b = {
c = 1,
d = {
e = {
},
},
},
}
functionThatExpectsATable(yourTable) --throws error because yourTable is a string
虽然我可以使用load
或loadstring
来获取所需的表格,但这是一个潜在的安全漏洞,因为我的程序允许用户输入任何URL来加载他们的表格数据。如果他们指向了Lua代码而不是Lua格式化的表格,那么该代码将被执行。
如何将“表格式化字符串”转换为表格而不执行它?