From e5a4647314d33ab329ddb5b546c15b01413afb7a Mon Sep 17 00:00:00 2001 From: Mark Jason Dominus Date: Thu, 19 Dec 2024 13:49:29 -0500 Subject: [PATCH] Final fixes to xsyslog_ev tests --- cunit/util.testc | 12 +++++++----- lib/util.c | 21 ++++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/cunit/util.testc b/cunit/util.testc index 60b01b4476..9e3d6b41e7 100644 --- a/cunit/util.testc +++ b/cunit/util.testc @@ -2,7 +2,7 @@ #include "util.h" #include -extern char *xsyslog_ev_escape_value(const char *); +extern const char *__test_xsyslog_ev_escape_value(struct buf *); extern char __test_xsyslog_ev_guess_printf_escape(const char *); static void test_xsyslog_ev_fmt(void) @@ -97,13 +97,14 @@ static void test_xsyslog_ev_escape_value(void) }; unsigned i; + struct buf in = BUF_INITIALIZER; for (i=0; test[i]; i += 2) { - const char *in = test[i]; + buf_setcstr(&in, test[i]); const char *expected = test[i+1]; - char *actual = __test_xsyslog_ev_escape_value(in); + const char *actual = __test_xsyslog_ev_escape_value(&in); CU_ASSERT_STRING_EQUAL(actual, expected); - free(actual); }; + buf_free(&in); } static void test_xsyslog_ev_guess_printf_escape(void) @@ -163,9 +164,10 @@ static void test_xsyslog_ev_guess_printf_escape(void) const char *in = test[i]; const char *expected = test[i+1]; char actual = __test_xsyslog_ev_guess_printf_escape(in); - if (actual != *expected) fprintf(stderr, "%s -> %c ? s/b %c \n", in, actual, *expected); + if (actual != *expected) fprintf(stderr, "%s -> %c ? s/b %c (%hd) \n", in, actual, *expected, *expected); CU_ASSERT_EQUAL(actual, expected[0]); } } + diff --git a/lib/util.c b/lib/util.c index 509bc38075..48456afd10 100644 --- a/lib/util.c +++ b/lib/util.c @@ -2194,8 +2194,9 @@ EXPORTED void xsyslog_fn(int priority, const char *description, errno = saved_errno; } -static char *xsyslog_ev_escape_value(struct buf *); -static void xsyslog_ev_fmt_skip_va(const char*, va_list); +static const char *xsyslog_ev_escape_value(struct buf *); +static void xsyslog_ev_fmt_skip_va(const char *, va_list); +static char *xsyslog_ev_fmt_va(const char *, va_list); static char xsyslog_ev_guess_printf_escape(const char *); /* xsyslog_ev: @@ -2349,8 +2350,8 @@ static void xsyslog_ev_fmt_skip_va(const char* fmt, va_list ap) case 'd': (void) va_arg(ap, int); break; case 'l': (void) va_arg(ap, long); break; case 'f': (void) va_arg(ap, double); break; - case 's': (void) va_arg(ap, (char *)); break; - case 'p': (void) va_arg(ap, (void *)); break; + case 's': (void) va_arg(ap, char *); break; + case 'p': (void) va_arg(ap, void *); break; case 0: // we could handle this by skipping the va_arg call entirely, which would allow the user to write // xsyslog_ev(..., key, "foo", ...) @@ -2380,8 +2381,9 @@ static void xsyslog_ev_fmt_skip_va(const char* fmt, va_list ap) static char xsyslog_ev_guess_printf_escape(const char *s) { unsigned is_long = 0; TOP: - while (*s++ != '%') ; // seek past first % sign - if (!*s) return 0; + while (*s && *s != '%') s++; // seek to first % sign or to end of string + if (*s == 0) return 0; // String has no percent signs at all + s++; if (*s == '%') { s++; goto TOP; } // it's "%%", skip it and continue switch (*s) { // skip optional flag @@ -2395,7 +2397,7 @@ static char xsyslog_ev_guess_printf_escape(const char *s) { if (*s == 'l') { is_long++; s++; } switch (*s) { case 'd': case 'i': case '0': case 'u': - case 'x': case 'X': case 'c': + case 'x': case 'X': case 'c': case 'o': return is_long ? 'l' : 'd'; case 'e': case 'E': case 'f': case 'F': @@ -2508,3 +2510,8 @@ static const char *xsyslog_ev_escape_value(struct buf *val) return buf_cstring(val); } + +EXPORTED const char *__test_xsyslog_ev_escape_value(struct buf *val) +{ + return xsyslog_ev_escape_value(val); +}