Skip to content

Commit

Permalink
wrap some errors and fix static check error style warning (#433)
Browse files Browse the repository at this point in the history
pkg/[cbfs, intel/microcode, uefi]: wrap errors and fix error strings (errors should not be capitalized)

Signed-off-by: Siarhiej Siemianczuk <[email protected]>
Co-authored-by: ron minnich <[email protected]>
  • Loading branch information
binjip978 and rminnich authored Sep 17, 2024
1 parent 19e147b commit cbe89b1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pkg/cbfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,5 @@ func (f *File) Decompress() ([]byte, error) {
compressor := compression.LZ4{}
return compressor.Decode(f.FData)
}
return nil, fmt.Errorf("Unknown compression")
return nil, fmt.Errorf("unknown compression")
}
10 changes: 5 additions & 5 deletions pkg/cbfs/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewImage(rs io.ReadSeeker) (*Image, error) {
// ReadSeeker on a []byte.
b, err := io.ReadAll(rs)
if err != nil {
return nil, fmt.Errorf("ReadAll: %v", err)
return nil, fmt.Errorf("failed to read: %w", err)
}
in := bytes.NewReader(b)
f, m, err := fmap.Read(in)
Expand All @@ -53,7 +53,7 @@ func NewImage(rs io.ReadSeeker) (*Image, error) {
}
}
if i.Area == nil {
return nil, fmt.Errorf("No CBFS in fmap")
return nil, fmt.Errorf("no CBFS in fmap")
}
r := io.NewSectionReader(in, int64(i.Area.Offset), int64(i.Area.Size))

Expand Down Expand Up @@ -89,7 +89,7 @@ func NewImage(rs io.ReadSeeker) (*Image, error) {
}
Debug("Segment: %v", s)
if err := s.Read(bytes.NewReader(f.FData)); err != nil {
return nil, fmt.Errorf("Reading %#x byte subheader, type %v: %v", len(f.FData), f.Type, err)
return nil, fmt.Errorf("reading %#x byte subheader, type %v: %w", len(f.FData), f.Type, err)
}
Debug("Segment was readable")
i.Segs = append(i.Segs, s)
Expand Down Expand Up @@ -120,15 +120,15 @@ func (i *Image) Update() error {
return err
}
if _, err := b.Write(s.GetFile().Attr); err != nil {
return fmt.Errorf("Writing attr to cbfs record for %v: %v", s, err)
return fmt.Errorf("writing attr to cbfs record for %v: %w", s, err)
}
if err := s.Write(&b); err != nil {
return err
}
// This error should not happen but we need to check just in case.
end := uint32(len(b.Bytes())) + s.GetFile().RecordStart
if end > i.Area.Size {
return fmt.Errorf("Region [%#x, %#x] outside of CBFS [%#x, %#x]", s.GetFile().RecordStart, end, s.GetFile().RecordStart, i.Area.Size)
return fmt.Errorf("region [%#x, %#x] outside of CBFS [%#x, %#x]", s.GetFile().RecordStart, end, s.GetFile().RecordStart, i.Area.Size)
}

Debug("Copy %s %d bytes to i.Data[%d]", s.GetFile().Type.String(), len(b.Bytes()), i.Area.Offset+s.GetFile().RecordStart)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cbfs/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (p *PayloadRecord) Read(in io.ReadSeeker) error {
// Seek to offset (after the header); the remainder is the actual payload.
offset, err := in.Seek(0, io.SeekCurrent)
if err != nil {
return fmt.Errorf("Finding location in stream: %v", err)
return fmt.Errorf("finding location in stream: %w", err)
}
bodySize := int64(p.Size) - offset
Debug("Payload size: %v, body size: %v, offset: %v", p.Size, bodySize, offset)
Expand Down
20 changes: 10 additions & 10 deletions pkg/intel/microcode/microcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,26 @@ func ParseIntelMicrocode(r io.Reader) (*Microcode, error) {
var m Microcode

if err := binary.Read(r, binary.LittleEndian, &m.Header); err != nil {
return nil, fmt.Errorf("Failed to read header: %v", err)
return nil, fmt.Errorf("failed to read header: %w", err)
}

// Sanitychecks
if getTotalSize(m.Header) < getDataSize(m.Header)+uint32(binary.Size(Header{})) {
return nil, fmt.Errorf("Bad data file size")
return nil, fmt.Errorf("bad data file size")
}
if m.HeaderLoaderRevision != 1 || m.HeaderVersion != 1 {
return nil, fmt.Errorf("Invalid version or revision")
return nil, fmt.Errorf("invalid version or revision")
}
if getDataSize(m.Header)%4 > 0 {
return nil, fmt.Errorf("Data size not 32bit aligned")
return nil, fmt.Errorf("data size not 32bit aligned")
}
if getTotalSize(m.Header)%4 > 0 {
return nil, fmt.Errorf("Total size not 32bit aligned")
return nil, fmt.Errorf("total size not 32bit aligned")
}
// Read data
m.Data = make([]byte, getDataSize(m.Header))
if err := binary.Read(r, binary.LittleEndian, &m.Data); err != nil {
return nil, fmt.Errorf("Failed to read data: %v", err)
return nil, fmt.Errorf("failed to read data: %w", err)
}

// Calculcate checksum
Expand All @@ -123,7 +123,7 @@ func ParseIntelMicrocode(r io.Reader) (*Microcode, error) {
checksum += data
}
if checksum != 0 {
return nil, fmt.Errorf("Checksum is not null: %#x", checksum)
return nil, fmt.Errorf("checksum is not null: %#x", checksum)
}

if getTotalSize(m.Header) <= getDataSize(m.Header)+uint32(binary.Size(Header{})) {
Expand All @@ -132,12 +132,12 @@ func ParseIntelMicrocode(r io.Reader) (*Microcode, error) {

// Read extended header
if err := binary.Read(r, binary.LittleEndian, &m.ExtSigTable); err != nil {
return nil, fmt.Errorf("Failed to read extended sig table: %v", err)
return nil, fmt.Errorf("failed to read extended sig table: %w", err)
}
for i := uint32(0); i < m.ExtSigTable.Count; i++ {
var signature ExtendedSignature
if err := binary.Read(r, binary.LittleEndian, &signature); err != nil {
return nil, fmt.Errorf("Failed to read extended signature: %v", err)
return nil, fmt.Errorf("failed to read extended signature: %w", err)
}
m.ExtendedSignatures = append(m.ExtendedSignatures, signature)
}
Expand All @@ -160,7 +160,7 @@ func ParseIntelMicrocode(r io.Reader) (*Microcode, error) {
checksum += data
}
if checksum != 0 {
return nil, fmt.Errorf("Extended header checksum is not null: %#x", checksum)
return nil, fmt.Errorf("extended header checksum is not null: %#x", checksum)
}

return &m, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/uefi/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type ThreeUint8 [3]uint8

func (t *ThreeUint8) UnmarshalJSON(b []byte) error {
if copy(t[:], b) == 0 {
return fmt.Errorf("Cannot unmarshal 3 uint8 from %v\n", b)
return fmt.Errorf("cannot unmarshal 3 uint8 from %v", b)
}
return nil
}
Expand Down

0 comments on commit cbe89b1

Please sign in to comment.