fix(snapshot): address restore review follow-ups
- commit
- 89277e323607e170fc0234b3006ac13f1f89ba3c
- author
- brice · · 2026-07-06 13:54 UTC
- parent
- f4b9a61
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
3 files · +11 -0
M internal/platform/proc_linux.go +3 -0
| @@ -12,6 +12,9 @@ func ProcessCwd(pid int) (string, error) { | |||
| 12 | 12 | } | |
| 13 | 13 | ||
| 14 | 14 | func processCwd(root string, pid int) (string, error) { | |
| 15 | + | if pid <= 0 { | |
| 16 | + | return "", fmt.Errorf("process cwd: invalid pid %d", pid) | |
| 17 | + | } | |
| 15 | 18 | return os.Readlink(fmt.Sprintf("%s/%d/cwd", root, pid)) | |
| 16 | 19 | } | |
| 17 | 20 | ||
| @@ -12,6 +12,9 @@ func ProcessCwd(pid int) (string, error) { | |||||
| 12 | } | 12 | } | ||
| 13 | 13 | ||||
| 14 | func processCwd(root string, pid int) (string, error) { | 14 | func processCwd(root string, pid int) (string, error) { | ||
| 15 | + | if pid <= 0 { | |||
| 16 | + | return "", fmt.Errorf("process cwd: invalid pid %d", pid) | |||
| 17 | + | } | |||
| 15 | return os.Readlink(fmt.Sprintf("%s/%d/cwd", root, pid)) | 18 | return os.Readlink(fmt.Sprintf("%s/%d/cwd", root, pid)) | ||
| 16 | } | 19 | } | ||
| 17 | 20 | ||||
M internal/platform/process_linux_test.go +6 -0
| @@ -9,6 +9,12 @@ import ( | |||
| 9 | 9 | "testing" | |
| 10 | 10 | ) | |
| 11 | 11 | ||
| 12 | + | func TestProcessCwdRejectsInvalidPID(t *testing.T) { | |
| 13 | + | if _, err := processCwd("/proc", 0); err == nil { | |
| 14 | + | t.Fatal("processCwd accepted invalid pid") | |
| 15 | + | } | |
| 16 | + | } | |
| 17 | + | ||
| 12 | 18 | func TestProcessArgv(t *testing.T) { | |
| 13 | 19 | argv, err := ProcessArgv(os.Getpid()) | |
| 14 | 20 | if err != nil { | |
| @@ -9,6 +9,12 @@ import ( | |||||
| 9 | "testing" | 9 | "testing" | ||
| 10 | ) | 10 | ) | ||
| 11 | 11 | ||||
| 12 | + | func TestProcessCwdRejectsInvalidPID(t *testing.T) { | |||
| 13 | + | if _, err := processCwd("/proc", 0); err == nil { | |||
| 14 | + | t.Fatal("processCwd accepted invalid pid") | |||
| 15 | + | } | |||
| 16 | + | } | |||
| 17 | + | ||||
| 12 | func TestProcessArgv(t *testing.T) { | 18 | func TestProcessArgv(t *testing.T) { | ||
| 13 | argv, err := ProcessArgv(os.Getpid()) | 19 | argv, err := ProcessArgv(os.Getpid()) | ||
| 14 | if err != nil { | 20 | if err != nil { | ||
M internal/usecase/daemon/snapshot_restore_test.go +2 -0
| @@ -205,6 +205,8 @@ func TestRestoreSnapshotsProcessCommandWriteFailureIsNonFatal(t *testing.T) { | |||
| 205 | 205 | d.ApplyConfig(domain.Config{Snapshot: domain.SnapshotConfig{RestoreProcessesSet: true, RestoreProcesses: []string{"pi"}}}) | |
| 206 | 206 | ||
| 207 | 207 | require.NoError(t, d.restoreSession(context.Background(), snapcodec.Session{Name: "agent", Tabs: []snapcodec.Tab{{Cols: 80, Rows: 24, Tree: layout.NewTree("pane-1"), Panes: []snapcodec.Pane{{ID: "pane-1", Cwd: "/tmp", Process: proc}}}}})) | |
| 208 | + | require.Len(t, factory.opens, 1) | |
| 209 | + | require.Equal(t, []string{"pi --resume abc123\n"}, factory.opens[0].pty.writes) | |
| 208 | 210 | } | |
| 209 | 211 | ||
| 210 | 212 | func TestRestoreSnapshotsOpensCollapsedStackPanesWithValidPTYSize(t *testing.T) { | |
| @@ -205,6 +205,8 @@ func TestRestoreSnapshotsProcessCommandWriteFailureIsNonFatal(t *testing.T) { | |||||
| 205 | d.ApplyConfig(domain.Config{Snapshot: domain.SnapshotConfig{RestoreProcessesSet: true, RestoreProcesses: []string{"pi"}}}) | 205 | d.ApplyConfig(domain.Config{Snapshot: domain.SnapshotConfig{RestoreProcessesSet: true, RestoreProcesses: []string{"pi"}}}) | ||
| 206 | 206 | ||||
| 207 | require.NoError(t, d.restoreSession(context.Background(), snapcodec.Session{Name: "agent", Tabs: []snapcodec.Tab{{Cols: 80, Rows: 24, Tree: layout.NewTree("pane-1"), Panes: []snapcodec.Pane{{ID: "pane-1", Cwd: "/tmp", Process: proc}}}}})) | 207 | require.NoError(t, d.restoreSession(context.Background(), snapcodec.Session{Name: "agent", Tabs: []snapcodec.Tab{{Cols: 80, Rows: 24, Tree: layout.NewTree("pane-1"), Panes: []snapcodec.Pane{{ID: "pane-1", Cwd: "/tmp", Process: proc}}}}})) | ||
| 208 | + | require.Len(t, factory.opens, 1) | |||
| 209 | + | require.Equal(t, []string{"pi --resume abc123\n"}, factory.opens[0].pty.writes) | |||
| 208 | } | 210 | } | ||
| 209 | 211 | ||||
| 210 | func TestRestoreSnapshotsOpensCollapsedStackPanesWithValidPTYSize(t *testing.T) { | 212 | func TestRestoreSnapshotsOpensCollapsedStackPanesWithValidPTYSize(t *testing.T) { | ||