mirror of
https://github.com/s3fs-fuse/s3fs-fuse.git
synced 2025-02-03 02:58:26 +00:00
Convert manual memory allocations to std::unique_ptr (#2253)
This commit is contained in:
parent
c5a75a1fb2
commit
528a61718d
12
src/curl.cpp
12
src/curl.cpp
@ -803,17 +803,11 @@ bool S3fsCurl::PushbackSseKeys(const std::string& input)
|
|||||||
std::string base64_key;
|
std::string base64_key;
|
||||||
std::string raw_key;
|
std::string raw_key;
|
||||||
if(onekey.length() > 256 / 8){
|
if(onekey.length() > 256 / 8){
|
||||||
char* p_key;
|
|
||||||
size_t keylength;
|
size_t keylength;
|
||||||
|
|
||||||
if(nullptr != (p_key = reinterpret_cast<char*>(s3fs_decode64(onekey.c_str(), onekey.size(), &keylength)))) {
|
std::unique_ptr<unsigned char[]> p_key(s3fs_decode64(onekey.c_str(), onekey.size(), &keylength));
|
||||||
raw_key = std::string(p_key, keylength);
|
raw_key = std::string(reinterpret_cast<char *>(p_key.get()), keylength);
|
||||||
base64_key = onekey;
|
base64_key = onekey;
|
||||||
delete[] p_key;
|
|
||||||
} else {
|
|
||||||
S3FS_PRN_ERR("Failed to convert base64 to SSE-C key %s", onekey.c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
base64_key = s3fs_base64(reinterpret_cast<const unsigned char*>(onekey.c_str()), onekey.length());
|
base64_key = s3fs_base64(reinterpret_cast<const unsigned char*>(onekey.c_str()), onekey.length());
|
||||||
raw_key = onekey;
|
raw_key = onekey;
|
||||||
|
10
src/s3fs.cpp
10
src/s3fs.cpp
@ -3636,7 +3636,7 @@ static bool get_xattr_posix_key_value(const char* path, std::string& xattrvalue,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// convert value by base64
|
// 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);
|
free_xattrs(xattrs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -3764,7 +3764,7 @@ static std::string raw_build_xattrs(const xattrs_t& xattrs)
|
|||||||
strxattrs += "\":\"";
|
strxattrs += "\":\"";
|
||||||
|
|
||||||
if(iter->second){
|
if(iter->second){
|
||||||
strxattrs += s3fs_base64(iter->second->pvalue, iter->second->length);
|
strxattrs += s3fs_base64(iter->second->pvalue.get(), iter->second->length);
|
||||||
}
|
}
|
||||||
strxattrs += '\"';
|
strxattrs += '\"';
|
||||||
}
|
}
|
||||||
@ -3821,8 +3821,8 @@ static int set_xattrs_to_header(headers_t& meta, const char* name, const char* v
|
|||||||
PXATTRVAL pval = new XATTRVAL;
|
PXATTRVAL pval = new XATTRVAL;
|
||||||
pval->length = size;
|
pval->length = size;
|
||||||
if(0 < size){
|
if(0 < size){
|
||||||
pval->pvalue = new unsigned char[size];
|
pval->pvalue.reset(new unsigned char[size]);
|
||||||
memcpy(pval->pvalue, value, size);
|
memcpy(pval->pvalue.get(), value, size);
|
||||||
}else{
|
}else{
|
||||||
pval->pvalue = nullptr;
|
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;
|
unsigned char* pvalue = nullptr;
|
||||||
if(nullptr != xiter->second){
|
if(nullptr != xiter->second){
|
||||||
length = xiter->second->length;
|
length = xiter->second->length;
|
||||||
pvalue = xiter->second->pvalue;
|
pvalue = xiter->second->pvalue.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(0 < size){
|
if(0 < size){
|
||||||
|
@ -418,14 +418,9 @@ inline unsigned char char_decode64(const char ch)
|
|||||||
return by;
|
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;
|
std::unique_ptr<unsigned char[]> result(new unsigned char[input_len / 4 * 3]);
|
||||||
if(!input || 0 == input_len || !plength){
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
result = new unsigned char[input_len / 4 * 3];
|
|
||||||
|
|
||||||
unsigned char parts[4];
|
unsigned char parts[4];
|
||||||
size_t rpos;
|
size_t rpos;
|
||||||
size_t wpos;
|
size_t wpos;
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#define S3FS_STRING_UTIL_H_
|
#define S3FS_STRING_UTIL_H_
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
//
|
//
|
||||||
// A collection of string utilities for manipulating URLs and HTTP responses.
|
// 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_lower(const unsigned char* input, size_t length);
|
||||||
std::string s3fs_hex_upper(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);
|
std::string s3fs_base64(const unsigned char* input, size_t length);
|
||||||
// TODO: return std::unique_ptr
|
std::unique_ptr<unsigned char[]> s3fs_decode64(const char* input, size_t input_len, size_t* plength);
|
||||||
unsigned char* s3fs_decode64(const char* input, size_t input_len, size_t* plength);
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// WTF8
|
// WTF8
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <memory>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -53,35 +54,35 @@ void test_trim()
|
|||||||
|
|
||||||
void test_base64()
|
void test_base64()
|
||||||
{
|
{
|
||||||
unsigned char *buf;
|
std::unique_ptr<unsigned char[]> buf;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
ASSERT_STREQUALS(s3fs_base64(nullptr, 0).c_str(), nullptr);
|
ASSERT_STREQUALS(s3fs_base64(nullptr, 0).c_str(), nullptr);
|
||||||
buf = s3fs_decode64(nullptr, 0, &len);
|
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);
|
ASSERT_STREQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>(""), 0).c_str(), nullptr);
|
||||||
buf = s3fs_decode64("", 0, &len);
|
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=="));
|
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("1"), 1), std::string("MQ=="));
|
||||||
buf = s3fs_decode64("MQ==", 4, &len);
|
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(len, static_cast<size_t>(1));
|
||||||
|
|
||||||
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("12"), 2), std::string("MTI="));
|
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("12"), 2), std::string("MTI="));
|
||||||
buf = s3fs_decode64("MTI=", 4, &len);
|
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(len, static_cast<size_t>(2));
|
||||||
|
|
||||||
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("123"), 3), std::string("MTIz"));
|
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("123"), 3), std::string("MTIz"));
|
||||||
buf = s3fs_decode64("MTIz", 4, &len);
|
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(len, static_cast<size_t>(3));
|
||||||
|
|
||||||
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("1234"), 4), std::string("MTIzNA=="));
|
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("1234"), 4), std::string("MTIzNA=="));
|
||||||
buf = s3fs_decode64("MTIzNA==", 8, &len);
|
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));
|
ASSERT_EQUALS(len, static_cast<size_t>(4));
|
||||||
|
|
||||||
// TODO: invalid input
|
// TODO: invalid input
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -56,14 +57,11 @@
|
|||||||
//
|
//
|
||||||
typedef struct xattr_value
|
typedef struct xattr_value
|
||||||
{
|
{
|
||||||
unsigned char* pvalue;
|
std::unique_ptr<unsigned char[]> pvalue;
|
||||||
size_t length;
|
size_t length;
|
||||||
|
|
||||||
explicit xattr_value(unsigned char* pval = nullptr, size_t len = 0) : pvalue(pval), length(len) {}
|
explicit xattr_value(unsigned char* pval = nullptr, size_t len = 0) : pvalue(pval), length(len) {}
|
||||||
~xattr_value()
|
~xattr_value() {}
|
||||||
{
|
|
||||||
delete[] pvalue;
|
|
||||||
}
|
|
||||||
}XATTRVAL, *PXATTRVAL;
|
}XATTRVAL, *PXATTRVAL;
|
||||||
|
|
||||||
typedef std::map<std::string, PXATTRVAL> xattrs_t;
|
typedef std::map<std::string, PXATTRVAL> xattrs_t;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user