contentenc: better error reporting in ParseHeader

Log the message ourselves and return EINVAL.

Before:

	gocryptfs[26962]: go-fuse: can't convert error type: ParseHeader: invalid version: got 0, want 2

After:

	gocryptfs[617]: ParseHeader: invalid version: want 2, got 0. Returning EINVAL.
This commit is contained in:
Jakob Unterwurzacher 2017-05-25 14:18:44 +02:00
parent 70c16fde4c
commit c0e411f81d

View File

@ -6,10 +6,11 @@ package contentenc
import ( import (
"encoding/binary" "encoding/binary"
"fmt"
"log" "log"
"syscall"
"github.com/rfjakob/gocryptfs/internal/cryptocore" "github.com/rfjakob/gocryptfs/internal/cryptocore"
"github.com/rfjakob/gocryptfs/internal/tlog"
) )
const ( const (
@ -43,12 +44,14 @@ func (h *FileHeader) Pack() []byte {
// ParseHeader - parse "buf" into fileHeader object // ParseHeader - parse "buf" into fileHeader object
func ParseHeader(buf []byte) (*FileHeader, error) { func ParseHeader(buf []byte) (*FileHeader, error) {
if len(buf) != HeaderLen { if len(buf) != HeaderLen {
return nil, fmt.Errorf("ParseHeader: invalid length: got %d, want %d", len(buf), HeaderLen) tlog.Warn.Printf("ParseHeader: invalid length: want %d bytes, got %d. Returning EINVAL.", HeaderLen, len(buf))
return nil, syscall.EINVAL
} }
var h FileHeader var h FileHeader
h.Version = binary.BigEndian.Uint16(buf[0:headerVersionLen]) h.Version = binary.BigEndian.Uint16(buf[0:headerVersionLen])
if h.Version != CurrentVersion { if h.Version != CurrentVersion {
return nil, fmt.Errorf("ParseHeader: invalid version: got %d, want %d", h.Version, CurrentVersion) tlog.Warn.Printf("ParseHeader: invalid version: want %d, got %d. Returning EINVAL.", CurrentVersion, h.Version)
return nil, syscall.EINVAL
} }
h.ID = buf[headerVersionLen:] h.ID = buf[headerVersionLen:]
return &h, nil return &h, nil