Skip to content

RCUTILS_SAFE_FWRITE_TO_STDERR with variable length messages #78

@dhood

Description

@dhood

If you pass a message of type const char * to RCUTILS_SAFE_FWRITE_TO_STDERR e.g.

RCUTILS_SAFE_FWRITE_TO_STDERR(rcutils_get_error_string_safe());

then the sizeof(msg) calculation in the macro gives the size of the pointer:

#define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) fwrite(msg, sizeof(char), sizeof(msg), stderr)

From what I can tell the count passed to fwrite is not a max size, but the size, so I think this line can cause buffer overrun if the msg is less than length 8. That doesn't happen with our existing code because they're all longer (instead our messages get truncated), but it could happen.

What's the appropriate fix here?

  • continue using sizeof(msg) and only use this macro with static buffers
  • switch to strlen(msg) and only use this macro will null-terminated buffers (also a risk)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions