Skip to content

Commit 4d9b60f

Browse files
committed
sdjournal: close dlopen handle on error in New* functions
We don't want to leak it.
1 parent c14ce9e commit 4d9b60f

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

sdjournal/journal.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,22 @@ func (j *Journal) getFunction(name string) (unsafe.Pointer, error) {
278278
}
279279

280280
// NewJournal returns a new Journal instance pointing to the local journal
281-
func NewJournal() (*Journal, error) {
281+
func NewJournal() (j *Journal, err error) {
282282
h, err := dlopen.GetHandle(libsystemdNames)
283283
if err != nil {
284284
return nil, err
285285
}
286+
defer func() {
287+
if err == nil {
288+
return
289+
}
290+
err2 := h.Close()
291+
if err2 != nil {
292+
err = fmt.Errorf(`%q and "error closing handle: %v"`, err, err2)
293+
}
294+
}()
286295

287-
j := &Journal{lib: h}
296+
j = &Journal{lib: h}
288297

289298
sd_journal_open, err := j.getFunction("sd_journal_open")
290299
if err != nil {
@@ -303,18 +312,27 @@ func NewJournal() (*Journal, error) {
303312
// NewJournalFromDir returns a new Journal instance pointing to a journal residing
304313
// in a given directory. The supplied path may be relative or absolute; if
305314
// relative, it will be converted to an absolute path before being opened.
306-
func NewJournalFromDir(path string) (*Journal, error) {
315+
func NewJournalFromDir(path string) (j *Journal, err error) {
307316
h, err := dlopen.GetHandle(libsystemdNames)
308317
if err != nil {
309318
return nil, err
310319
}
320+
defer func() {
321+
if err == nil {
322+
return
323+
}
324+
err2 := h.Close()
325+
if err2 != nil {
326+
err = fmt.Errorf(`%q and "error closing handle: %v"`, err, err2)
327+
}
328+
}()
311329

312330
path, err = filepath.Abs(path)
313331
if err != nil {
314332
return nil, err
315333
}
316334

317-
j := &Journal{lib: h}
335+
j = &Journal{lib: h}
318336

319337
sd_journal_open_directory, err := j.getFunction("sd_journal_open_directory")
320338
if err != nil {

0 commit comments

Comments
 (0)