Skip to content

Commit

Permalink
Audited use of append.
Browse files Browse the repository at this point in the history
The ramfs used append incorrectly in one place.
This was fixed by an explicit allocation.

Signed-off-by: David M. Rogers <predictivestatmech@gmail.com>
  • Loading branch information
frobnitzem committed Apr 27, 2023
1 parent 8cd7d7f commit 2d417d9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions cfilesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func (ent cEnt) Walk(ctx context.Context,
// drop part of ent.path
//steps = steps[:len(qids)]
//remain := len(ent.path) - bsp
// Note: potentially dangerous use of append [may modify ent.path]
//next.path = append(ent.path[:remain], steps[bsp:]...)
if len(qids) > 0 {
next.qid = qids[len(qids)-1]
Expand Down
6 changes: 5 additions & 1 deletion cmd/9pr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ func main() {
}

completer := readline.NewPrefixCompleter(
readline.PcItem("ls"),
readline.PcItem("cat"),
readline.PcItem("cd"),
readline.PcItem("exit"),
readline.PcItem("ls"),
readline.PcItem("mkdir"),
readline.PcItem("pwd"),
readline.PcItem("rm"),
Expand Down Expand Up @@ -122,6 +123,9 @@ func main() {
var cmd func(ctx context.Context, args ...string) error

switch name {
case "exit":
csession.Stop(nil)
return
case "ls":
cmd = commander.cmdls
case "cd":
Expand Down
23 changes: 18 additions & 5 deletions ramfs/dirent.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (h FileHandle) OpenDir(ctx context.Context) (p9p.ReadNext, error) {

func (h FileHandle) Clunk(ctx context.Context) error {
h.ent.decref()
for i := range(h.parents) {
for i := range h.parents {
h.parents[len(h.parents)-i-1].decref()
}
return nil
Expand Down Expand Up @@ -163,12 +163,25 @@ func (h FileHandle) Walk(ctx context.Context, names ...string) ([]p9p.Qid, p9p.D
// ref = /
// rh.ent = /
// rh.parents = []
rh.parents = append(append(
rh.parents = make([]*FileEnt, len(h.parents)-ndel + 1 + len(ans)-ndel)

// would use append, but append mutates its input
/*rh.parents = append(append(
h.parents[:len(h.parents)-ndel],
ref),
ans[ndel:]...)
for _, p := range(rh.parents) {
ans[ndel:]...)*/
i0 := len(h.parents)-ndel + 1
for i := range rh.parents {
var p *FileEnt
if i < len(h.parents)-ndel {
p = h.parents[i]
} else if i >= i0 {
p = ans[ndel+i-i0]
} else {
p = ref
}
p.incref()
rh.parents[i] = p
}
rh.ent = rh.parents[len(rh.parents)-1]
rh.parents = rh.parents[:len(rh.parents)-1]
Expand All @@ -177,7 +190,7 @@ func (h FileHandle) Walk(ctx context.Context, names ...string) ([]p9p.Qid, p9p.D
}

qids = make([]p9p.Qid, len(ans))
for i, a := range(ans) {
for i, a := range ans {
qids[i] = a.Info.Qid
}

Expand Down

0 comments on commit 2d417d9

Please sign in to comment.