From 009f2f55b593991e74208374a1db3bfcd21e8ff5 Mon Sep 17 00:00:00 2001 From: piotr-topnotch <131806586+piotr-topnotch@users.noreply.github.com> Date: Wed, 13 Mar 2024 02:48:53 +0100 Subject: [PATCH] use std::make_unique() for out-of-bound read test and exception safety (#113) --- src/client.cc | 13 ++++--------- tests/tests.cc | 3 ++- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/client.cc b/src/client.cc index 7f5436ff..ba765b1e 100644 --- a/src/client.cc +++ b/src/client.cc @@ -683,16 +683,11 @@ minio::s3::PutObjectResponse minio::s3::Client::PutObject(PutObjectArgs args) { "SSE operation must be performed over a secure connection"); } - char* buf = NULL; - if (args.part_count > 0) { - buf = new char[args.part_size]; - } else { - buf = new char[args.part_size + 1]; - } - std::string upload_id; - PutObjectResponse resp = PutObject(args, upload_id, buf); - delete[] buf; + auto buf = std::make_unique( + (args.part_count > 0) ? args.part_size : args.part_size + 1); + PutObjectResponse resp = PutObject(args, upload_id, buf.get()); + buf.reset(); if (!resp && !upload_id.empty()) { AbortMultipartUploadArgs amu_args; diff --git a/tests/tests.cc b/tests/tests.cc index 7f9443a1..8bf97dcb 100644 --- a/tests/tests.cc +++ b/tests/tests.cc @@ -38,7 +38,8 @@ class RandomBuf : public std::streambuf { if (size_ == 0) return EOF; size_t size = std::min(size_, buf_.size()); - setg(&buf_[0], &buf_[0], &buf_[size]); + auto* const data = buf_.data(); + setg(data, data, data + size); for (size_t i = 0; i < size; ++i) buf_[i] = charset[pick(rg)]; size_ -= size; return 0;