From ebdec4bcefe286c6b322bf26aa48b32aac6b57e5 Mon Sep 17 00:00:00 2001 From: Markus Uhlin Date: Sat, 13 Apr 2024 16:39:59 +0200 Subject: My utils --- FICS/build.mk | 1 + FICS/maxxes-utils.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ FICS/maxxes-utils.h | 22 ++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 FICS/maxxes-utils.c create mode 100644 FICS/maxxes-utils.h diff --git a/FICS/build.mk b/FICS/build.mk index 321cb87..e807dec 100644 --- a/FICS/build.mk +++ b/FICS/build.mk @@ -18,6 +18,7 @@ OBJS = $(SRC_DIR)adminproc.o\ $(SRC_DIR)legal.o\ $(SRC_DIR)lists.o\ $(SRC_DIR)matchproc.o\ + $(SRC_DIR)maxxes-utils.o\ $(SRC_DIR)movecheck.o\ $(SRC_DIR)multicol.o\ $(SRC_DIR)network.o\ diff --git a/FICS/maxxes-utils.c b/FICS/maxxes-utils.c new file mode 100644 index 0000000..7db8369 --- /dev/null +++ b/FICS/maxxes-utils.c @@ -0,0 +1,46 @@ +// SPDX-FileCopyrightText: 2024 Markus Uhlin +// SPDX-License-Identifier: ISC + +#include +#include +#include +#include + +#if __linux__ +#include +#endif + +#include "maxxes-utils.h" + +void +snprintf_trunc_chk(const char *file, const long int line, + char *str, size_t size, const char *format, ...) +{ + int ret; + va_list ap; + + va_start(ap, format); + ret = vsnprintf(str, size, format, ap); + va_end(ap); + + if (ret < 0 || (size_t)ret >= size) + warnx("%s:%ld: warning: vsnprintf() truncated", file, line); +} + +void +strlcpy_trunc_chk(char *dst, const char *src, size_t dstsize, + const char *file, + const long int line) +{ + if (strlcpy(dst, src, dstsize) >= dstsize) + warnx("%s:%ld: warning: strlcpy() truncated", file, line); +} + +void +strlcat_trunc_chk(char *dst, const char *src, size_t dstsize, + const char *file, + const long int line) +{ + if (strlcat(dst, src, dstsize) >= dstsize) + warnx("%s:%ld: warning: strlcat() truncated", file, line); +} diff --git a/FICS/maxxes-utils.h b/FICS/maxxes-utils.h new file mode 100644 index 0000000..667151f --- /dev/null +++ b/FICS/maxxes-utils.h @@ -0,0 +1,22 @@ +#ifndef MAXXES_UTILITIES_H +#define MAXXES_UTILITIES_H + +#include + +#include "common.h" + +#define msnprintf(p_str, p_size, ...) \ + snprintf_trunc_chk(__FILE__, __LINE__, (p_str), (p_size), __VA_ARGS__) +#define mstrlcpy(p_dst, p_src, p_dstsize) \ + strlcpy_trunc_chk((p_dst), (p_src), (p_dstsize), __FILE__, __LINE__) +#define mstrlcat(p_dst, p_src, p_dstsize) \ + strlcat_trunc_chk((p_dst), (p_src), (p_dstsize), __FILE__, __LINE__) + +void snprintf_trunc_chk(const char *file, const long int line, + char *str, size_t size, const char *format, ...) PRINTFLIKE(5); +void strlcpy_trunc_chk(char *dst, const char *src, size_t dstsize, + const char *, const long int); +void strlcat_trunc_chk(char *dst, const char *src, size_t dstsize, + const char *, const long int); + +#endif -- cgit v1.2.3