From cbd63c6a1c57deeb28bfcd6dc655a665053c600f Mon Sep 17 00:00:00 2001 From: zekin Date: Fri, 10 May 2024 16:02:48 +0800 Subject: [PATCH] fix: resp set trailer will panic (#1102) Co-authored-by: yinxuran.lucky --- pkg/protocol/header.go | 2 ++ pkg/protocol/header_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/pkg/protocol/header.go b/pkg/protocol/header.go index 278b137b5..88885483c 100644 --- a/pkg/protocol/header.go +++ b/pkg/protocol/header.go @@ -1766,6 +1766,8 @@ func (h *ResponseHeader) setSpecialHeader(key, value []byte) bool { // Transfer-Encoding is managed automatically. return true } else if utils.CaseInsensitiveCompare(bytestr.StrTrailer, key) { + // copy value to avoid panic + value = append(h.bufKV.value[:0], value...) h.Trailer().SetTrailers(value) return true } diff --git a/pkg/protocol/header_test.go b/pkg/protocol/header_test.go index a14704053..ba2bd6998 100644 --- a/pkg/protocol/header_test.go +++ b/pkg/protocol/header_test.go @@ -806,3 +806,13 @@ func TestResponseHeaderDateEmpty(t *testing.T) { t.Fatalf("ResponseDateNoDefaultNotEmpty fail, response: \n%+v\noutcome: \n%q\n", h, headers) //nolint:govet } } + +func TestSetTrailerWithROString(t *testing.T) { + h := &RequestHeader{} + h.Add(consts.HeaderTrailer, "foo,bar,hertz") + assert.DeepEqual(t, "Foo, Bar, Hertz", h.Get(consts.HeaderTrailer)) + + h1 := &ResponseHeader{} + h1.Add(consts.HeaderTrailer, "foo,bar,hertz") + assert.DeepEqual(t, "Foo, Bar, Hertz", h1.Get(consts.HeaderTrailer)) +}