Skip to content

Commit 6cc4f0b

Browse files
committed
issue: fix nil pointer dereference on plumb error
plumbserve() fails with a nil pointer dereference when it tries to print errors via w.Err(). This happens because w is a dummy window with a nil *acme.Win. This commit replaces w.Err() calls in plumbserve() with acme.Errf(). It also extracts the plumbserve() method out of *awin, given that the previous change removes this dependency. Fixes issue #5.
1 parent 7a65b24 commit 6cc4f0b

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

issue/acme.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"fmt"
1515
"log"
1616
"os"
17+
"path"
1718
"regexp"
1819
"strconv"
1920
"strings"
@@ -25,9 +26,11 @@ import (
2526
"github.com/google/go-github/github"
2627
)
2728

29+
const root = "/issue/"
30+
2831
func (w *awin) project() string {
2932
p := w.prefix
30-
p = strings.TrimPrefix(p, "/issue/")
33+
p = strings.TrimPrefix(p, root)
3134
i := strings.Index(p, "/")
3235
if i >= 0 {
3336
j := strings.Index(p[i+1:], "/")
@@ -40,7 +43,7 @@ func (w *awin) project() string {
4043

4144
func acmeMode() {
4245
var dummy awin
43-
dummy.prefix = "/issue/" + *project + "/"
46+
dummy.prefix = path.Join(root, *project) + "/"
4447
if flag.NArg() > 0 {
4548
// TODO(rsc): Without -a flag, the query is conatenated into one query.
4649
// Decide which behavior should be used, and use it consistently.
@@ -59,59 +62,59 @@ func acmeMode() {
5962
dummy.Look("all")
6063
}
6164

62-
go dummy.plumbserve()
65+
go plumbserve()
6366

6467
select {}
6568
}
6669

67-
func (w *awin) plumbserve() {
70+
func plumbserve() {
6871
fid, err := plumb.Open("githubissue", 0)
6972
if err != nil {
70-
w.Err(fmt.Sprintf("plumb: %v", err))
73+
acme.Errf(root, "plumb: %v", err)
7174
return
7275
}
7376
r := bufio.NewReader(fid)
7477
for {
7578
var m plumb.Message
7679
if err := m.Recv(r); err != nil {
77-
w.Err(fmt.Sprintf("plumb recv: %v", err))
80+
acme.Errf(root, "plumb recv: %v", err)
7881
return
7982
}
8083
if m.Type != "text" {
81-
w.Err(fmt.Sprintf("plumb recv: unexpected type: %s\n", m.Type))
84+
acme.Errf(root, "plumb recv: unexpected type: %s", m.Type)
8285
continue
8386
}
8487
if m.Dst != "githubissue" {
85-
w.Err(fmt.Sprintf("plumb recv: unexpected dst: %s\n", m.Dst))
88+
acme.Errf(root, "plumb recv: unexpected dst: %s", m.Dst)
8689
continue
8790
}
8891
// TODO use m.Dir
8992
data := string(m.Data)
9093
var project, what string
91-
if strings.HasPrefix(data, "/issue/") {
92-
project = data[len("/issue/"):]
94+
if strings.HasPrefix(data, root) {
95+
project = data[len(root):]
9396
i := strings.LastIndex(project, "/")
9497
if i < 0 {
95-
w.Err(fmt.Sprintf("plumb recv: bad text %q", data))
98+
acme.Errf(root, "plumb recv: bad text %q", data)
9699
continue
97100
}
98101
project, what = project[:i], project[i+1:]
99102
} else {
100103
i := strings.Index(data, "#")
101104
if i < 0 {
102-
w.Err(fmt.Sprintf("plumb recv: bad text %q", data))
105+
acme.Errf(root, "plumb recv: bad text %q", data)
103106
continue
104107
}
105108
project, what = data[:i], data[i+1:]
106109
}
107110
if strings.Count(project, "/") != 1 {
108-
w.Err(fmt.Sprintf("plumb recv: bad text %q", data))
111+
acme.Errf(root, "plumb recv: bad text %q", data)
109112
continue
110113
}
111114
var plummy awin
112-
plummy.prefix = "/issue/" + project + "/"
115+
plummy.prefix = path.Join(root, project) + "/"
113116
if !plummy.Look(what) {
114-
w.Err(fmt.Sprintf("plumb recv: can't look %s%s", plummy.prefix, what))
117+
acme.Errf(root, "plumb recv: can't look %s%s", plummy.prefix, what)
115118
}
116119
}
117120
}
@@ -251,7 +254,7 @@ func (w *awin) Look(text string) bool {
251254
if m := repoHashRE.FindStringSubmatch(text); m != nil {
252255
project := m[1]
253256
what := m[2]
254-
prefix := "/issue/" + project + "/"
257+
prefix := path.Join(root, project) + "/"
255258
if acme.Show(prefix+what) != nil {
256259
return true
257260
}

0 commit comments

Comments
 (0)