Skip to content

Fix/replace log.Fatal with error propagation in library init and fix file leak - #13

Open
krisharma955 wants to merge 2 commits into
aboutcode-org:mainfrom
krisharma955:fix/error-propagation-and-file-leak
Open

Fix/replace log.Fatal with error propagation in library init and fix file leak#13
krisharma955 wants to merge 2 commits into
aboutcode-org:mainfrom
krisharma955:fix/error-propagation-and-file-leak

Conversation

@krisharma955

Copy link
Copy Markdown

This PR fixes two issues:

  1. purlvalidator.go: The library's init() function called log.Fatal() if the embedded FST failed to load, which crashes any program that imports this package with no way for the caller to handle the error. Replaced with lazy sync.Once initialization so Validate() returns (false, error) instead of terminating the process.

  2. cmd/main.go: Added defer f.Close() after os.Create("purls.fst") to prevent a file descriptor leak.

Changes

  • purlvalidator.go: Removed init() with log.Fatal, added getValidator() using sync.Once for thread-safe lazy initialization. Public API (Validate) unchanged.
  • cmd/main.go: Added defer f.Close() after file creation.

Handle the error returned by os.ReadFile() in insert_purls instead
of discarding it with _. Previously, if a data file could not be read,
zero PURLs were silently inserted into the FST with no indication of
failure.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>

Signed-off-by: krishsharma955 <krishsharma9005@gmail.com>
… leak

Replace init() with sync.Once lazy initialization in purlvalidator.go
so the library no longer crashes consumers on FST load failure. Add
defer f.Close() in cmd/main.go to prevent file descriptor leak.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>

Signed-off-by: krishsharma955 <krishsharma9005@gmail.com>
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

Successfully merging this pull request may close these issues.

1 participant