staticfg icon indicating copy to clipboard operation
staticfg copied to clipboard

Crash if func_name is None

Open paulschreiber opened this issue 1 year ago • 2 comments

Description

When running code like so:

>>> from staticfg import CFGBuilder
>>> cfg = CFGBuilder().build_from_file('blah.py', './blah.py')

The CFGBuilder crashes if func_name is None.

  File "/opt/homebrew/lib/python3.12/site-packages/staticfg/builder.py", line 275, in visit_Call
    func_name = visit_func(func)
                ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.12/site-packages/staticfg/builder.py", line 267, in visit_func
    func_name += "." + node.attr
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'

The relevant code:

    def visit_Call(self, node):
        def visit_func(node):
            if type(node) == ast.Name:
                return node.id
            elif type(node) == ast.Attribute:
                # Recursion on series of calls to attributes.
                func_name = visit_func(node.value)
                func_name += "." + node.attr
                return func_name

I can work around this like so:

            elif type(node) == ast.Attribute:
                # Recursion on series of calls to attributes.
                func_name = visit_func(node.value)
                if func_name is None:
                    func_name = ""
                func_name += "." + node.attr
                return func_name

but that might just be masking the problem.

paulschreiber avatar Nov 09 '23 16:11 paulschreiber

已收到您的邮件。如果急需回复,请致电15504411531。

liuhao230 avatar Nov 09 '23 16:11 liuhao230

Same thing with me. I applied the same concept, just slightly different:

 def visit_Call(self, node):
     def visit_func(node):
         if type(node) == ast.Name:
             return node.id
         elif type(node) == ast.Attribute:
             # Recursion on series of calls to attributes.
             func_name = visit_func(node.value)
             if func_name is not None:
                 func_name += "." + node.attr
             else:
                 func_name = "unknown_attribute"
             return func_name
         elif type(node) == ast.Constant:
             return node.s
         elif type(node) == ast.Subscript:
             return node.value.id

glad it was an easy fix.

JohnDoughsins avatar Dec 29 '23 02:12 JohnDoughsins