stevedonovan/Penlight — Issue #19: pretty.write fails to write tables with keys other than strings or numbers
Author: jdesgats
Created: 2012-01-01 17:34:22+00:00
State: open

Whenever you call pl.pretty.write with a table with keys which are not strings or numbers in clever mode, it crashes because it tries to call method `find`.

> return require"pl.pretty".write{[{}] = "crash"}
/home/julien/.luarocks/share/lua/5.1//pl/pretty.lua:78: attempt to call method 'find' (a nil value)
Stack traceback:
At @/home/julien/.luarocks/share/lua/5.1//pl/pretty.lua:78 (in upvalue is_identifier)
0075: local tables = {}
0076:
0077: local function is_identifier (s)
*0078: return (s:find('^[%a_][%w_]*$')) and not keywords[s]
0079: end
0080:
0081: local function put(s)
At @/home/julien/.luarocks/share/lua/5.1//pl/pretty.lua:149 (in local writeit)
0146: writeit(val,indent,newindent)
0147: else
0148: if not numkey or not used[key] then -- non-array indices
*0149: if numkey or not is_identifier(key) then
0150: key = index(numkey,key)
0151: end
0152: put(indent..key..set)
At @/home/julien/.luarocks/share/lua/5.1//pl/pretty.lua:163 (in ?)
0160: putln(tostring(t)..',')
0161: end
0162: end
*0163: writeit(tbl,'',space)
0164: eat_last_comma()
0165: return concat(lines,#space > 0 and '\n' or '')
0166: end