From 6b88b740f6b63731064e1d5267c057a6a86e7cf6 Mon Sep 17 00:00:00 2001 From: Markus Uhlin Date: Sun, 7 Jul 2024 12:54:02 +0200 Subject: Added parameter 'size' to psprintf_highlight() and replaced unbounded string handling functions --- FICS/comproc.c | 11 ++++++----- FICS/utils.c | 22 ++++++++++++---------- FICS/utils.h | 4 ++-- 3 files changed, 20 insertions(+), 17 deletions(-) (limited to 'FICS') diff --git a/FICS/comproc.c b/FICS/comproc.c index c778502..405a59f 100644 --- a/FICS/comproc.c +++ b/FICS/comproc.c @@ -845,8 +845,8 @@ who_terse(int p, int num, int *plist, int type) } if (p == p1) { - psprintf_highlight(p, ptmp + strlen(ptmp), "%s", - parray[p1].name); + psprintf_highlight(p, ptmp + strlen(ptmp), + sizeof ptmp - strlen(ptmp), "%s", parray[p1].name); } else { strlcat(ptmp, parray[p1].name, sizeof ptmp); } @@ -909,8 +909,8 @@ who_verbose(int p, int num, int plist[]) if (p == p1) { strlcpy(tmp, " ", sizeof tmp); - psprintf_highlight(p, tmp + strlen(tmp), "%-17s", - p1WithAttrs); + psprintf_highlight(p, tmp + strlen(tmp), + sizeof tmp - strlen(tmp), "%-17s", p1WithAttrs); } else { ret = snprintf(tmp, sizeof tmp, " %-17s", p1WithAttrs); @@ -968,7 +968,8 @@ who_winloss(int p, int num, int plist[]) p1WithAttrs[17] = '\0'; if (p1 == p) { - psprintf_highlight(p, playerLine, "%-17s", p1WithAttrs); + psprintf_highlight(p, playerLine, sizeof playerLine, + "%-17s", p1WithAttrs); } else { snprintf(playerLine, sizeof playerLine, "%-17s", p1WithAttrs); diff --git a/FICS/utils.c b/FICS/utils.c index e81781f..29fe60c 100644 --- a/FICS/utils.c +++ b/FICS/utils.c @@ -315,35 +315,37 @@ pprintf_highlight(int p, char *format, ...) } PRIVATE void -sprintf_dohightlight(int p, char *s) +sprintf_dohightlight(int p, char *s, size_t size) { if (parray[p].highlight & 0x01) - strcat(s, "\033[7m"); + strlcat(s, "\033[7m", size); if (parray[p].highlight & 0x02) - strcat(s, "\033[1m"); + strlcat(s, "\033[1m", size); if (parray[p].highlight & 0x04) - strcat(s, "\033[4m"); + strlcat(s, "\033[4m", size); if (parray[p].highlight & 0x08) - strcat(s, "\033[2m"); + strlcat(s, "\033[2m", size); } PUBLIC int -psprintf_highlight(int p, char *s, char *format, ...) +psprintf_highlight(int p, char *s, size_t size, char *format, ...) { int retval; va_list ap; if (parray[p].highlight) { - sprintf_dohightlight(p, s); + char tmp[1000] = { '\0' }; va_start(ap, format); - retval = vsprintf(s + strlen(s), format, ap); + retval = vsnprintf(tmp, sizeof tmp, format, ap); va_end(ap); - strcat(s, "\033[0m"); + sprintf_dohightlight(p, s, size); + strlcat(s, tmp, size); + strlcat(s, "\033[0m", size); } else { va_start(ap, format); - retval = vsprintf(s, format, ap); + retval = vsnprintf(s, size, format, ap); va_end(ap); } diff --git a/FICS/utils.h b/FICS/utils.h index c6a673b..88f3cc9 100644 --- a/FICS/utils.h +++ b/FICS/utils.h @@ -96,8 +96,8 @@ extern int psend_command(int, char *, char *); extern int psend_file(int, char *, char *); extern int psend_logoutfile(int, char *, char *); extern int psend_raw_file(int, char *, char *); -extern int psprintf_highlight(int, char *, char *, ...) - PRINTFLIKE(3); +extern int psprintf_highlight(int, char *, size_t, char *, ...) + PRINTFLIKE(4); extern int safechar(int); extern int safestring(char *); extern int search_directory(char *, char *, char **, int); -- cgit v1.2.3