aboutsummaryrefslogtreecommitdiffhomepage
path: root/ldd
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-11-14 21:43:34 +0900
committerOphestra <cat@gensokyo.uk>2025-11-14 21:43:34 +0900
commit45953b3d9c250658767aa6bb673422f8d954b950 (patch)
tree9edebe77558c585c5b8d8c41db7957bf12058b72 /ldd
parent42759e7a9f4e6c45d2e043a404e444160e6cddba (diff)
ldd: cancel on decoder error
This prevents blocking from failures caused by ldd(1) emitting output that is not anticipated by the decoder. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'ldd')
-rw-r--r--ldd/exec.go5
-rw-r--r--ldd/exec_test.go45
2 files changed, 50 insertions, 0 deletions
diff --git a/ldd/exec.go b/ldd/exec.go
index d91cd652..959a3f43 100644
--- a/ldd/exec.go
+++ b/ldd/exec.go
@@ -68,6 +68,11 @@ func Exec(ctx context.Context, msg message.Msg, p string) ([]*Entry, error) {
}
entries, decodeErr := d.Decode()
+ if decodeErr != nil {
+ // do not cancel on successful decode to avoid racing with ldd(1) termination
+ cancel()
+ }
+
if err := z.Wait(); err != nil {
m := stderr.Bytes()
if bytes.Contains(m, []byte(msgStaticSuffix)) || bytes.Contains(m, []byte(msgStaticGlibc)) {
diff --git a/ldd/exec_test.go b/ldd/exec_test.go
new file mode 100644
index 00000000..a6d3a7e5
--- /dev/null
+++ b/ldd/exec_test.go
@@ -0,0 +1,45 @@
+package ldd_test
+
+import (
+ "errors"
+ "os"
+ "os/exec"
+ "testing"
+
+ "hakurei.app/container"
+ "hakurei.app/ldd"
+ "hakurei.app/message"
+)
+
+func TestExec(t *testing.T) {
+ t.Parallel()
+
+ t.Run("failure", func(t *testing.T) {
+ t.Parallel()
+
+ _, err := ldd.Exec(t.Context(), nil, "/proc/nonexistent")
+
+ var exitError *exec.ExitError
+ if !errors.As(err, &exitError) {
+ t.Fatalf("Exec: error has incorrect concrete type: %#v", err)
+ }
+
+ const want = 1
+ if got := exitError.ExitCode(); got != want {
+ t.Fatalf("Exec: ExitCode = %d, want %d", got, want)
+ }
+ })
+
+ t.Run("success", func(t *testing.T) {
+ msg := message.New(nil)
+ msg.GetLogger().SetPrefix("check: ")
+ if entries, err := ldd.Exec(t.Context(), nil, container.MustExecutable(msg)); err != nil {
+ t.Fatalf("Exec: error = %v", err)
+ } else if testing.Verbose() {
+ // result cannot be measured here as build information is not known
+ t.Logf("Exec: %q", entries)
+ }
+ })
+}
+
+func TestMain(m *testing.M) { container.TryArgv0(nil); os.Exit(m.Run()) }