Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash if func_name is None #18

Open
paulschreiber opened this issue Nov 9, 2023 · 2 comments
Open

Crash if func_name is None #18

paulschreiber opened this issue Nov 9, 2023 · 2 comments

Comments

@paulschreiber
Copy link

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.

@liuhao230
Copy link

liuhao230 commented Nov 9, 2023 via email

@JohnDoughsins
Copy link

JohnDoughsins commented Dec 29, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants