diff --git a/src/curl.cpp b/src/curl.cpp index cb22a0f..4530af7 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -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(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 p_key(s3fs_decode64(onekey.c_str(), onekey.size(), &keylength)); + raw_key = std::string(reinterpret_cast(p_key.get()), keylength); + base64_key = onekey; } else { base64_key = s3fs_base64(reinterpret_cast(onekey.c_str()), onekey.length()); raw_key = onekey; diff --git a/src/s3fs.cpp b/src/s3fs.cpp index 81008d7..f4ea31f 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -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){ diff --git a/src/string_util.cpp b/src/string_util.cpp index a8c5a38..110a311 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -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 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 result(new unsigned char[input_len / 4 * 3]); unsigned char parts[4]; size_t rpos; size_t wpos; diff --git a/src/string_util.h b/src/string_util.h index 1e14a35..b4e87a5 100644 --- a/src/string_util.h +++ b/src/string_util.h @@ -22,6 +22,7 @@ #define S3FS_STRING_UTIL_H_ #include +#include // // 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 s3fs_decode64(const char* input, size_t input_len, size_t* plength); // // WTF8 diff --git a/src/test_string_util.cpp b/src/test_string_util.cpp index 24ba9c9..4736dfd 100644 --- a/src/test_string_util.cpp +++ b/src/test_string_util.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -53,35 +54,35 @@ void test_trim() void test_base64() { - unsigned char *buf; + std::unique_ptr buf; size_t len; ASSERT_STREQUALS(s3fs_base64(nullptr, 0).c_str(), nullptr); buf = s3fs_decode64(nullptr, 0, &len); - ASSERT_BUFEQUALS(reinterpret_cast(buf), len, nullptr, 0); + ASSERT_BUFEQUALS(reinterpret_cast(buf.get()), len, nullptr, 0); ASSERT_STREQUALS(s3fs_base64(reinterpret_cast(""), 0).c_str(), nullptr); buf = s3fs_decode64("", 0, &len); - ASSERT_BUFEQUALS(reinterpret_cast(buf), len, nullptr, 0); + ASSERT_BUFEQUALS(reinterpret_cast(buf.get()), len, nullptr, 0); ASSERT_EQUALS(s3fs_base64(reinterpret_cast("1"), 1), std::string("MQ==")); buf = s3fs_decode64("MQ==", 4, &len); - ASSERT_BUFEQUALS(reinterpret_cast(buf), len, "1", 1); + ASSERT_BUFEQUALS(reinterpret_cast(buf.get()), len, "1", 1); ASSERT_EQUALS(len, static_cast(1)); ASSERT_EQUALS(s3fs_base64(reinterpret_cast("12"), 2), std::string("MTI=")); buf = s3fs_decode64("MTI=", 4, &len); - ASSERT_BUFEQUALS(reinterpret_cast(buf), len, "12", 2); + ASSERT_BUFEQUALS(reinterpret_cast(buf.get()), len, "12", 2); ASSERT_EQUALS(len, static_cast(2)); ASSERT_EQUALS(s3fs_base64(reinterpret_cast("123"), 3), std::string("MTIz")); buf = s3fs_decode64("MTIz", 4, &len); - ASSERT_BUFEQUALS(reinterpret_cast(buf), len, "123", 3); + ASSERT_BUFEQUALS(reinterpret_cast(buf.get()), len, "123", 3); ASSERT_EQUALS(len, static_cast(3)); ASSERT_EQUALS(s3fs_base64(reinterpret_cast("1234"), 4), std::string("MTIzNA==")); buf = s3fs_decode64("MTIzNA==", 8, &len); - ASSERT_BUFEQUALS(reinterpret_cast(buf), len, "1234", 4); + ASSERT_BUFEQUALS(reinterpret_cast(buf.get()), len, "1234", 4); ASSERT_EQUALS(len, static_cast(4)); // TODO: invalid input diff --git a/src/types.h b/src/types.h index 3b3c07b..b73bdef 100644 --- a/src/types.h +++ b/src/types.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -56,14 +57,11 @@ // typedef struct xattr_value { - unsigned char* pvalue; + std::unique_ptr 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 xattrs_t;