From aa05c470b37cbb8257b5c374cf9e8d1e6b13bcfe Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Mon, 19 Feb 2024 00:22:45 +0200 Subject: [PATCH] string fixes --- python/src/ustreamer.c | 2 +- src/libs/logging.h | 2 +- src/libs/threading.h | 14 +++++++------- src/libs/tools.h | 5 +++-- src/ustreamer/encoder.c | 2 +- src/ustreamer/http/bev.c | 2 +- src/ustreamer/http/server.c | 4 ++-- src/ustreamer/http/static.c | 2 +- src/ustreamer/http/static.h | 1 + 9 files changed, 18 insertions(+), 16 deletions(-) diff --git a/python/src/ustreamer.c b/python/src/ustreamer.c index f1d27f091..3ccc432ca 100644 --- a/python/src/ustreamer.c +++ b/python/src/ustreamer.c @@ -99,7 +99,7 @@ static int _MemsinkObject_init(_MemsinkObject *self, PyObject *args, PyObject *k static PyObject *_MemsinkObject_repr(_MemsinkObject *self) { char repr[1024]; - snprintf(repr, 1023, "", self->obj); + US_SNPRINTF(repr, 1023, "", self->obj); return Py_BuildValue("s", repr); } diff --git a/src/libs/logging.h b/src/libs/logging.h index 311285f3a..c7e3d5223 100644 --- a/src/libs/logging.h +++ b/src/libs/logging.h @@ -91,7 +91,7 @@ extern pthread_mutex_t us_g_log_mutex; #define US_LOG_PRINTF_NOLOCK(x_label_color, x_label, x_msg_color, x_msg, ...) { \ - char m_tname_buf[US_MAX_THREAD_NAME] = {0}; \ + char m_tname_buf[US_THREAD_NAME_SIZE] = {0}; \ us_thread_get_name(m_tname_buf); \ if (us_g_log_colored) { \ fprintf(stderr, US_COLOR_GRAY "-- " x_label_color x_label US_COLOR_GRAY \ diff --git a/src/libs/threading.h b/src/libs/threading.h index d63e187ca..db776b107 100644 --- a/src/libs/threading.h +++ b/src/libs/threading.h @@ -41,9 +41,9 @@ #ifdef PTHREAD_MAX_NAMELEN_NP -# define US_MAX_THREAD_NAME ((size_t)(PTHREAD_MAX_NAMELEN_NP)) +# define US_THREAD_NAME_SIZE ((size_t)(PTHREAD_MAX_NAMELEN_NP)) #else -# define US_MAX_THREAD_NAME ((size_t)16) +# define US_THREAD_NAME_SIZE ((size_t)16) #endif #define US_THREAD_CREATE(x_tid, x_func, x_arg) assert(!pthread_create(&(x_tid), NULL, (x_func), (x_arg))) @@ -51,8 +51,8 @@ #ifdef WITH_PTHREAD_NP # define US_THREAD_RENAME(x_fmt, ...) { \ - char m_new_tname_buf[US_MAX_THREAD_NAME] = {0}; \ - assert(snprintf(m_new_tname_buf, US_MAX_THREAD_NAME, (x_fmt), ##__VA_ARGS__) > 0); \ + char m_new_tname_buf[US_THREAD_NAME_SIZE] = {0}; \ + US_SNPRINTF(m_new_tname_buf, (US_THREAD_NAME_SIZE - 1), (x_fmt), ##__VA_ARGS__); \ us_thread_set_name(m_new_tname_buf); \ } #else @@ -89,12 +89,12 @@ INLINE void us_thread_get_name(char *name) { // Always required for logging #ifdef WITH_PTHREAD_NP int retval = -1; # if defined(__linux__) || defined (__NetBSD__) - retval = pthread_getname_np(pthread_self(), name, US_MAX_THREAD_NAME); + retval = pthread_getname_np(pthread_self(), name, US_THREAD_NAME_SIZE - 1); # elif \ (defined(__FreeBSD__) && defined(__FreeBSD_version) && __FreeBSD_version >= 1103500) \ || (defined(__OpenBSD__) && defined(OpenBSD) && OpenBSD >= 201905) \ || defined(__DragonFly__) - pthread_get_name_np(pthread_self(), name, US_MAX_THREAD_NAME); + pthread_get_name_np(pthread_self(), name, US_THREAD_NAME_SIZE - 1); if (name[0] != '\0') { retval = 0; } @@ -118,7 +118,7 @@ INLINE void us_thread_get_name(char *name) { // Always required for logging const pid_t tid = 0; // Makes cppcheck happy # warning gettid() not implemented #endif - assert(snprintf(name, US_MAX_THREAD_NAME, "tid=%d", tid) > 0); + US_SNPRINTF(name, (US_THREAD_NAME_SIZE - 1), "tid=%d", tid); #ifdef WITH_PTHREAD_NP } diff --git a/src/libs/tools.h b/src/libs/tools.h index 664856a93..59a5c6cb9 100644 --- a/src/libs/tools.h +++ b/src/libs/tools.h @@ -62,7 +62,8 @@ #define US_DELETE(x_dest, x_free) { if (x_dest) { x_free(x_dest); } } #define US_MEMSET_ZERO(x_obj) memset(&(x_obj), 0, sizeof(x_obj)) -#define US_ASPRINTF(x_dest, x_fmt, ...) assert(asprintf(&(x_dest), (x_fmt), ##__VA_ARGS__) >= 0) +#define US_SNPRINTF(x_dest, x_size, x_fmt, ...) assert(snprintf((x_dest), (x_size), (x_fmt), ##__VA_ARGS__) > 0) +#define US_ASPRINTF(x_dest, x_fmt, ...) assert(asprintf(&(x_dest), (x_fmt), ##__VA_ARGS__) > 0) INLINE char *us_strdup(const char *str) { @@ -180,7 +181,7 @@ INLINE char *us_errno_to_string(int error) { const size_t max_len = sizeof(buf) - 1; # if (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE if (strerror_r(error, buf, max_len) != 0) { - assert(snprintf(buf, max_len, "Errno = %d", error) > 0); + US_SNPRINTF(buf, max_len, "Errno = %d", error); } return us_strdup(buf); # else diff --git a/src/ustreamer/encoder.c b/src/ustreamer/encoder.c index 83e49e972..8b72c7cd5 100644 --- a/src/ustreamer/encoder.c +++ b/src/ustreamer/encoder.c @@ -120,7 +120,7 @@ us_workers_pool_s *us_encoder_workers_pool_init(us_encoder_s *enc, us_device_s * for (; _ER(n_m2ms) < n_workers; ++_ER(n_m2ms)) { // Начинаем с нуля и доинициализируем на следующих заходах при необходимости char name[32]; - snprintf(name, 32, "JPEG-%u", _ER(n_m2ms)); + US_SNPRINTF(name, 31, "JPEG-%u", _ER(n_m2ms)); if (type == US_ENCODER_TYPE_M2M_VIDEO) { _ER(m2ms[_ER(n_m2ms)]) = us_m2m_mjpeg_encoder_init(name, enc->m2m_path, quality); } else { diff --git a/src/ustreamer/http/bev.c b/src/ustreamer/http/bev.c index f6c6bf880..1ee285098 100644 --- a/src/ustreamer/http/bev.c +++ b/src/ustreamer/http/bev.c @@ -31,7 +31,7 @@ char *us_bufferevent_format_reason(short what) { char *const perror_str = us_errno_to_string(EVUTIL_SOCKET_ERROR()); bool first = true; - strcat(reason, perror_str); + strncat(reason, perror_str, 1023); free(perror_str); strcat(reason, " ("); diff --git a/src/ustreamer/http/server.c b/src/ustreamer/http/server.c index fdd882478..ad53ba4be 100644 --- a/src/ustreamer/http/server.c +++ b/src/ustreamer/http/server.c @@ -502,12 +502,12 @@ static void _http_callback_snapshot(struct evhttp_request *request, void *v_serv char header_buf[256]; # define ADD_TIME_HEADER(x_key, x_value) { \ - snprintf(header_buf, 255, "%.06Lf", x_value); \ + US_SNPRINTF(header_buf, 255, "%.06Lf", x_value); \ ADD_HEADER(x_key, header_buf); \ } # define ADD_UNSIGNED_HEADER(x_key, x_value) { \ - snprintf(header_buf, 255, "%u", x_value); \ + US_SNPRINTF(header_buf, 255, "%u", x_value); \ ADD_HEADER(x_key, header_buf); \ } diff --git a/src/ustreamer/http/static.c b/src/ustreamer/http/static.c index f06f2808b..d7fe5d200 100644 --- a/src/ustreamer/http/static.c +++ b/src/ustreamer/http/static.c @@ -33,7 +33,7 @@ char *us_find_static_file_path(const char *root_path, const char *request_path) } US_CALLOC(path, strlen(root_path) + strlen(simplified_path) + 16); // + reserved for /index.html - sprintf(path, "%s/%s", root_path, simplified_path); + assert(sprintf(path, "%s/%s", root_path, simplified_path) > 0); struct stat st; # define LOAD_STAT { \ diff --git a/src/ustreamer/http/static.h b/src/ustreamer/http/static.h index ddbc70ab8..00b2033ea 100644 --- a/src/ustreamer/http/static.h +++ b/src/ustreamer/http/static.h @@ -26,6 +26,7 @@ #include #include #include +#include #include