Skip to content

Commit c926038

Browse files
authoredJan 27, 2025
fix: dont include xattr in extended link reading (#278)
* fix: dont include xattr in extended link reading * chore: add test for parse extended sym link * fix: update test to work and check extra
1 parent 05f792d commit c926038

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed
 

‎filesystem/squashfs/inode.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,12 @@ func parseExtendedSymlink(b []byte) (*extendedSymlink, int, error) {
639639
s := &extendedSymlink{
640640
links: binary.LittleEndian.Uint32(b[0:4]),
641641
}
642+
targetSize := int(binary.LittleEndian.Uint32(b[4:8]))
642643
// account for the synlink target, plus 4 bytes for the xattr index after it
643-
extra = int(binary.LittleEndian.Uint32(b[4:8])) + 4
644-
if len(b[target:]) > extra {
645-
s.target = string(b[8 : 8+extra])
646-
s.xAttrIndex = binary.LittleEndian.Uint32(b[8+extra : 8+extra+4])
644+
extra = targetSize + 4
645+
if len(b) >= extra+target {
646+
s.target = string(b[target : target+targetSize])
647+
s.xAttrIndex = binary.LittleEndian.Uint32(b[target+targetSize : target+targetSize+4])
647648
extra = 0
648649
}
649650
return s, extra, nil

‎filesystem/squashfs/inode_internal_test.go

+57-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package squashfs
22

33
import (
44
"bytes"
5+
"encoding/binary"
56
"fmt"
67
"strings"
78
"testing"
@@ -383,13 +384,64 @@ func TestBasicSymlink(t *testing.T) {
383384
})
384385
}
385386

386-
//nolint:unused,revive // keep for future when we implement it and will need t
387387
func TestExtendedSymlink(t *testing.T) {
388-
// when we have more data with which to work
388+
s := &extendedSymlink{
389+
links: 1,
390+
target: "/a/b/c/d/ef/g/h",
391+
xAttrIndex: 46,
392+
}
393+
b, err := testGetInodeMetabytes()
394+
if err != nil {
395+
t.Fatal(err)
396+
}
397+
398+
inodeB := binary.LittleEndian.AppendUint32(b[testBasicSymlinkStart:testBasicSymlinkEnd], s.xAttrIndex)
399+
400+
t.Run("toBytes", func(t *testing.T) {
401+
b := s.toBytes()
402+
if !bytes.Equal(b, inodeB) {
403+
t.Errorf("mismatched output, actual then expected")
404+
t.Logf("% x", b)
405+
t.Logf("% x", inodeB)
406+
}
407+
})
408+
t.Run("Size", func(t *testing.T) {
409+
size := s.size()
410+
if size != 0 {
411+
t.Errorf("mismatched sizes, actual %d expected %d", size, 0)
412+
}
413+
})
414+
415+
tests := []struct {
416+
b []byte
417+
sym *extendedSymlink
418+
ext int
419+
err error
420+
}{
421+
{inodeB, s, 0, nil},
422+
{inodeB[:7], nil, 0, fmt.Errorf("received %d bytes instead of expected minimal %d", 7, 8)},
423+
{inodeB[:20], &extendedSymlink{links: 1}, 19, nil},
424+
}
389425

390-
// func (i extendedSymlink) toBytes() []byte {
391-
// func (i extendedSymlink) size() int64 {
392-
// func parseExtendedSymlink(b []byte) (*extendedSymlink, error) {
426+
t.Run("parse", func(t *testing.T) {
427+
for i, tt := range tests {
428+
sym, ext, err := parseExtendedSymlink(tt.b)
429+
switch {
430+
case (err == nil && tt.err != nil) || (err != nil && tt.err == nil) || (err != nil && tt.err != nil && !strings.HasPrefix(err.Error(), tt.err.Error())):
431+
t.Errorf("%d: mismatched error, actual then expected", i)
432+
t.Logf("%v", err)
433+
t.Logf("%v", tt.err)
434+
case tt.ext != ext:
435+
t.Errorf("%d: mismatched extra, actual then expected", i)
436+
t.Logf("%v", ext)
437+
t.Logf("%v", tt.ext)
438+
case (sym == nil && tt.sym != nil) || (sym != nil && tt.sym == nil) || (sym != nil && tt.sym != nil && *sym != *tt.sym):
439+
t.Errorf("%d: mismatched results, actual then expected", i)
440+
t.Logf("%#v", *sym)
441+
t.Logf("%#v", *tt.sym)
442+
}
443+
}
444+
})
393445
}
394446

395447
//nolint:unused,revive // keep for future when we implement it and will need t

‎filesystem/squashfs/squashfs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ func (fs *FileSystem) hydrateDirectoryEntries(entries []*directoryEntryRaw) ([]*
532532
body, header := in.getBody(), in.getHeader()
533533
xattrIndex, has := body.xattrIndex()
534534
xattrs := map[string]string{}
535-
if has && xattrIndex != noXattrInodeFlag {
535+
if has {
536536
xattrs, err = fs.xattrs.find(int(xattrIndex))
537537
if err != nil {
538538
return nil, fmt.Errorf("error reading xattrs for %s: %v", e.name, err)

0 commit comments

Comments
 (0)