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

Handle empty binaries #16

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ Changelog
=========


v0.3.1 (2024-09-06)
------------------------

- Return nothing if not a Go binary


v0.3.0 (2024-09-05)
------------------------

Expand Down
25 changes: 20 additions & 5 deletions src/go_inspector/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,30 @@ def collect_go_package(location, **kwargs):
return _collect_go_package_from_data(go_data, location=location)


def get_build_info(go_data):
if not go_data or not "BuildInfo" in go_data:
return

build_info = go_data["BuildInfo"]
if (
not build_info.get("GoVersion")
and not build_info.get("Path")
and not build_info.get("Settings")
):
return
main = build_info.get("Main") or {}
if not main.get("Path") and not main.get("Version") and not main.get("Sum"):
return
return build_info


def _collect_go_package_from_data(go_data, location, **kwargs):
"""
Yield a Go PackageData found in the Go binary file ``go_data`` mapping extracted from
``location`` string. Raise exceptions on errors.
"""
if not go_data:
return

if not (build_info := go_data.get("BuildInfo")):
build_info = get_build_info(go_data)
if not build_info:
return

package_data = get_main_package(build_info=build_info, location=location)
Expand Down Expand Up @@ -172,7 +187,7 @@ def get_main_package(build_info, location):
extra_data=extra_data,
)
purl = PackageURL(type="generic", name=pathloc, qualifiers=dict(path=location))
pkg.set_purl(package_url=purl())
pkg.set_purl(package_url=purl)

elif main_module:
pkg = main_module.as_package_data()
Expand Down
Loading