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

fix: Avoid using strncat and a trailing newline in logs #301

Merged
merged 2 commits into from
Jun 17, 2020

Conversation

Swatinem
Copy link
Member

The usage of strcat bothers gcc-10 and clang analyzer.

fixes #298

@Swatinem Swatinem requested a review from a team June 16, 2020 16:30
Copy link
Member

@jan-auer jan-auer left a comment

Choose a reason for hiding this comment

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

This is coming a bit late, probably, but: Why don't you just print in three stages? Something like:

fprintf(stderr, "[sentry] %s", sentry__logger_describe(level));
vfprintf(stderr, message, args);
fputc('\n', stderr);

You have a few (well optimizable) function calls vs a string allocation here. The only disadvantage that I can see is that stderr can be unbuffered, but I think even that should not be an issue here.

@Mixaill
Copy link
Contributor

Mixaill commented Jun 16, 2020

The only disadvantage that I can see is that stderr can be unbuffered, but I think even that should not be an issue here.

It also could lead to messed output if application thread is also trying to write to the stderr at the same moment.

@Swatinem
Copy link
Member Author

It also could lead to messed output if application thread is also trying to write to the stderr at the same moment.

That’s what I wanted to say. Even our example itself is quite spammy with the http thread in the background, and you do have interspersed logging there, which is quite difficult to follow as it is. Either way, while the allocation is suboptimal, I suspect people won’t have logging on in production anyway, and if they do they will probably integrate with their own system.

@jan-auer
Copy link
Member

Excellent point, I knew something was up with this :) Alright, let me take another stab at this. How about:

const char *prefix = "[sentry] ";
const char *priority = sentry__logger_describe(level);

size_t total_len = strlen(prefix) + strlen(priority) + strlen(message) + 1;
char *format = sentry_malloc(total_len);
snprintf("%s%s%s", total_len, prefix, priority, message);

vfprintf(stderr, format, args);

Of course, prefix could also be inlined, but that's probably more prone to errors when changing the prefix.

@Swatinem
Copy link
Member Author

I also thought about that before, but yes, lets do it ;-)

Copy link
Member

@jan-auer jan-auer left a comment

Choose a reason for hiding this comment

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

Thanks for bearing with me :) I have to admit that I did not look into the performance implications of using snprintf over memcpy. My assumption is that this will still be optimized.

PR Title needs updating. Then G2G.

@Swatinem Swatinem changed the title fix: Use explicit memcpy and newline in logger fix: Use avoid using strncat and a trailing newline in logs Jun 17, 2020
@Swatinem Swatinem changed the title fix: Use avoid using strncat and a trailing newline in logs fix: Avoid using strncat and a trailing newline in logs Jun 17, 2020
@Swatinem Swatinem merged commit c12c838 into master Jun 17, 2020
@Swatinem Swatinem deleted the fix/logger-nl branch June 17, 2020 08:44
irov pushed a commit to irov/sentry-native that referenced this pull request Jun 17, 2020
And avoid a trailing newline for custom loggers.

fixes getsentry#298
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.

Remove final new line from message in sentry_options_set_logger
3 participants