diff --git a/src/errors/join.go b/src/errors/join.go index 1c486d591e3..349fc06ed9f 100644 --- a/src/errors/join.go +++ b/src/errors/join.go @@ -4,6 +4,10 @@ package errors +import ( + "unsafe" +) + // Join returns an error that wraps the given errors. // Any nil error values are discarded. // Join returns nil if every value in errs is nil. @@ -38,14 +42,19 @@ type joinError struct { } func (e *joinError) Error() string { - var b []byte - for i, err := range e.errs { - if i > 0 { - b = append(b, '\n') - } + // Since Join returns nil if every value in errs is nil, + // e.errs cannot be empty. + if len(e.errs) == 1 { + return e.errs[0].Error() + } + + b := []byte(e.errs[0].Error()) + for _, err := range e.errs[1:] { + b = append(b, '\n') b = append(b, err.Error()...) } - return string(b) + // At this point, b has at least one byte '\n'. + return unsafe.String(&b[0], len(b)) } func (e *joinError) Unwrap() []error {