From dbbdf55493fbd4083690b62aaf0aa39aa7d2500f Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 9 Oct 2024 12:18:19 -0500 Subject: [PATCH] fix: -Wunsafe-buffer-usage warning in WriteAsciiChunk() (#44134) * fix: -Wunsafe-buffer-usage warning in WriteAsciiChunk() * chore: add // SAFETY comment to explain UNSAFE_BUFFERS() use --- shell/common/heap_snapshot.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/shell/common/heap_snapshot.cc b/shell/common/heap_snapshot.cc index 0a13849329f2..bd509d6dde69 100644 --- a/shell/common/heap_snapshot.cc +++ b/shell/common/heap_snapshot.cc @@ -4,6 +4,7 @@ #include "shell/common/heap_snapshot.h" +#include "base/containers/span.h" #include "base/files/file.h" #include "base/memory/raw_ptr.h" #include "v8/include/v8-profiler.h" @@ -24,8 +25,13 @@ class HeapSnapshotOutputStream : public v8::OutputStream { void EndOfStream() override { is_complete_ = true; } v8::OutputStream::WriteResult WriteAsciiChunk(char* data, int size) override { - auto bytes_written = file_->WriteAtCurrentPos(data, size); - return bytes_written == size ? kContinue : kAbort; + const uint8_t* udata = reinterpret_cast(data); + const size_t usize = static_cast(std::max(0, size)); + // SAFETY: since WriteAsciiChunk() only gives us data + size, our + // UNSAFE_BUFFERS macro call is unavoidable here. It can be removed + // if/when v8 changes WriteAsciiChunk() to pass a v8::MemorySpan. + const auto data_span = UNSAFE_BUFFERS(base::span(udata, usize)); + return file_->WriteAtCurrentPosAndCheck(data_span) ? kContinue : kAbort; } private: