Convert manual memory allocations to std::unique_ptr (#2253)

This commit is contained in:
Andrew Gaul 2023-08-11 23:26:07 +09:00 committed by GitHub
parent c5a75a1fb2
commit 528a61718d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 35 deletions

View File

@ -803,17 +803,11 @@ bool S3fsCurl::PushbackSseKeys(const std::string& input)
std::string base64_key;
std::string raw_key;
if(onekey.length() > 256 / 8){
char* p_key;
size_t keylength;
if(nullptr != (p_key = reinterpret_cast<char*>(s3fs_decode64(onekey.c_str(), onekey.size(), &keylength)))) {
raw_key = std::string(p_key, keylength);
base64_key = onekey;
delete[] p_key;
} else {
S3FS_PRN_ERR("Failed to convert base64 to SSE-C key %s", onekey.c_str());
return false;
}
std::unique_ptr<unsigned char[]> p_key(s3fs_decode64(onekey.c_str(), onekey.size(), &keylength));
raw_key = std::string(reinterpret_cast<char *>(p_key.get()), keylength);
base64_key = onekey;
} else {
base64_key = s3fs_base64(reinterpret_cast<const unsigned char*>(onekey.c_str()), onekey.length());
raw_key = onekey;

View File

@ -3636,7 +3636,7 @@ static bool get_xattr_posix_key_value(const char* path, std::string& xattrvalue,
}
// convert value by base64
xattrvalue = s3fs_base64(iter->second->pvalue, iter->second->length);
xattrvalue = s3fs_base64(iter->second->pvalue.get(), iter->second->length);
free_xattrs(xattrs);
return true;
@ -3764,7 +3764,7 @@ static std::string raw_build_xattrs(const xattrs_t& xattrs)
strxattrs += "\":\"";
if(iter->second){
strxattrs += s3fs_base64(iter->second->pvalue, iter->second->length);
strxattrs += s3fs_base64(iter->second->pvalue.get(), iter->second->length);
}
strxattrs += '\"';
}
@ -3821,8 +3821,8 @@ static int set_xattrs_to_header(headers_t& meta, const char* name, const char* v
PXATTRVAL pval = new XATTRVAL;
pval->length = size;
if(0 < size){
pval->pvalue = new unsigned char[size];
memcpy(pval->pvalue, value, size);
pval->pvalue.reset(new unsigned char[size]);
memcpy(pval->pvalue.get(), value, size);
}else{
pval->pvalue = nullptr;
}
@ -4034,7 +4034,7 @@ static int s3fs_getxattr(const char* path, const char* name, char* value, size_t
unsigned char* pvalue = nullptr;
if(nullptr != xiter->second){
length = xiter->second->length;
pvalue = xiter->second->pvalue;
pvalue = xiter->second->pvalue.get();
}
if(0 < size){

View File

@ -418,14 +418,9 @@ inline unsigned char char_decode64(const char ch)
return by;
}
unsigned char* s3fs_decode64(const char* input, size_t input_len, size_t* plength)
std::unique_ptr<unsigned char[]> s3fs_decode64(const char* input, size_t input_len, size_t* plength)
{
unsigned char* result;
if(!input || 0 == input_len || !plength){
return nullptr;
}
result = new unsigned char[input_len / 4 * 3];
std::unique_ptr<unsigned char[]> result(new unsigned char[input_len / 4 * 3]);
unsigned char parts[4];
size_t rpos;
size_t wpos;

View File

@ -22,6 +22,7 @@
#define S3FS_STRING_UTIL_H_
#include <cstring>
#include <memory>
//
// A collection of string utilities for manipulating URLs and HTTP responses.
@ -106,8 +107,7 @@ bool get_keyword_value(const std::string& target, const char* keyword, std::stri
std::string s3fs_hex_lower(const unsigned char* input, size_t length);
std::string s3fs_hex_upper(const unsigned char* input, size_t length);
std::string s3fs_base64(const unsigned char* input, size_t length);
// TODO: return std::unique_ptr
unsigned char* s3fs_decode64(const char* input, size_t input_len, size_t* plength);
std::unique_ptr<unsigned char[]> s3fs_decode64(const char* input, size_t input_len, size_t* plength);
//
// WTF8

View File

@ -20,6 +20,7 @@
#include <cstdlib>
#include <limits>
#include <memory>
#include <stdint.h>
#include <string>
@ -53,35 +54,35 @@ void test_trim()
void test_base64()
{
unsigned char *buf;
std::unique_ptr<unsigned char[]> buf;
size_t len;
ASSERT_STREQUALS(s3fs_base64(nullptr, 0).c_str(), nullptr);
buf = s3fs_decode64(nullptr, 0, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, nullptr, 0);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf.get()), len, nullptr, 0);
ASSERT_STREQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>(""), 0).c_str(), nullptr);
buf = s3fs_decode64("", 0, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, nullptr, 0);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf.get()), len, nullptr, 0);
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("1"), 1), std::string("MQ=="));
buf = s3fs_decode64("MQ==", 4, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, "1", 1);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf.get()), len, "1", 1);
ASSERT_EQUALS(len, static_cast<size_t>(1));
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("12"), 2), std::string("MTI="));
buf = s3fs_decode64("MTI=", 4, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, "12", 2);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf.get()), len, "12", 2);
ASSERT_EQUALS(len, static_cast<size_t>(2));
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("123"), 3), std::string("MTIz"));
buf = s3fs_decode64("MTIz", 4, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, "123", 3);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf.get()), len, "123", 3);
ASSERT_EQUALS(len, static_cast<size_t>(3));
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("1234"), 4), std::string("MTIzNA=="));
buf = s3fs_decode64("MTIzNA==", 8, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, "1234", 4);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf.get()), len, "1234", 4);
ASSERT_EQUALS(len, static_cast<size_t>(4));
// TODO: invalid input

View File

@ -25,6 +25,7 @@
#include <cstring>
#include <string>
#include <map>
#include <memory>
#include <list>
#include <vector>
@ -56,14 +57,11 @@
//
typedef struct xattr_value
{
unsigned char* pvalue;
std::unique_ptr<unsigned char[]> pvalue;
size_t length;
explicit xattr_value(unsigned char* pval = nullptr, size_t len = 0) : pvalue(pval), length(len) {}
~xattr_value()
{
delete[] pvalue;
}
~xattr_value() {}
}XATTRVAL, *PXATTRVAL;
typedef std::map<std::string, PXATTRVAL> xattrs_t;