Skip to content

Commit

Permalink
Enables new compilation warnings with a view to sanitize code
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-guyot-infomaniak committed Oct 14, 2024
1 parent 41d8e9f commit 8640358
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ if (NOT DEFINED APPLICATION_ICON_NAME)
set(APPLICATION_ICON_NAME "${APPLICATION_SHORTNAME}")
endif ()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wconversion -Wsign-conversion -Weffc++ -Wimplicit-fallthrough -Wformat=2")

if (WIN32)
add_definitions(-D__USE_MINGW_ANSI_STDIO=1)
add_definitions(-D_CRT_SECURE_NO_WARNINGS=1)
Expand Down
35 changes: 18 additions & 17 deletions src/3rdparty/keychain/src/keychain_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

namespace {

const SecKeychainRef defaultUserKeychain = NULL; // NULL means 'default'
const SecKeychainRef defaultUserKeychain = NULL; // NULL means 'default'

/*! \brief Converts a CFString to a std::string
*
Expand All @@ -44,11 +44,11 @@ std::string CFStringToStdString(const CFStringRef cfstring) {
return std::string(ccstr);
}

auto utf16Pairs = CFStringGetLength(cfstring);
auto maxUtf8Bytes = CFStringGetMaximumSizeForEncoding(utf16Pairs, kCFStringEncodingUTF8);
const auto utf16Pairs = CFStringGetLength(cfstring);
const auto maxUtf8Bytes = CFStringGetMaximumSizeForEncoding(utf16Pairs, kCFStringEncodingUTF8);

std::vector<char> cstr(maxUtf8Bytes, '\0');
auto result = CFStringGetCString(cfstring, cstr.data(), cstr.size(), kCFStringEncodingUTF8);
std::vector<char> cstr(static_cast<size_t>(maxUtf8Bytes), '\0');
const auto result = CFStringGetCString(cfstring, cstr.data(), static_cast<long>(cstr.size()), kCFStringEncodingUTF8);

return result ? std::string(cstr.data()) : std::string();
}
Expand Down Expand Up @@ -90,9 +90,9 @@ void updateError(keychain::Error &err, OSStatus status) {
break;

// potential errors in case the user needs to unlock the keychain first
case errSecUserCanceled: // user pressed the Cancel button
case errSecAuthFailed: // too many failed password attempts
case errSecInteractionRequired: // user interaction required but not allowed
case errSecUserCanceled: // user pressed the Cancel button
case errSecAuthFailed: // too many failed password attempts
case errSecInteractionRequired: // user interaction required but not allowed
err.type = keychain::ErrorType::AccessDenied;
break;

Expand All @@ -110,8 +110,8 @@ OSStatus modifyPassword(const std::string &serviceName, const std::string &user,
SecKeychainItemRef item = NULL;
OSStatus status = SecKeychainFindGenericPassword(defaultUserKeychain, static_cast<UInt32>(serviceName.length()),
serviceName.data(), static_cast<UInt32>(user.length()), user.data(),
NULL, // unused output parameter
NULL, // unused output parameter
NULL, // unused output parameter
NULL, // unused output parameter
&item);

if (status == errSecSuccess) {
Expand All @@ -125,7 +125,7 @@ OSStatus modifyPassword(const std::string &serviceName, const std::string &user,
return status;
}

} // namespace
} // namespace

namespace keychain {

Expand All @@ -134,9 +134,10 @@ void setPassword(const std::string &package, const std::string &service, const s
err = Error{};
const auto serviceName = makeServiceName(package, service);

OSStatus status = SecKeychainAddGenericPassword(
defaultUserKeychain, static_cast<UInt32>(serviceName.length()), serviceName.data(), static_cast<UInt32>(user.length()),
user.data(), static_cast<UInt32>(password.length()), password.data(), NULL /* unused output parameter */);
OSStatus status =
SecKeychainAddGenericPassword(defaultUserKeychain, static_cast<UInt32>(serviceName.length()), serviceName.data(),
static_cast<UInt32>(user.length()), user.data(), static_cast<UInt32>(password.length()),
password.data(), NULL /* unused output parameter */);

if (status == errSecDuplicateItem) {
// password exists -- override
Expand Down Expand Up @@ -177,8 +178,8 @@ void deletePassword(const std::string &package, const std::string &service, cons

OSStatus status = SecKeychainFindGenericPassword(defaultUserKeychain, static_cast<UInt32>(serviceName.length()),
serviceName.data(), static_cast<UInt32>(user.length()), user.data(),
NULL, // unused output parameter
NULL, // unused output parameter
NULL, // unused output parameter
NULL, // unused output parameter
&item);

if (status != errSecSuccess) {
Expand All @@ -195,4 +196,4 @@ void deletePassword(const std::string &package, const std::string &service, cons
}
}

} // namespace keychain
} // namespace keychain

0 comments on commit 8640358

Please sign in to comment.