Skip to content

Commit 9c64a7c

Browse files
authored
Logging middleware: fix invalid memory address panic if response is nil (#471)
panic: runtime error: invalid memory address or nil pointer dereference [recovered, repanicked] [signal SIGSEGV: segmentation violation code=0x2 addr=0x38 pc=0x102840ad4]
1 parent b5e3d66 commit 9c64a7c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

githubapp/middleware_logging.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ func ClientLogging(lvl zerolog.Level, opts ...ClientLoggingOption) ClientMiddlew
8686
} else {
8787
evt.Int64("size", size)
8888
}
89+
90+
addRateLimitInformationToLog(options.LogRateLimitInformation, evt, res)
8991
} else {
9092
evt.Bool("cached", false).
9193
Int("status", -1).
9294
Int64("size", -1)
9395
}
9496

95-
addRateLimitInformationToLog(options.LogRateLimitInformation, evt, res)
9697
evt.Msg("github_request")
9798
return res, err
9899
})

githubapp/middleware_logging_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ func TestClientLogging(t *testing.T) {
123123
"response_body": missingField,
124124
})
125125
})
126+
127+
t.Run("nilRequest", func(t *testing.T) {
128+
req, _ := newLoggingRequest("GET", "https://test.domain/path", []byte("The request"))
129+
rt := newEmptyRoundTripper()
130+
131+
logMiddleware := ClientLogging(zerolog.InfoLevel, LogRateLimitInformation(&RateLimitLoggingOption{
132+
Limit: true,
133+
Remaining: true,
134+
Used: true,
135+
Reset: true,
136+
Resource: true,
137+
}))
138+
rt = logMiddleware(rt)
139+
140+
_, err := rt.RoundTrip(req)
141+
if err != nil {
142+
t.Fatalf("unexpected error making request: %v", err)
143+
}
144+
145+
// To ensure that this is not crashing
146+
})
126147
}
127148

128149
func newLoggingRequest(method, url string, body []byte) (*http.Request, *bytes.Buffer) {
@@ -158,6 +179,12 @@ func newStaticRoundTripper(status int, body []byte) http.RoundTripper {
158179
})
159180
}
160181

182+
func newEmptyRoundTripper() http.RoundTripper {
183+
return roundTripperFunc(func(r *http.Request) (*http.Response, error) {
184+
return nil, nil
185+
})
186+
}
187+
161188
var missingField struct{}
162189

163190
func assertLogFields(t *testing.T, out []byte, expected map[string]interface{}) {

0 commit comments

Comments
 (0)