From 69a2e01edd818a76949f4f499e14feac896aad8f Mon Sep 17 00:00:00 2001 From: Avi Drissman Date: Tue, 12 Dec 2023 17:44:37 +0000 Subject: Add limitations to bit_cast Using bit_cast on pointers does not accomplish anything useful, and obscures what might be incorrect behavior. Do not allow base::bit_cast to be misused in that way. Bug: 1506769 Change-Id: Ib9f3acf954c5537c7520f72827854de81afda382 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5094596 Reviewed-by: Hongchan Choi Reviewed-by: Ilya Sherman Reviewed-by: Will Harris Reviewed-by: danakj Reviewed-by: Kentaro Hara Reviewed-by: Dominic Battre Commit-Queue: Avi Drissman Reviewed-by: Adam Rice Reviewed-by: Robert Sesek Code-Coverage: findit-for-me@appspot.gserviceaccount.com Cr-Commit-Position: refs/heads/main@{#1236404} NOKEYCHECK=True GitOrigin-RevId: 70cb7f749d60410db4397c0fc3b35d189ad591ab --- google/compression_utils.cc | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/google/compression_utils.cc b/google/compression_utils.cc index 279ea07..c2b17e4 100644 --- a/google/compression_utils.cc +++ b/google/compression_utils.cc @@ -4,7 +4,6 @@ #include "third_party/zlib/google/compression_utils.h" -#include "base/bit_cast.h" #include "base/check_op.h" #include "base/process/memory.h" #include "base/sys_byteorder.h" @@ -24,8 +23,8 @@ bool GzipCompress(base::span input, // uLongf can be larger than size_t. uLongf compressed_size_long = static_cast(output_buffer_size); if (zlib_internal::GzipCompressHelper( - base::bit_cast(output_buffer), &compressed_size_long, - base::bit_cast(input.data()), + reinterpret_cast(output_buffer), &compressed_size_long, + reinterpret_cast(input.data()), static_cast(input.size()), malloc_fn, free_fn) != Z_OK) { return false; } @@ -55,7 +54,7 @@ bool GzipCompress(base::span input, std::string* output) { if (zlib_internal::GzipCompressHelper( compressed_data, &compressed_data_size, - base::bit_cast(input.data()), input_size, nullptr, + reinterpret_cast(input.data()), input_size, nullptr, nullptr) != Z_OK) { free(compressed_data); return false; @@ -82,8 +81,8 @@ bool GzipUncompress(const std::string& input, std::string* output) { uncompressed_output.resize(uncompressed_size); if (zlib_internal::GzipUncompressHelper( - base::bit_cast(uncompressed_output.data()), - &uncompressed_size, base::bit_cast(input.data()), + reinterpret_cast(uncompressed_output.data()), + &uncompressed_size, reinterpret_cast(input.data()), static_cast(input.length())) == Z_OK) { output->swap(uncompressed_output); return true; @@ -102,8 +101,8 @@ bool GzipUncompress(base::span input, if (uncompressed_size > output.size()) return false; return zlib_internal::GzipUncompressHelper( - base::bit_cast(output.data()), &uncompressed_size, - base::bit_cast(input.data()), + reinterpret_cast(const_cast(output.data())), + &uncompressed_size, reinterpret_cast(input.data()), static_cast(input.size())) == Z_OK; } @@ -117,8 +116,8 @@ bool GzipUncompress(base::span input, std::string* output) { uLongf uncompressed_size = GetUncompressedSize(input); output->resize(uncompressed_size); return zlib_internal::GzipUncompressHelper( - base::bit_cast(output->data()), &uncompressed_size, - base::bit_cast(input.data()), + reinterpret_cast(output->data()), &uncompressed_size, + reinterpret_cast(input.data()), static_cast(input.size())) == Z_OK; } @@ -128,7 +127,8 @@ uint32_t GetUncompressedSize(base::span compressed_data) { uint32_t GetUncompressedSize(base::span compressed_data) { return zlib_internal::GetGzipUncompressedSize( - base::bit_cast(compressed_data.data()), compressed_data.size()); + reinterpret_cast(compressed_data.data()), + compressed_data.size()); } } // namespace compression -- cgit v1.2.3