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

Format security for string_format function #1306

Closed
mewim opened this issue Feb 21, 2023 · 2 comments
Closed

Format security for string_format function #1306

mewim opened this issue Feb 21, 2023 · 2 comments

Comments

@mewim
Copy link
Member

mewim commented Feb 21, 2023

In our string_format function (

static std::string string_format(const std::string& format, Args... args) {
int size_s = snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
if (size_s <= 0) {
throw Exception("Error during formatting.");
}
auto size = static_cast<size_t>(size_s);
auto buf = std::make_unique<char[]>(size);
snprintf(buf.get(), size, format.c_str(), args...);
return {buf.get(), buf.get() + size - 1}; // We don't want the '\0' inside
}
), snprintf is used where the format string is not a string literal. This is considered as a bad practice (https://fedoraproject.org/wiki/Format-Security-FAQ) and also breaks the build process (#1303) if a more restricted option Werror=format-security is used. If possible, we should find a way to change this.

@mewim mewim changed the title Format security for snprintf Format security for string_format function Feb 21, 2023
@vbmithr
Copy link

vbmithr commented Feb 21, 2023

Apparently (not a C++ expert) you can use a C++20 function for that:

https://en.cppreference.com/w/cpp/utility/format/format

@acquamarin
Copy link
Collaborator

acquamarin commented Mar 1, 2023

Apparently (not a C++ expert) you can use a C++20 function for that:

https://en.cppreference.com/w/cpp/utility/format/format

Unfortunately, the c++ std::format function is not available in every compiler according to this page:
https://en.cppreference.com/w/cpp/compiler_support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants