Browse Source

refactor: use `base::as_bytes()` in `WriteAsciiChunk()` (#45747)

refactor: use base::as_bytes() in WriteAsciiChunk()

this avoids a reinterpret_cast and a static_cast

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <[email protected]>
trop[bot] 1 month ago
parent
commit
65d1bf2d44
1 changed files with 3 additions and 3 deletions
  1. 3 3
      shell/common/heap_snapshot.cc

+ 3 - 3
shell/common/heap_snapshot.cc

@@ -7,6 +7,7 @@
 #include "base/containers/span.h"
 #include "base/files/file.h"
 #include "base/memory/raw_ptr.h"
+#include "base/numerics/safe_conversions.h"
 #include "v8/include/v8-profiler.h"
 #include "v8/include/v8.h"
 
@@ -25,12 +26,11 @@ class HeapSnapshotOutputStream : public v8::OutputStream {
   void EndOfStream() override { is_complete_ = true; }
 
   v8::OutputStream::WriteResult WriteAsciiChunk(char* data, int size) override {
-    const uint8_t* udata = reinterpret_cast<const uint8_t*>(data);
-    const size_t usize = static_cast<size_t>(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));
+    const auto data_span = base::as_bytes(
+        UNSAFE_BUFFERS(base::span{data, base::saturated_cast<size_t>(size)}));
     return file_->WriteAtCurrentPosAndCheck(data_span) ? kContinue : kAbort;
   }