From c33f1fb03ec563caed708bfd8ae85e0282542936 Mon Sep 17 00:00:00 2001 From: Wiktor Garbacz Date: Fri, 18 Sep 2020 07:22:51 -0700 Subject: [PATCH] Simplify casts Drive-by: check for malloc failure in sapi::v::Array PiperOrigin-RevId: 332446225 Change-Id: I375ea94845e04dffc3353d70737402daa66ae50a --- sandboxed_api/client.cc | 6 +++--- sandboxed_api/var_array.h | 25 ++++++++++++------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/sandboxed_api/client.cc b/sandboxed_api/client.cc index df2b24d..738fc87 100644 --- a/sandboxed_api/client.cc +++ b/sandboxed_api/client.cc @@ -254,8 +254,8 @@ void HandleReallocMsg(uintptr_t ptr, uintptr_t size, FuncRet* ret) { __sanitizer_get_allocated_size(reinterpret_cast(ptr)); #endif ret->ret_type = v::Type::kPointer; - ret->int_val = reinterpret_cast( - realloc(const_cast(reinterpret_cast(ptr)), size)); + ret->int_val = + reinterpret_cast(realloc(reinterpret_cast(ptr), size)); ret->success = true; #ifdef MEMORY_SANITIZER // Memory is copied to the pointer using an API that the memory sanitizer @@ -273,7 +273,7 @@ void HandleReallocMsg(uintptr_t ptr, uintptr_t size, FuncRet* ret) { void HandleFreeMsg(uintptr_t ptr, FuncRet* ret) { VLOG(1) << "HandleFreeMsg: free(0x" << absl::StrCat(absl::Hex(ptr)) << ")"; - free(const_cast(reinterpret_cast(ptr))); + free(reinterpret_cast(ptr)); ret->ret_type = v::Type::kVoid; ret->success = true; ret->int_val = 0ULL; diff --git a/sandboxed_api/var_array.h b/sandboxed_api/var_array.h index 60968ba..fcf2da9 100644 --- a/sandboxed_api/var_array.h +++ b/sandboxed_api/var_array.h @@ -15,9 +15,11 @@ #ifndef SANDBOXED_API_VAR_ARRAY_H_ #define SANDBOXED_API_VAR_ARRAY_H_ +#include #include #include +#include #include "absl/base/macros.h" #include "absl/status/status.h" #include "absl/strings/str_cat.h" @@ -39,19 +41,19 @@ class Array : public Var, public Pointable { nelem_(nelem), total_size_(nelem_ * sizeof(T)), buffer_owned_(false) { - SetLocal(const_cast(reinterpret_cast(arr_))); + SetLocal(const_cast*>(arr_)); } // The array is allocated and owned by this object. explicit Array(size_t nelem) - : arr_(static_cast(malloc(sizeof(T) * nelem))), - nelem_(nelem), - total_size_(nelem_ * sizeof(T)), - buffer_owned_(true) { - SetLocal(const_cast(reinterpret_cast(arr_))); + : nelem_(nelem), total_size_(nelem_ * sizeof(T)), buffer_owned_(true) { + void* storage = malloc(sizeof(T) * nelem); + CHECK(storage != nullptr); + SetLocal(storage); + arr_ = static_cast(storage); } virtual ~Array() { if (buffer_owned_) { - free(const_cast(reinterpret_cast(arr_))); + free(const_cast*>(arr_)); } } @@ -121,7 +123,7 @@ class Array : public Var, public Pointable { arr_ = static_cast(new_addr); total_size_ = size; nelem_ = size / sizeof(T); - SetLocal(arr_); + SetLocal(new_addr); return absl::OkStatus(); } @@ -142,7 +144,7 @@ class Array : public Var, public Pointable { class CStr : public Array { public: explicit CStr(char* cstr) : Array(strlen(cstr) + 1) { - strcpy(this->GetData(), cstr); // NOLINT + std::copy(cstr, cstr + GetNElem(), GetData()); } std::string ToString() const final { @@ -159,10 +161,7 @@ class ConstCStr : public Array { : Array(cstr, strlen(cstr) + 1) {} std::string ToString() const final { - if (GetData() == nullptr) { - return "CStr: [nullptr]"; - } - return absl::StrCat("CStr: len(w/o NUL):", strlen(GetData()), ", ['", + return absl::StrCat("ConstCStr: len(w/o NUL):", strlen(GetData()), ", ['", GetData(), "']"); } };