Skip to content

Commit

Permalink
prevent multiple calls to LoadELF (#201)
Browse files Browse the repository at this point in the history
* fixed state check in the LoadELF (avoid multiple loading)

* added a UT

* fixed UT
  • Loading branch information
val06 authored Oct 17, 2024
1 parent 290d7b3 commit ede90e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func (m *Manager) GetProbe(id ProbeIdentificationPair) (*Probe, bool) {
func (m *Manager) LoadELF(elf io.ReaderAt) error {
m.stateLock.Lock()
defer m.stateLock.Unlock()
if m.state > elfLoaded {
if m.state >= elfLoaded {
return ErrManagerELFLoaded
}
return m.loadELF(elf)
Expand Down
18 changes: 18 additions & 0 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,21 @@ func TestInstructionPatching(t *testing.T) {
t.Fatal(err)
}
}

func TestLoadELF(t *testing.T) {
f, err := os.Open("testdata/patching.elf")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = f.Close() })

m := &Manager{
state: reset,
}
if err = m.LoadELF(f); err != nil {
t.Errorf("LoadELF() error = %v, expected: %v", err, nil)
}
if err = m.LoadELF(f); !errors.Is(err, ErrManagerELFLoaded) {
t.Errorf("LoadELF() error = %v, expected: %v", err, ErrManagerELFLoaded)
}
}

0 comments on commit ede90e3

Please sign in to comment.