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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/sentry_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,9 @@ sentry__logger_defaultlogger(
const char *prefix = "[sentry] ";
const char *priority = sentry__logger_describe(level);

size_t len = strlen(prefix) + strlen(priority) + strlen(message) + 1;
size_t len = strlen(prefix) + strlen(priority) + strlen(message) + 2;
char *format = sentry_malloc(len);

memset(format, '\0', len);

// Some compilers/tools, such as MSVC warn for using `strcpy` and friends.
// However, there are not really any good alternatives:
// * `strcpy_s` is only available on Windows
// * `strlcpy` is apparently a BSD thing
// * `strscpy` is a linux kernel thing
// * `strncpy` is not really a good choice either, but tools do not mark it
// as "unsafe".
strncpy(format, prefix, strlen(prefix));
strncat(format, priority, strlen(priority));
strncat(format, message, strlen(message));
snprintf("%s%s%s\n", len, prefix, priority, message);

vfprintf(stderr, format, args);

Expand Down
15 changes: 6 additions & 9 deletions src/sentry_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ const char *sentry__logger_describe(sentry_level_t level);
void sentry__logger_log(sentry_level_t level, const char *message, ...);

#define SENTRY_TRACEF(message, ...) \
sentry__logger_log(SENTRY_LEVEL_DEBUG, message "\n", __VA_ARGS__)
sentry__logger_log(SENTRY_LEVEL_DEBUG, message, __VA_ARGS__)

#define SENTRY_TRACE(message) \
sentry__logger_log(SENTRY_LEVEL_DEBUG, message "\n")
#define SENTRY_TRACE(message) sentry__logger_log(SENTRY_LEVEL_DEBUG, message)

#define SENTRY_DEBUGF(message, ...) \
sentry__logger_log(SENTRY_LEVEL_INFO, message "\n", __VA_ARGS__)
sentry__logger_log(SENTRY_LEVEL_INFO, message, __VA_ARGS__)

#define SENTRY_DEBUG(message) \
sentry__logger_log(SENTRY_LEVEL_INFO, message "\n")
#define SENTRY_DEBUG(message) sentry__logger_log(SENTRY_LEVEL_INFO, message)

#define SENTRY_WARNF(message, ...) \
sentry__logger_log(SENTRY_LEVEL_WARNING, message "\n", __VA_ARGS__)
sentry__logger_log(SENTRY_LEVEL_WARNING, message, __VA_ARGS__)

#define SENTRY_WARN(message) \
sentry__logger_log(SENTRY_LEVEL_WARNING, message "\n")
#define SENTRY_WARN(message) sentry__logger_log(SENTRY_LEVEL_WARNING, message)