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

segfault when opening rules file #124

Open
jyoung15 opened this issue Oct 18, 2022 · 1 comment
Open

segfault when opening rules file #124

jyoung15 opened this issue Oct 18, 2022 · 1 comment

Comments

@jyoung15
Copy link

When attempting to open a rules files that is unreadable (for example due to file permissions), the program crashes:

Started Go Elasticsearch Alerts.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x713a16]
goroutine 1 [running]:
os.(*File).Name(...)
        /usr/lib/go/src/os/file.go:57
github.com/morningconsult/go-elasticsearch-alerts/config.ParseRules()
        /home/xxxxx/go-elasticsearch-alerts.git/config/parse.go:295 +0x476

This appears to be due to invalid use of the file pointer when os.Open returns an error. Suggested fix below. Can submit pull request upon request.

diff --git a/config/parse.go b/config/parse.go
index 9bb5d12..6b700b6 100644
--- a/config/parse.go
+++ b/config/parse.go
@@ -290,11 +290,11 @@ func ParseRules() ([]RuleConfig, error) {
                file, err := os.Open(filepath.Clean(ruleFile))
                if err != nil {
                        if os.IsNotExist(err) {
                                continue
                        }
-                       return nil, xerrors.Errorf("error opening file %s: %v", file.Name(), err)
+                       return nil, xerrors.Errorf("error opening file: %v", err)
                }

                dec := json.NewDecoder(file)
                dec.UseNumber()

With patch above applied, error message appears as expected:

error opening file: open rules/xxxxx.json: permission denied
@alexdulin
Copy link
Member

Hi @jyoung15, thanks for pointing this out! Please feel free to open a pull request with your suggested fix. Contributions are welcome!

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

2 participants