Skip to content

Use strlen / strnlen_s in RCUTILS_SAFE_FWRITE_TO_STDERR - #80

Merged
dhood merged 3 commits into
masterfrom
fix_78
Dec 3, 2017
Merged

dhood merged 3 commits into
masterfrom
fix_78

Conversation

@dhood

@dhood dhood commented Dec 2, 2017 •

Copy link
Copy Markdown
Member

fixes #78
Otherwise it does not work correctly for dynamic buffers

CI forthcoming, at which point I'll put this into review once I'm sure it works cross-platform

Otherwise it does not work correctly for dynamic buffers,
see #78
@dhood dhood added the in progress Actively being worked on (Kanban column) label Dec 2, 2017
@dhood dhood self-assigned this Dec 2, 2017
Comment thread include/rcutils/error_handling.h Outdated
} rcutils_error_state_t;

#define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) fwrite(msg, sizeof(char), sizeof(msg), stderr)
#ifdef __STDC_LIB_EXT1__ || WIN32

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please use _WIN32 instead of WIN32 like the rest of the codebase

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this comparison ever evaluate to false ?

As it stands __STDC_LIB_EXT1__ is always defined isn't it ? or is string.h undefining it ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note: @dhood is defining __STDC_WANT_LIB_EXT1__ above not __STDC_LIB_EXT1__, so my guess is string.h might set it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see, it may deserve a comment to explain what's going on here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I didnt know that modern GCC now accept boolean conditions on ifdef I remember always having to use #if defined for this kind of operation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, that is odd, I wouldn't be surprised if that didn't do what your expecting.

@wjwwood wjwwood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

Comment thread include/rcutils/error_handling.h Outdated
#endif

#define __STDC_WANT_LIB_EXT1__ 1 // needed for `strnlen_s`
#include <string.h>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nitpick: alphabetical ordering

Comment thread include/rcutils/error_handling.h Outdated
} rcutils_error_state_t;

#define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) fwrite(msg, sizeof(char), sizeof(msg), stderr)
#ifdef __STDC_LIB_EXT1__ || WIN32

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note: @dhood is defining __STDC_WANT_LIB_EXT1__ above not __STDC_LIB_EXT1__, so my guess is string.h might set it.

@dhood

dhood commented Dec 3, 2017

Copy link
Copy Markdown
Member Author

Regarding strnlen_s support:

  • Visual Studio 2015 has strnlen_s support by default
  • Some implementations in theory support it with the #define __STDC_WANT_LIB_EXT1__ 1 before #include <string.h>
  • Experimentally I've checked that the versions we're using of gcc and clang don't appear to support it even with #define __STDC_WANT_LIB_EXT1__ 1
  • Even for implementations that do support it we need to have the __STDC_WANT_LIB_EXT1__ before the #include <string.h> so it's complicated to reliably use the extension anyway, so I just removed it in the end

@mikaelarguedas mikaelarguedas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks for iterating, lgtm now, with green ci

@dhood

dhood commented Dec 3, 2017

Copy link
Copy Markdown
Member Author
  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status (flaky tests)
  • Windows Build Status (flaky tests)

@dhood
dhood merged commit 9fc616d into master Dec 3, 2017
@dhood
dhood deleted the fix_78 branch December 3, 2017 21:11
@dhood dhood removed the in progress Actively being worked on (Kanban column) label Dec 3, 2017
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

Successfully merging this pull request may close these issues.

RCUTILS_SAFE_FWRITE_TO_STDERR with variable length messages

3 participants