Skip to content

Commit

Permalink
Prevent long node IDs.
Browse files Browse the repository at this point in the history
Fixes #99
  • Loading branch information
jrfonseca committed Nov 9, 2024
1 parent 6edec70 commit a5dfe18
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions gprof2dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import fnmatch
import codecs
import io
import hashlib

assert sys.version_info[0] >= 3

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

def node(self, node, **attrs):
self.write("\t")
self.id(node)
self.node_id(node)
self.attr_list(attrs)
self.write(";\n")

def edge(self, src, dst, **attrs):
self.write("\t")
self.id(src)
self.node_id(src)
self.write(" -> ")
self.id(dst)
self.node_id(dst)
self.attr_list(attrs)
self.write(";\n")

Expand All @@ -3559,11 +3560,22 @@ def attr_list(self, attrs):
first = False
else:
self.write(", ")
self.id(name)
assert isinstance(name, str)
assert name.isidentifier()
self.write(name)
self.write('=')
self.id(value)
self.write(']')

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

def id(self, id):
if isinstance(id, (int, float)):
s = str(id)
Expand Down

0 comments on commit a5dfe18

Please sign in to comment.