From 9956fe29ff9e9011bb03540801e213936736e1f1 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Mon, 4 Jun 2012 14:54:39 +1000 Subject: [PATCH] LOG: fix the printing of %p in the blackbox Signed-off-by: Angus Salkeld --- configure.ac | 10 ++++++---- include/os_base.h | 4 ++++ lib/log_format.c | 12 +++++++----- tests/check_log.c | 6 ++++++ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/configure.ac b/configure.ac index 06edc82..36a35f3 100644 --- a/configure.ac +++ b/configure.ac @@ -125,10 +125,12 @@ fi AC_HEADER_STDC AC_HEADER_SYS_WAIT -AC_CHECK_HEADERS([arpa/inet.h link.h fcntl.h inttypes.h limits.h netinet/in.h stdint.h \ - dlfcn.h time.h sys/time.h stdlib.h string.h strings.h sys/types.h sys/stat.h \ - sys/param.h sys/socket.h sys/time.h sys/poll.h sys/epoll.h sys/uio.h sys/event.h \ - sys/sockio.h sys/un.h sys/resource.h syslog.h errno.h unistd.h sys/mman.h \ +AC_CHECK_HEADERS([arpa/inet.h link.h fcntl.h inttypes.h limits.h netinet/in.h \ + stdint.h stddef.h stdlib.h string.h strings.h \ + dlfcn.h time.h sys/time.h sys/types.h sys/stat.h \ + sys/param.h sys/socket.h sys/time.h sys/poll.h sys/epoll.h \ + sys/uio.h sys/event.h sys/sockio.h sys/un.h sys/resource.h \ + syslog.h errno.h unistd.h sys/mman.h \ sys/sem.h sys/ipc.h sys/msg.h netdb.h]) # Checks for typedefs, structures, and compiler characteristics. diff --git a/include/os_base.h b/include/os_base.h index 600a95c..ea463c9 100644 --- a/include/os_base.h +++ b/include/os_base.h @@ -41,6 +41,10 @@ #include #endif /* HAVE_STDINT_H */ +#ifdef HAVE_STDDEF_H +#include +#endif /* HAVE_STDDEF_H */ + #ifdef HAVE_INTTYPES_H #include #endif diff --git a/lib/log_format.c b/lib/log_format.c index b5ae3f9..4a51d58 100644 --- a/lib/log_format.c +++ b/lib/log_format.c @@ -513,10 +513,9 @@ reprocess: } case 'p': { - void *arg_pointer; - arg_pointer = va_arg(ap, void *); - memcpy (&serialize[location], &arg_pointer, sizeof (void *)); - location += sizeof (arg_pointer); + ptrdiff_t arg_pointer = va_arg(ap, ptrdiff_t); + memcpy(&serialize[location], &arg_pointer, sizeof(ptrdiff_t)); + location += sizeof(ptrdiff_t); break; } case '%': @@ -698,11 +697,14 @@ reprocess: } case 'p': { + ptrdiff_t pt; + memcpy(&pt, &buf[data_pos], + sizeof(ptrdiff_t)); fmt[fmt_pos++] = *format; fmt[fmt_pos++] = '\0'; location += snprintf(&string[location], str_len - location, - fmt, &buf[data_pos]); + fmt, pt); data_pos += sizeof(void*); format++; break; diff --git a/tests/check_log.c b/tests/check_log.c index 33210b1..4e06370 100644 --- a/tests/check_log.c +++ b/tests/check_log.c @@ -49,9 +49,15 @@ format_this(char *out, const char *fmt, ...) START_TEST(test_va_serialize) { char buf[QB_LOG_MAX_LEN]; + char cmp_buf[QB_LOG_MAX_LEN]; + format_this(buf, "one line"); ck_assert_str_eq(buf, "one line"); + format_this(buf, "p1:%p, p2:%p", format_this, buf); + snprintf(cmp_buf, QB_LOG_MAX_LEN, "p1:%p, p2:%p", format_this, buf); + ck_assert_str_eq(buf, cmp_buf); + format_this(buf, "s1:%s, s2:%s", "Yes", "Never"); ck_assert_str_eq(buf, "s1:Yes, s2:Never");