Skip to content

Commit a5dfe18

Browse files
committed
Prevent long node IDs.
Fixes #99
1 parent 6edec70 commit a5dfe18

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

gprof2dot.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import fnmatch
3535
import codecs
3636
import io
37+
import hashlib
3738

3839
assert sys.version_info[0] >= 3
3940

@@ -3535,15 +3536,15 @@ def attr(self, what, **attrs):
35353536

35363537
def node(self, node, **attrs):
35373538
self.write("\t")
3538-
self.id(node)
3539+
self.node_id(node)
35393540
self.attr_list(attrs)
35403541
self.write(";\n")
35413542

35423543
def edge(self, src, dst, **attrs):
35433544
self.write("\t")
3544-
self.id(src)
3545+
self.node_id(src)
35453546
self.write(" -> ")
3546-
self.id(dst)
3547+
self.node_id(dst)
35473548
self.attr_list(attrs)
35483549
self.write(";\n")
35493550

@@ -3559,11 +3560,22 @@ def attr_list(self, attrs):
35593560
first = False
35603561
else:
35613562
self.write(", ")
3562-
self.id(name)
3563+
assert isinstance(name, str)
3564+
assert name.isidentifier()
3565+
self.write(name)
35633566
self.write('=')
35643567
self.id(value)
35653568
self.write(']')
35663569

3570+
def node_id(self, id):
3571+
# Node IDs need to be unique (can't be truncated) but dot doesn't allow
3572+
# IDs longer than 16384 characters, so use an hash instead for the huge
3573+
# C++ symbols that can arise, as seen in
3574+
# https://github.com/jrfonseca/gprof2dot/issues/99
3575+
if isinstance(id, str) and len(id) > 1024:
3576+
id = '_' + hashlib.sha1(id.encode('utf-8'), usedforsecurity=False).hexdigest()
3577+
self.id(id)
3578+
35673579
def id(self, id):
35683580
if isinstance(id, (int, float)):
35693581
s = str(id)

0 commit comments

Comments
 (0)