diff --git a/janus/src/logging.h b/janus/src/logging.h index 6f30181..56d0514 100644 --- a/janus/src/logging.h +++ b/janus/src/logging.h @@ -32,7 +32,7 @@ #define US_JLOG_ERROR(x_prefix, x_msg, ...) JANUS_LOG(LOG_ERR, "== %s/%-9s -- " x_msg "\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__) #define US_JLOG_PERROR(x_prefix, x_msg, ...) { \ - char m_perror_buf[1024] = {0}; \ - char *m_perror_ptr = us_errno_to_string(errno, m_perror_buf, 1023); \ - JANUS_LOG(LOG_ERR, "[%s/%-9s] " x_msg ": %s\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__, m_perror_ptr); \ + char *const m_perror_str = us_errno_to_string(errno); \ + JANUS_LOG(LOG_ERR, "[%s/%-9s] " x_msg ": %s\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__, m_perror_str); \ + free(m_perror_str); \ } diff --git a/src/libs/logging.h b/src/libs/logging.h index ea2a886..de0cf54 100644 --- a/src/libs/logging.h +++ b/src/libs/logging.h @@ -116,9 +116,9 @@ extern pthread_mutex_t us_g_log_mutex; } #define US_LOG_PERROR(x_msg, ...) { \ - char m_perror_buf[1024] = {0}; \ - char *m_perror_ptr = us_errno_to_string(errno, m_perror_buf, 1024); \ - US_LOG_ERROR(x_msg ": %s", ##__VA_ARGS__, m_perror_ptr); \ + char *const m_perror_str = us_errno_to_string(errno); \ + US_LOG_ERROR(x_msg ": %s", ##__VA_ARGS__, m_perror_str); \ + free(m_perror_str); \ } #define US_LOG_INFO(x_msg, ...) { \ @@ -149,9 +149,9 @@ extern pthread_mutex_t us_g_log_mutex; #define US_LOG_VERBOSE_PERROR(x_msg, ...) { \ if (us_g_log_level >= US_LOG_LEVEL_VERBOSE) { \ - char m_perror_buf[1024] = {0}; \ - char *m_perror_ptr = us_errno_to_string(errno, m_perror_buf, 1023); \ - US_LOG_PRINTF(US_COLOR_BLUE, "VERB ", US_COLOR_BLUE, x_msg ": %s", ##__VA_ARGS__, m_perror_ptr); \ + char *m_perror_str = us_errno_to_string(errno); \ + US_LOG_PRINTF(US_COLOR_BLUE, "VERB ", US_COLOR_BLUE, x_msg ": %s", ##__VA_ARGS__, m_perror_str); \ + free(m_perror_str); \ } \ } diff --git a/src/libs/tools.h b/src/libs/tools.h index 162493d..94facf2 100644 --- a/src/libs/tools.h +++ b/src/libs/tools.h @@ -187,15 +187,14 @@ INLINE int us_flock_timedwait_monotonic(int fd, long double timeout) { return retval; } -INLINE char *us_errno_to_string(int error, char *buf, size_t size) { - assert(buf != NULL); - assert(size > 0); +INLINE char *us_errno_to_string(int error) { locale_t locale = newlocale(LC_MESSAGES_MASK, "C", NULL); - const char *str = "!!! newlocale() error !!!"; - strncpy(buf, (locale ? strerror_l(error, locale) : str), size - 1); - buf[size - 1] = '\0'; + char *buf; if (locale) { + buf = us_strdup(strerror_l(error, locale)); freelocale(locale); + } else { + buf = us_strdup("!!! newlocale() error !!!"); } return buf; } diff --git a/src/ustreamer/http/bev.c b/src/ustreamer/http/bev.c index d5eb7af..5bbf0c6 100644 --- a/src/ustreamer/http/bev.c +++ b/src/ustreamer/http/bev.c @@ -28,11 +28,11 @@ char *us_bufferevent_format_reason(short what) { US_CALLOC(reason, 2048); // evutil_socket_error_to_string() is not thread-safe - char perror_buf[1024] = {0}; - const char *perror_ptr = us_errno_to_string(EVUTIL_SOCKET_ERROR(), perror_buf, 1024); + char *const perror_str = us_errno_to_string(EVUTIL_SOCKET_ERROR()); bool first = true; - strcat(reason, perror_ptr); + strcat(reason, perror_str); + free(perror_str); strcat(reason, " ("); # define FILL_REASON(x_bev, x_name) { \