Skip to content

Commit

Permalink
string fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevaev committed Feb 18, 2024
1 parent 13af11a commit aa05c47
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion python/src/ustreamer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<Memsink(%s)>", self->obj);
US_SNPRINTF(repr, 1023, "<Memsink(%s)>", self->obj);
return Py_BuildValue("s", repr);
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
14 changes: 7 additions & 7 deletions src/libs/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@


#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)))
#define US_THREAD_JOIN(x_tid) assert(!pthread_join((x_tid), NULL))

#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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions src/libs/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ustreamer/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/ustreamer/http/bev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, " (");

Expand Down
4 changes: 2 additions & 2 deletions src/ustreamer/http/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
}

Expand Down
2 changes: 1 addition & 1 deletion src/ustreamer/http/static.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 { \
Expand Down
1 change: 1 addition & 0 deletions src/ustreamer/http/static.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include <sys/stat.h>

Expand Down

0 comments on commit aa05c47

Please sign in to comment.