Skip to content

Commit b24f29a

Browse files
committed
protect __tostring for remaining data types
1 parent 365ffd9 commit b24f29a

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/microscope.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ local function db_node( db, val, depth, key )
269269
(key == nil or not db.prune[ key ]) then
270270
db.n_nodes = db.n_nodes + 1
271271
node = {
272-
id = tostring( db.n_nodes ),
272+
id = db.n_nodes.."",
273273
value = val,
274274
depth = depth,
275275
shape = nil, label = nil, draw = nil, next = nil,
@@ -435,7 +435,7 @@ end
435435
local function make_label_elem( tnode, v, db, subid, depth, alt )
436436
local t = type( v )
437437
if t == "number" or t == "boolean" then
438-
return escape( tostring( v ), db.use_html )
438+
return escape( ptostring( v ), db.use_html )
439439
elseif t == "string" then
440440
return quote( escape( abbrev( v ), db.use_html ) )
441441
else -- userdata, function, thread, table
@@ -465,7 +465,7 @@ local function make_html_table( db, node, val )
465465
-- first the array part
466466
local n, v = 1, rawget( val, 1 )
467467
while v ~= nil do
468-
local el_label = make_label_elem( node, v, db, tostring( n ), depth )
468+
local el_label = make_label_elem( node, v, db, n.."", depth )
469469
label = label .. [[
470470
<TR><TD PORT="]] .. n .. [[" COLSPAN="2">]] .. el_label .. [[
471471
</TD></TR>
@@ -503,7 +503,7 @@ local function make_record_table( db, node, val )
503503
-- first the array part
504504
local n,v = 1, rawget( val, 1 )
505505
while v ~= nil do
506-
local el_label = make_label_elem( node, v, db, tostring( n ), depth )
506+
local el_label = make_label_elem( node, v, db, n.."", depth )
507507
label = label .. " | <" .. n .. "> " .. el_label
508508
handled[ n ] = true
509509
n = n + 1
@@ -550,7 +550,7 @@ local function make_html_stack( db, node )
550550
n = n + 1
551551
for i = #frame, 1, -1 do
552552
label = label .. ' <TR><TD>' ..
553-
escape( tostring( i ), true ) .. '</TD><TD>' ..
553+
escape( i.."", true ) .. '</TD><TD>' ..
554554
escape( abbrev( frame[ i ][ 1 ] ), true ) ..
555555
'</TD><TD PORT="' .. n .. '">' ..
556556
make_label_elem( node, frame[ i ][ 2 ], db, n, depth ) ..
@@ -582,7 +582,7 @@ local function make_record_stack( db, node )
582582
n = n + 1
583583
local nums, keys, values = {}, {}, {}
584584
for i = #frame, 1, -1 do
585-
nums[ #nums+1 ] = escape( tostring( i ), false )
585+
nums[ #nums+1 ] = escape( i.."", false )
586586
keys[ #keys+1 ] = escape( abbrev( frame[ i ][ 1 ] ), false )
587587
values[ #values+1 ] = "<" .. n .. "> " ..
588588
make_label_elem( node, frame[ i ][ 2 ], db, n, depth )
@@ -713,7 +713,7 @@ end
713713

714714

715715
local function dottify_thread( db, node, val )
716-
local label = escape( abbrev( tostring( val ) ), false )
716+
local label = escape( abbrev( ptostring( val ) ), false )
717717
node.group = label
718718
if getsize then
719719
label = label.." ["..getsize( val ).."]"
@@ -730,7 +730,7 @@ end
730730

731731

732732
local function dottify_function( db, node, val )
733-
local label = escape( abbrev( tostring( val ) ), false )
733+
local label = escape( abbrev( ptostring( val ) ), false )
734734
if getsize then
735735
label = label.." ["..getsize( val ).."]"
736736
end
@@ -751,14 +751,14 @@ end
751751

752752

753753
local function dottify_other( db, node, val )
754-
node.label = escape( abbrev( tostring( val ) ), false )
754+
node.label = escape( abbrev( ptostring( val ) ), false )
755755
node.shape = "plaintext"
756756
end
757757

758758

759759
local function dottify_stack( db, node )
760760
if node.thread then
761-
node.group = escape( abbrev( tostring( node.thread ) ), false )
761+
node.group = escape( abbrev( ptostring( node.thread ) ), false )
762762
end
763763
if db.use_html then
764764
make_html_stack( db, node )
@@ -769,7 +769,7 @@ end
769769

770770

771771
local function dottify_size( db, node, val )
772-
node.label = escape( abbrev( tostring( val ) ), false )
772+
node.label = escape( abbrev( val.."" ), false )
773773
node.shape = "circle"
774774
node.width = "0.3"
775775
node.margin = "0.01"

tests/microscope1.test.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,16 +463,16 @@ end
463463

464464
do
465465
local t = {}
466-
setmetatable( t, {
467-
__tostring = function( v )
468-
error( "Argh!" )
469-
end
470-
} )
466+
local mt = { __tostring = function() error( "Argh!" ) end }
467+
setmetatable( t, mt )
471468
local u = newproxy( true )
472469
getmetatable( u ).__tostring = function( u )
473470
error( "Argh!" )
474471
end
475-
dot( { t, u }, "__tostring raising error" )
472+
debug.setmetatable( 1, mt )
473+
debug.setmetatable( true, mt )
474+
dot( { t, u, false, 123 }, "__tostring raising error" )
475+
mt.__tostring = nil
476476
end
477477

478478
-- TODO ;-)

0 commit comments

Comments
 (0)