Simplify casts

Drive-by: check for malloc failure in sapi:✌️:Array
PiperOrigin-RevId: 332446225
Change-Id: I375ea94845e04dffc3353d70737402daa66ae50a
This commit is contained in:
Wiktor Garbacz 2020-09-18 07:22:51 -07:00 committed by Copybara-Service
parent 08a956a415
commit c33f1fb03e
2 changed files with 15 additions and 16 deletions

View File

@ -254,8 +254,8 @@ void HandleReallocMsg(uintptr_t ptr, uintptr_t size, FuncRet* ret) {
__sanitizer_get_allocated_size(reinterpret_cast<const void*>(ptr)); __sanitizer_get_allocated_size(reinterpret_cast<const void*>(ptr));
#endif #endif
ret->ret_type = v::Type::kPointer; ret->ret_type = v::Type::kPointer;
ret->int_val = reinterpret_cast<uintptr_t>( ret->int_val =
realloc(const_cast<void*>(reinterpret_cast<const void*>(ptr)), size)); reinterpret_cast<uintptr_t>(realloc(reinterpret_cast<void*>(ptr), size));
ret->success = true; ret->success = true;
#ifdef MEMORY_SANITIZER #ifdef MEMORY_SANITIZER
// Memory is copied to the pointer using an API that the 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) { void HandleFreeMsg(uintptr_t ptr, FuncRet* ret) {
VLOG(1) << "HandleFreeMsg: free(0x" << absl::StrCat(absl::Hex(ptr)) << ")"; VLOG(1) << "HandleFreeMsg: free(0x" << absl::StrCat(absl::Hex(ptr)) << ")";
free(const_cast<void*>(reinterpret_cast<const void*>(ptr))); free(reinterpret_cast<void*>(ptr));
ret->ret_type = v::Type::kVoid; ret->ret_type = v::Type::kVoid;
ret->success = true; ret->success = true;
ret->int_val = 0ULL; ret->int_val = 0ULL;

View File

@ -15,9 +15,11 @@
#ifndef SANDBOXED_API_VAR_ARRAY_H_ #ifndef SANDBOXED_API_VAR_ARRAY_H_
#define SANDBOXED_API_VAR_ARRAY_H_ #define SANDBOXED_API_VAR_ARRAY_H_
#include <algorithm>
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include <glog/logging.h>
#include "absl/base/macros.h" #include "absl/base/macros.h"
#include "absl/status/status.h" #include "absl/status/status.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
@ -39,19 +41,19 @@ class Array : public Var, public Pointable {
nelem_(nelem), nelem_(nelem),
total_size_(nelem_ * sizeof(T)), total_size_(nelem_ * sizeof(T)),
buffer_owned_(false) { buffer_owned_(false) {
SetLocal(const_cast<void*>(reinterpret_cast<const void*>(arr_))); SetLocal(const_cast<std::remove_const_t<T>*>(arr_));
} }
// The array is allocated and owned by this object. // The array is allocated and owned by this object.
explicit Array(size_t nelem) explicit Array(size_t nelem)
: arr_(static_cast<T*>(malloc(sizeof(T) * nelem))), : nelem_(nelem), total_size_(nelem_ * sizeof(T)), buffer_owned_(true) {
nelem_(nelem), void* storage = malloc(sizeof(T) * nelem);
total_size_(nelem_ * sizeof(T)), CHECK(storage != nullptr);
buffer_owned_(true) { SetLocal(storage);
SetLocal(const_cast<void*>(reinterpret_cast<const void*>(arr_))); arr_ = static_cast<T*>(storage);
} }
virtual ~Array() { virtual ~Array() {
if (buffer_owned_) { if (buffer_owned_) {
free(const_cast<void*>(reinterpret_cast<const void*>(arr_))); free(const_cast<std::remove_const_t<T>*>(arr_));
} }
} }
@ -121,7 +123,7 @@ class Array : public Var, public Pointable {
arr_ = static_cast<T*>(new_addr); arr_ = static_cast<T*>(new_addr);
total_size_ = size; total_size_ = size;
nelem_ = size / sizeof(T); nelem_ = size / sizeof(T);
SetLocal(arr_); SetLocal(new_addr);
return absl::OkStatus(); return absl::OkStatus();
} }
@ -142,7 +144,7 @@ class Array : public Var, public Pointable {
class CStr : public Array<char> { class CStr : public Array<char> {
public: public:
explicit CStr(char* cstr) : Array<char>(strlen(cstr) + 1) { explicit CStr(char* cstr) : Array<char>(strlen(cstr) + 1) {
strcpy(this->GetData(), cstr); // NOLINT std::copy(cstr, cstr + GetNElem(), GetData());
} }
std::string ToString() const final { std::string ToString() const final {
@ -159,10 +161,7 @@ class ConstCStr : public Array<const char> {
: Array<const char>(cstr, strlen(cstr) + 1) {} : Array<const char>(cstr, strlen(cstr) + 1) {}
std::string ToString() const final { std::string ToString() const final {
if (GetData() == nullptr) { return absl::StrCat("ConstCStr: len(w/o NUL):", strlen(GetData()), ", ['",
return "CStr: [nullptr]";
}
return absl::StrCat("CStr: len(w/o NUL):", strlen(GetData()), ", ['",
GetData(), "']"); GetData(), "']");
} }
}; };