diff --git a/internal/sysenc/buffer.go b/internal/sysenc/buffer.go index 97ac473c8..d184ea196 100644 --- a/internal/sysenc/buffer.go +++ b/internal/sysenc/buffer.go @@ -54,6 +54,11 @@ func (b Buffer) CopyTo(dst []byte) int { return copy(dst, b.unsafeBytes()) } +// AppendTo appends the buffer onto dst. +func (b Buffer) AppendTo(dst []byte) []byte { + return append(dst, b.unsafeBytes()...) +} + // Pointer returns the location where a syscall should write. func (b Buffer) Pointer() sys.Pointer { // NB: This deliberately ignores b.length to support zero-copy diff --git a/marshalers.go b/marshalers.go index 716c7a708..1efa5d425 100644 --- a/marshalers.go +++ b/marshalers.go @@ -70,14 +70,13 @@ func appendPerCPUSlice(buf []byte, slice any, possibleCPUs, elemLength, alignedE return nil, err } - elemBytes.CopyTo(buf[len(buf) : len(buf)+elemLength]) // Only copies elemLength - buf = buf[:len(buf)+alignedElemLength] + buf = elemBytes.AppendTo(buf) + buf = append(buf, make([]byte, alignedElemLength-elemLength)...) } + // Ensure buf is zero-padded full size. - for i := 0; i < possibleCPUs-sliceLen; i++ { - b := make([]byte, alignedElemLength) - buf = append(buf, b...) - } + buf = append(buf, make([]byte, (possibleCPUs-sliceLen)*alignedElemLength)...) + return buf, nil }