Skip to content

Commit

Permalink
fix(cookie-manager): handle empty bytes without decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Jul 30, 2024
1 parent e137e12 commit af19974
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions pkg/credman/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type CookieManager struct {
f *os.File
f *os.File
filePath string
key []byte
cookies map[string]*types.Cookie
Expand All @@ -33,11 +33,6 @@ func NewCookieManager(filePath string, key []byte) (*CookieManager, error) {
}

func (cm *CookieManager) loadCookies() error {
/* _, err := os.Stat(cm.filePath)
if os.IsNotExist(err) {
return nil
}
*/
var err error
cm.f, err = os.OpenFile(cm.filePath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
Expand All @@ -49,22 +44,20 @@ func (cm *CookieManager) loadCookies() error {
if err != nil {
return err
}

if len(cookiesData) == 0 { // don't decode empty data
return nil
}
buf := bytes.NewBuffer(cookiesData)
dec := gob.NewDecoder(buf)
err = dec.Decode(&cm.cookies)

if err != nil {
return err
}
return nil
}

func (cm *CookieManager) saveCookies() error {
/*file, err := os.Create(cm.filePath)
if err != nil {
return err
}
defer file.Close()*/
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(cm.cookies)
Expand Down Expand Up @@ -126,4 +119,4 @@ func (cm *CookieManager) UpdateCookie(cookie *types.Cookie) error {
func (cm *CookieManager) Close() error {
defer cm.f.Close()
return cm.saveCookies()
}
}

0 comments on commit af19974

Please sign in to comment.