Skip to content

Commit

Permalink
minor fixes to merge in
Browse files Browse the repository at this point in the history
  • Loading branch information
mjdominus committed Dec 23, 2024
1 parent d1a9e81 commit c471a74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
12 changes: 7 additions & 5 deletions cunit/util.testc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "util.h"
#include <malloc.h>

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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]);
}
}



21 changes: 14 additions & 7 deletions lib/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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", ...)
Expand Down Expand Up @@ -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
Expand All @@ -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':
Expand Down Expand Up @@ -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);
}

0 comments on commit c471a74

Please sign in to comment.