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

Use GCC extension for printf-like functions #154

Merged
merged 3 commits into from
Apr 30, 2019

Conversation

thomas-moulard
Copy link
Contributor

This commit annotates rcutils_format_string_limit, rcutils_log
and rcutils_snprintf with the GCC extension for printf-like
statements.

Annotating this function allows GCC to emit warnings when dangerous
patterns are used through the whole codebase.

As those functions are wrappers around different flavors of printf,
surfacing those warnings prevents dangerous printf statements.

For instance:

const char* message = // some dynamically evaluated logging message
printf(message);

...is unsafe. Instead, we should be using:

printf("%s", message);

...to avoid crashes if the variable "message" contains a sequence
of characters printf may recognize (like "%d").

printf-related warnings are enabled by '-Wformat*' flags ('-Wformat',
'-Wformat-security', etc.).

The complete list of flags and detected patterns is available in
the GCC documentation:

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

Note: clang does also support this attribute but not MSVC. On
Windows, no additional safety is provided by this commit.

Signed-off-by: Thomas Moulard tmoulard@amazon.com

This commit annotates rcutils_format_string_limit, rcutils_log
and rcutils_snprintf with the GCC extension for printf-like
statements.

Annotating this function allows GCC to emit warnings when dangerous
patterns are used through the whole codebase.

As those functions are wrappers around different flavors of printf,
surfacing those warnings prevents dangerous printf statements.

For instance:

const char* message = // some dynamically evaluated logging message
printf(message);

...is unsafe. Instead, we should be using:

printf("%s", message);

...to avoid crashes if the variable "message" contains a sequence
of characters printf may recognize (like "%d").

printf-related warnings are enabled by '-Wformat*' flags ('-Wformat',
'-Wformat-security', etc.).

The complete list of flags and detected patterns is available in
the GCC documentation:

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

Note: clang does also support this attribute but not MSVC. On
Windows, no additional safety is provided by this commit.

Signed-off-by: Thomas Moulard <tmoulard@amazon.com>
@tfoote tfoote added the in progress Actively being worked on (Kanban column) label Apr 27, 2019
@@ -113,15 +113,15 @@ TEST(CLASSNAME(TestLogging, RMW_IMPLEMENTATION), test_logging) {
EXPECT_EQ("name3", g_last_log_event.name);
EXPECT_EQ("message 33", g_last_log_event.message);

rcutils_log(NULL, RCUTILS_LOG_SEVERITY_WARN, "", "");
rcutils_log(NULL, RCUTILS_LOG_SEVERITY_WARN, "", "%s", "");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This prevents GCC emitting warning (see -Wformat-zero-length)

Signed-off-by: Thomas Moulard <tmoulard@amazon.com>
@thomas-moulard
Copy link
Contributor Author

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Signed-off-by: Thomas Moulard <tmoulard@amazon.com>
@thomas-moulard
Copy link
Contributor Author

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@thomas-moulard
Copy link
Contributor Author

@dirk-thomas Good to go on my side 🚢

@dirk-thomas dirk-thomas added enhancement New feature or request in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Apr 30, 2019
@dirk-thomas
Copy link
Member

Thanks for the improvement.

@dirk-thomas dirk-thomas merged commit dcb2733 into ros2:master Apr 30, 2019
@dirk-thomas dirk-thomas removed the in review Waiting for review (Kanban column) label Apr 30, 2019
nuclearsandwich added a commit to ros/pluginlib that referenced this pull request May 1, 2019
With ros2/rcutils#154 warnings are emited for
"dangerous patterns" in format strings.
%p expects a void * argument so a void * argument we shall provide.

Signed-off-by: Steven! Ragnarök <steven@nuclearsandwich.com>
nuclearsandwich added a commit to ros/pluginlib that referenced this pull request May 1, 2019
With ros2/rcutils#154 warnings are emitted for
"dangerous patterns" in format strings.
%p expects a void * argument so a void * argument we shall provide.

Signed-off-by: Steven! Ragnarök <steven@nuclearsandwich.com>
@dirk-thomas
Copy link
Member

@thomas-moulard This patch broke all nightly builds. Please run more through tests in the future. ros/pluginlib#152 will hopefully fix the builds.

nuclearsandwich added a commit to ros2/message_filters that referenced this pull request May 1, 2019
This resolves warnings which popped up as a result of
ros2/rcutils#154.
clalancette pushed a commit to ros2/message_filters that referenced this pull request May 1, 2019
* Make format string agree with argument type.

This resolves warnings which popped up as a result of
ros2/rcutils#154.

* Switch to PRId64.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
thomas-moulard pushed a commit to thomas-moulard/rosbag2 that referenced this pull request May 2, 2019
GCC now warns us that log message should not be passed
as format string. This is a dangerous pattern because
if the log message contains a sequence of characters
printf will interpret such as "%d", the program may crash.

Signed-off-by: Thomas Moulard <tmoulard@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants