@samitouri / QOSAMI-WSL / commits / df6113d7

Write explicit null terminator in CopyToSpan (#40413)

CopyToSpan copies String.size() bytes and advances Offset by String.size()+1 (accounting for a null terminator), but never actually writes the null byte. This relies on callers to have pre-zeroed the destination buffer. Add an explicit null terminator write to make the function self-contained and safe regardless of buffer initialization. Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Ben Hillis committed May 4, 2026 at 18:14 UTC df6113d700328c23e1283c4d7f5df9f2be935f01
1 file changed +1
src/shared/inc/stringshared.h
+1
@@ -47,6 +47,7 @@ using MacAddress = std::array<std::uint8_t, 6>;
47 inline unsigned int CopyToSpan(const std::string_view String, const gsl::span<gsl::byte> Span, size_t& Offset)
48 {
49 gsl::copy(as_bytes(gsl::make_span(String.data(), String.size())), Span.subspan(Offset));
50 + Span[Offset + String.size()] = gsl::byte{0};
51 const auto PreviousOffset = gsl::narrow_cast<unsigned int>(Offset);
52 Offset += String.size() + 1;
53 return PreviousOffset;