Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions internal/sourceanalysis/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -193,8 +192,11 @@ func extractRlibArchive(rlibPath string) (bytes.Buffer, error) {
}
for {
header, err := reader.Next()
if errors.Is(err, io.EOF) {
return bytes.Buffer{}, fmt.Errorf("no object file found in rlib archive '%s'", rlibPath)
}
if err != nil {
log.Fatalf("%v", err)
return bytes.Buffer{}, fmt.Errorf("failed to read rlib archive '%s': %w", rlibPath, err)
}
if header.Name == "//" { // "//" is used in GNU ar format as a store for long file names
fileBuf := bytes.Buffer{}
Expand Down
28 changes: 28 additions & 0 deletions internal/sourceanalysis/rust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sourceanalysis

import (
"bytes"
"fmt"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -103,3 +104,30 @@ func Test_rustBuildSource(t *testing.T) {
}
}
}

// An ar archive with no object file member is not an rlib we can analyse, so it should return
// an error.
func Test_extractRlibArchive_noObjectFile(t *testing.T) {
t.Parallel()

// Built by hand so the test needs no `ar` binary on the machine running it.
var archive bytes.Buffer
archive.WriteString("!<arch>\n")
const content = "hi"
// name[16] mtime[12] uid[6] gid[6] mode[8] size[10] fmag[2]
fmt.Fprintf(&archive, "%-16s%-12d%-6d%-6d%-8s%-10d`\n", "a.txt/", 0, 0, 0, "100644", len(content))
archive.WriteString(content)

path := filepath.Join(t.TempDir(), "not-an-rlib.rlib")
if err := os.WriteFile(path, archive.Bytes(), 0600); err != nil {
t.Fatalf("failed to write test archive: %v", err)
}

_, err := extractRlibArchive(path)
if err == nil {
t.Fatal("expected an error for an ar archive with no object file, got nil")
}
if !strings.Contains(err.Error(), "no object file found") {
t.Errorf("expected a 'no object file found' error, got: %v", err)
}
}