Skip to content

Commit 0bdc22d

Browse files
author
Paulo Gomes
authored
Merge pull request #649 from pjbgf/fix-libgit2-panic
libgit2: fix access to nil t.stdin and improve observability
2 parents ae0b38c + 36fcdee commit 0bdc22d

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

pkg/git/libgit2/managed/http.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,13 @@ func (self *httpSmartSubtransportStream) Free() {
292292
// ensure body is fully processed and closed
293293
// for increased likelihood of transport reuse in HTTP/1.x.
294294
// it should not be a problem to do this more than once.
295-
_, _ = io.Copy(io.Discard, self.resp.Body) // errors can be safely ignored
296-
_ = self.resp.Body.Close() // errors can be safely ignored
295+
if _, err := io.Copy(io.Discard, self.resp.Body); err != nil {
296+
traceLog.Error(err, "[http]: cannot discard response body")
297+
}
298+
299+
if err := self.resp.Body.Close(); err != nil {
300+
traceLog.Error(err, "[http]: cannot close response body")
301+
}
297302
}
298303
}
299304
}

pkg/git/libgit2/managed/ssh.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ func (t *sshSmartSubtransport) Action(urlString string, action git2go.SmartServi
137137
if t.lastAction == git2go.SmartServiceActionUploadpackLs {
138138
return t.currentStream, nil
139139
}
140-
t.Close()
140+
if err := t.Close(); err != nil {
141+
traceLog.Error(err, "[ssh]: error cleaning up previous stream")
142+
}
141143
}
142144
cmd = fmt.Sprintf("git-upload-pack '%s'", uPath)
143145

@@ -146,7 +148,9 @@ func (t *sshSmartSubtransport) Action(urlString string, action git2go.SmartServi
146148
if t.lastAction == git2go.SmartServiceActionReceivepackLs {
147149
return t.currentStream, nil
148150
}
149-
t.Close()
151+
if err := t.Close(); err != nil {
152+
traceLog.Error(err, "[ssh]: error cleaning up previous stream")
153+
}
150154
}
151155
cmd = fmt.Sprintf("git-receive-pack '%s'", uPath)
152156

@@ -161,11 +165,11 @@ func (t *sshSmartSubtransport) Action(urlString string, action git2go.SmartServi
161165
defer cred.Free()
162166

163167
var addr string
168+
port := "22"
164169
if u.Port() != "" {
165-
addr = fmt.Sprintf("%s:%s", u.Hostname(), u.Port())
166-
} else {
167-
addr = fmt.Sprintf("%s:22", u.Hostname())
170+
port = u.Port()
168171
}
172+
addr = fmt.Sprintf("%s:%s", u.Hostname(), port)
169173

170174
ckey, sshConfig, err := cacheKeyAndConfig(addr, cred)
171175
if err != nil {
@@ -264,19 +268,21 @@ func (t *sshSmartSubtransport) Close() error {
264268

265269
traceLog.Info("[ssh]: sshSmartSubtransport.Close()")
266270
t.currentStream = nil
267-
if t.client != nil {
271+
if t.client != nil && t.stdin != nil {
268272
if err := t.stdin.Close(); err != nil {
269273
returnErr = fmt.Errorf("cannot close stdin: %w", err)
270274
}
271-
t.client = nil
272275
}
276+
t.client = nil
277+
273278
if t.session != nil {
274279
traceLog.Info("[ssh]: skipping session.wait")
275280
traceLog.Info("[ssh]: session.Close()")
276281
if err := t.session.Close(); err != nil {
277282
returnErr = fmt.Errorf("cannot close session: %w", err)
278283
}
279284
}
285+
t.session = nil
280286

281287
return returnErr
282288
}
@@ -302,6 +308,10 @@ func (stream *sshSmartSubtransportStream) Free() {
302308
}
303309

304310
func cacheKeyAndConfig(remoteAddress string, cred *git2go.Credential) (string, *ssh.ClientConfig, error) {
311+
if cred == nil {
312+
return "", nil, fmt.Errorf("cannot create cache key from a nil credential")
313+
}
314+
305315
username, _, privatekey, passphrase, err := cred.GetSSHKey()
306316
if err != nil {
307317
return "", nil, err

0 commit comments

Comments
 (0)