Merge pull request #1435 from gaul/no-exceptions

Remove exceptions from s3fs_strtoofft
This commit is contained in:
Takeshi Nakatani 2020-10-04 22:19:25 +09:00 committed by GitHub
commit 493cf20f95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 51 deletions

View File

@ -33,7 +33,7 @@ AC_CHECK_HEADERS([sys/xattr.h])
AC_CHECK_HEADERS([attr/xattr.h]) AC_CHECK_HEADERS([attr/xattr.h])
AC_CHECK_HEADERS([sys/extattr.h]) AC_CHECK_HEADERS([sys/extattr.h])
CXXFLAGS="$CXXFLAGS -Wall -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=2" CXXFLAGS="$CXXFLAGS -Wall -fno-exceptions -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=2"
dnl ---------------------------------------------- dnl ----------------------------------------------
dnl For macOS dnl For macOS

View File

@ -1133,7 +1133,7 @@ S3fsCurl* S3fsCurl::UploadMultipartPostRetryCallback(S3fsCurl* s3fscurl)
if(!get_keyword_value(s3fscurl->url, "partNumber", part_num_str)){ if(!get_keyword_value(s3fscurl->url, "partNumber", part_num_str)){
return NULL; return NULL;
} }
if(!try_strtoofft(part_num_str.c_str(), tmp_part_num, /*base=*/ 10)){ if(!s3fs_strtoofft(&tmp_part_num, part_num_str.c_str(), /*base=*/ 10)){
return NULL; return NULL;
} }
part_num = static_cast<off_t>(tmp_part_num); part_num = static_cast<off_t>(tmp_part_num);
@ -1181,7 +1181,7 @@ S3fsCurl* S3fsCurl::CopyMultipartPostRetryCallback(S3fsCurl* s3fscurl)
if(!get_keyword_value(s3fscurl->url, "partNumber", part_num_str)){ if(!get_keyword_value(s3fscurl->url, "partNumber", part_num_str)){
return NULL; return NULL;
} }
if(!try_strtoofft(part_num_str.c_str(), tmp_part_num, /*base=*/ 10)){ if(!s3fs_strtoofft(&tmp_part_num, part_num_str.c_str(), /*base=*/ 10)){
return NULL; return NULL;
} }
part_num = static_cast<off_t>(tmp_part_num); part_num = static_cast<off_t>(tmp_part_num);
@ -1664,7 +1664,7 @@ bool S3fsCurl::SetIAMCredentials(const char* response)
if(S3fsCurl::is_ibm_iam_auth){ if(S3fsCurl::is_ibm_iam_auth){
off_t tmp_expire = 0; off_t tmp_expire = 0;
if(!try_strtoofft(keyval[std::string(S3fsCurl::IAM_expiry_field)].c_str(), tmp_expire, /*base=*/ 10)){ if(!s3fs_strtoofft(&tmp_expire, keyval[std::string(S3fsCurl::IAM_expiry_field)].c_str(), /*base=*/ 10)){
return false; return false;
} }
S3fsCurl::AWSAccessTokenExpire = static_cast<time_t>(tmp_expire); S3fsCurl::AWSAccessTokenExpire = static_cast<time_t>(tmp_expire);

View File

@ -59,51 +59,30 @@ template std::string str(unsigned long long value);
// Functions // Functions
//------------------------------------------------------------------- //-------------------------------------------------------------------
// replacement for C++11 std::stoll bool s3fs_strtoofft(off_t* value, const char* str, int base)
off_t s3fs_strtoofft(const char* str, int base)
{ {
if(value == NULL || str == NULL){
return false;
}
errno = 0; errno = 0;
char *temp; char *temp;
long long result = strtoll(str, &temp, base); long long result = strtoll(str, &temp, base);
if(temp == str || *temp != '\0'){ if(temp == str || *temp != '\0'){
throw std::invalid_argument("s3fs_strtoofft"); return false;
} }
if((result == LLONG_MIN || result == LLONG_MAX) && errno == ERANGE){ if((result == LLONG_MIN || result == LLONG_MAX) && errno == ERANGE){
throw std::out_of_range("s3fs_strtoofft"); return false;
} }
return result;
}
// wrapped s3fs_strtoofft() *value = result;
//
// This function catches the s3fs_strtoofft () exception and returns a boolean value.
//
bool try_strtoofft(const char* str, off_t& value, int base)
{
if(str){
try{
value = s3fs_strtoofft(str, base);
}catch(std::exception &e){
S3FS_PRN_WARN("something error is occurred in convert std::string(%s) to off_t.", str);
return false;
}
}else{
S3FS_PRN_WARN("parameter std::string is null.");
return false;
}
return true; return true;
} }
// wrapped try_strtoofft -> s3fs_strtoofft()
//
// This function returns 0 if a value that cannot be converted is specified.
// Only call if 0 is considered an error and the operation can continue.
//
off_t cvt_strtoofft(const char* str, int base) off_t cvt_strtoofft(const char* str, int base)
{ {
off_t result = 0; off_t result = 0;
if(!try_strtoofft(str, result, base)){ if(!s3fs_strtoofft(&result, str, base)){
S3FS_PRN_WARN("something error is occurred in convert std::string(%s) to off_t, thus return 0 as default.", (str ? str : "null")); S3FS_PRN_WARN("something error is occurred in convert std::string(%s) to off_t, thus return 0 as default.", (str ? str : "null"));
return 0; return 0;
} }

View File

@ -55,10 +55,14 @@ template <class T> std::string str(T value);
// Utilities // Utilities
//------------------------------------------------------------------- //-------------------------------------------------------------------
// //
// Convert string to off_t. Throws std::invalid_argument and std::out_of_range on bad input. // Convert string to off_t. Returns false on bad input.
// Replacement for C++11 std::stoll.
//
bool s3fs_strtoofft(off_t* value, const char* str, int base = 0);
//
// This function returns 0 if a value that cannot be converted is specified.
// Only call if 0 is considered an error and the operation can continue.
// //
off_t s3fs_strtoofft(const char* str, int base = 0);
bool try_strtoofft(const char* str, off_t& value, int base = 0);
off_t cvt_strtoofft(const char* str, int base = 0); off_t cvt_strtoofft(const char* str, int base = 0);
// //

View File

@ -89,19 +89,30 @@ void test_base64()
void test_strtoofft() void test_strtoofft()
{ {
ASSERT_EQUALS(s3fs_strtoofft("0"), static_cast<off_t>(0L)); off_t value;
ASSERT_EQUALS(s3fs_strtoofft("9"), static_cast<off_t>(9L));
try{ ASSERT_TRUE(s3fs_strtoofft(&value, "0"));
s3fs_strtoofft("A"); ASSERT_EQUALS(value, static_cast<off_t>(0L));
abort();
}catch(std::exception &e){ ASSERT_TRUE(s3fs_strtoofft(&value, "9"));
// expected ASSERT_EQUALS(value, static_cast<off_t>(9L));
}
ASSERT_EQUALS(s3fs_strtoofft("A", /*base=*/ 16), static_cast<off_t>(10L)); ASSERT_FALSE(s3fs_strtoofft(&value, "A"));
ASSERT_EQUALS(s3fs_strtoofft("F", /*base=*/ 16), static_cast<off_t>(15L));
ASSERT_EQUALS(s3fs_strtoofft("a", /*base=*/ 16), static_cast<off_t>(10L)); ASSERT_TRUE(s3fs_strtoofft(&value, "A", /*base=*/ 16));
ASSERT_EQUALS(s3fs_strtoofft("f", /*base=*/ 16), static_cast<off_t>(15L)); ASSERT_EQUALS(value, static_cast<off_t>(10L));
ASSERT_EQUALS(s3fs_strtoofft("deadbeef", /*base=*/ 16), static_cast<off_t>(3735928559L));
ASSERT_TRUE(s3fs_strtoofft(&value, "F", /*base=*/ 16));
ASSERT_EQUALS(value, static_cast<off_t>(15L));
ASSERT_TRUE(s3fs_strtoofft(&value, "a", /*base=*/ 16));
ASSERT_EQUALS(value, static_cast<off_t>(10L));
ASSERT_TRUE(s3fs_strtoofft(&value, "f", /*base=*/ 16));
ASSERT_EQUALS(value, static_cast<off_t>(15L));
ASSERT_TRUE(s3fs_strtoofft(&value, "deadbeef", /*base=*/ 16));
ASSERT_EQUALS(value, static_cast<off_t>(3735928559L));
} }
void test_wtf8_encoding() void test_wtf8_encoding()

View File

@ -74,6 +74,8 @@ void assert_strequals(const char *x, const char *y, const char *file, int line)
} }
} }
#define ASSERT_TRUE(x) assert_equals((x), true, __FILE__, __LINE__)
#define ASSERT_FALSE(x) assert_equals((x), false, __FILE__, __LINE__)
#define ASSERT_EQUALS(x, y) assert_equals((x), (y), __FILE__, __LINE__) #define ASSERT_EQUALS(x, y) assert_equals((x), (y), __FILE__, __LINE__)
#define ASSERT_NEQUALS(x, y) assert_nequals((x), (y), __FILE__, __LINE__) #define ASSERT_NEQUALS(x, y) assert_nequals((x), (y), __FILE__, __LINE__)
#define ASSERT_STREQUALS(x, y) assert_strequals((x), (y), __FILE__, __LINE__) #define ASSERT_STREQUALS(x, y) assert_strequals((x), (y), __FILE__, __LINE__)