Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KDESKTOP-1129] Enables new compilation warnings with a view to sanitize code #306

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ if (NOT DEFINED APPLICATION_ICON_NAME)
set(APPLICATION_ICON_NAME "${APPLICATION_SHORTNAME}")
endif ()


if (WIN32)
add_definitions(-D__USE_MINGW_ANSI_STDIO=1)
add_definitions(-D_CRT_SECURE_NO_WARNINGS=1)
Expand All @@ -163,7 +164,7 @@ if (WIN32)
endif (WIN32)

if (UNIX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wconversion -Wsign-conversion -Weffc++ -Wimplicit-fallthrough -Wformat=2 -g")
if (APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()
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');
Copy link
Contributor Author

@luc-guyot-infomaniak luc-guyot-infomaniak Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast is now required for the compilation to succeeds.

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
Loading