From b5cbafdbd5017d59f959f49db9b6cf92f70ac50d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Fontenele?= Date: Fri, 17 Jun 2022 14:04:26 +0200 Subject: [PATCH] handler: Avoid calling WriteChunk if no data is available (#746) * fix: avoiding `writeChunk` no-op * fix: formatting * fix: added return statement * fix: preserving side effects * refactor: smaller change --- pkg/handler/unrouted_handler.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/handler/unrouted_handler.go b/pkg/handler/unrouted_handler.go index 690270e..391b14a 100644 --- a/pkg/handler/unrouted_handler.go +++ b/pkg/handler/unrouted_handler.go @@ -576,6 +576,7 @@ func (handler *UnroutedHandler) PatchFile(w http.ResponseWriter, r *http.Request info.Size = uploadLength info.SizeIsDeferred = false + } if err := handler.writeChunk(ctx, upload, info, w, r); err != nil { @@ -623,7 +624,7 @@ func (handler *UnroutedHandler) writeChunk(ctx context.Context, upload Upload, i var err error // Prevent a nil pointer dereference when accessing the body which may not be // available in the case of a malicious request. - if r.Body != nil { + if r.Body != nil && maxSize > 0 { // Limit the data read from the request's body to the allowed maximum reader := newBodyReader(io.LimitReader(r.Body, maxSize))