Remove unneeded uses of std::map::operator[] (#2642)

These unintentionally mutate the map.  Script suggested by @danmar.
This commit is contained in:
Andrew Gaul
2025-02-08 18:21:44 -08:00
committed by GitHub
parent edf4141ad6
commit dc92b1b087
4 changed files with 70 additions and 7 deletions

View File

@ -51,6 +51,7 @@ cppcheck:
--suppress=useStlAlgorithm \
--suppress=checkLevelNormal \
--suppress=normalCheckLevelMaxBranches \
--addon=test/map-subscript-read.py \
src/ test/
#

View File

@ -4004,8 +4004,9 @@ static int s3fs_removexattr(const char* path, const char* name)
}
if(need_put_header){
// not found opened file.
if(updatemeta["x-amz-meta-xattr"].empty()){
updatemeta.erase("x-amz-meta-xattr");
auto iter = updatemeta.find("x-amz-meta-xattr");
if(iter != updatemeta.end() && iter->second.empty()){
updatemeta.erase(iter);
}
merge_headers(meta, updatemeta, true);

View File

@ -514,19 +514,32 @@ bool S3fsCred::SetIAMCredentials(const char* response)
return false;
}
AWSAccessToken = keyval[IAM_token_field];
auto aws_access_token = keyval.find(IAM_token_field);
if(aws_access_token == keyval.end()){
return false;
}
if(is_ibm_iam_auth){
auto access_token_expire = keyval.find(IAM_expiry_field);
off_t tmp_expire = 0;
if(!s3fs_strtoofft(&tmp_expire, keyval[IAM_expiry_field].c_str(), /*base=*/ 10)){
if(access_token_expire == keyval.end() || !s3fs_strtoofft(&tmp_expire, access_token_expire->second.c_str(), /*base=*/ 10)){
return false;
}
AWSAccessTokenExpire = static_cast<time_t>(tmp_expire);
}else{
AWSAccessKeyId = keyval[S3fsCred::IAMCRED_ACCESSKEYID];
AWSSecretAccessKey = keyval[S3fsCred::IAMCRED_SECRETACCESSKEY];
AWSAccessTokenExpire = cvtIAMExpireStringToTime(keyval[IAM_expiry_field].c_str());
auto access_key_id = keyval.find(S3fsCred::IAMCRED_ACCESSKEYID);
auto secret_access_key = keyval.find(S3fsCred::IAMCRED_SECRETACCESSKEY);
auto access_token_expire = keyval.find(IAM_expiry_field);
if(access_key_id == keyval.end() || secret_access_key == keyval.end() || access_token_expire == keyval.end()){
return false;
}
AWSAccessKeyId = access_key_id->second;
AWSSecretAccessKey = secret_access_key->second;
AWSAccessTokenExpire = cvtIAMExpireStringToTime(access_token_expire->second.c_str());
}
AWSAccessToken = aws_access_token->second;
return true;
}

View File

@ -0,0 +1,48 @@
#!/usr/bin/env python3
#
# locate map operator [] reads
#
# Example usage of this addon (scan a sourcefile main.cpp)
# cppcheck --dump main.cpp
# python map-subscript-read.py main.cpp.dump
import cppcheckdata
import sys
DEBUG = ('-debug' in sys.argv)
def reportError(token, severity, msg, id):
cppcheckdata.reportError(token, severity, msg, 'map', id)
def simpleMatch(token, pattern):
return cppcheckdata.simpleMatch(token, pattern)
def check_map_subscript(data):
#if data.language != 'cpp':
# return
for cfg in data.iterconfigurations():
for token in cfg.tokenlist:
if token.str != '[' or token.astOperand1 is None or token.astOperand2 is None:
continue
if token.astParent and token.astParent.str == '=' and token.astParent.astOperand1 == token:
continue
m = token.astOperand1
if m.variable is None:
continue
if simpleMatch(m.variable.typeStartToken, 'std :: map <'):
reportError(token, 'style', 'Reading from std::map with subscript operator [].', 'mapSubscriptRead')
elif simpleMatch(m.variable.typeStartToken, 'std :: unordered_map <'):
reportError(token, 'style', 'Reading from std::unordered_map with subscript operator [].', 'mapSubscriptRead')
for arg in sys.argv[1:]:
if arg == '--cli':
continue
data = cppcheckdata.CppcheckData(arg)
check_map_subscript(data)
sys.exit(cppcheckdata.EXIT_CODE)