Skip to content

Commit

Permalink
fix: Avoid using strncat in logger (#301)
Browse files Browse the repository at this point in the history
And avoid a trailing newline for custom loggers.

fixes #298
  • Loading branch information
Swatinem authored Jun 17, 2020
1 parent 1801f07 commit c12c838
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
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)

0 comments on commit c12c838

Please sign in to comment.