Skip to content

Commit

Permalink
Merge pull request #908 from wakatime/bugfix/offline-nil-pointer
Browse files Browse the repository at this point in the history
Fix nil pointer dereference when catching panic from bbolt
  • Loading branch information
gandarez authored Aug 15, 2023
2 parents cb408d8 + b626431 commit c847afa
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions pkg/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math"
"net/http"
"path/filepath"
"runtime/debug"
"time"

"github.com/wakatime/wakatime-cli/pkg/api"
Expand Down Expand Up @@ -398,18 +397,15 @@ func ReadHeartbeats(filepath string, limit int) ([]heartbeat.Heartbeat, error) {

// openDB opens a connection to the offline db.
// It returns the pointer to bolt.DB, a function to close the connection and an error.
func openDB(filepath string) (*bolt.DB, func(), error) {
var err error

// Although named parameters should be avoided, this func uses them to access inside the deferred function and set an error.
func openDB(filepath string) (db *bolt.DB, _ func(), err error) {
defer func() {
if r := recover(); r != nil {
err = ErrOpenDB{Err: fmt.Errorf("panicked: %v", r)}

log.Errorf("%w. Stack: %s", err, string(debug.Stack()))
}
}()

db, err := bolt.Open(filepath, 0600, &bolt.Options{Timeout: 30 * time.Second})
db, err = bolt.Open(filepath, 0600, &bolt.Options{Timeout: 30 * time.Second})
if err != nil {
return nil, nil, fmt.Errorf("failed to open db file: %s", err)
}
Expand Down

0 comments on commit c847afa

Please sign in to comment.