printf-hook-builtin: Support GNU %m specifier
authorMartin Willi <martin@revosec.ch>
Fri, 27 Sep 2013 09:16:11 +0000 (11:16 +0200)
committerMartin Willi <martin@revosec.ch>
Fri, 11 Oct 2013 09:06:09 +0000 (11:06 +0200)
src/libstrongswan/tests/test_printf.c
src/libstrongswan/utils/printf_hook/printf_hook_builtin.c

index 2e42475..60655de 100644 (file)
@@ -15,6 +15,8 @@
 
 #include "test_suite.h"
 
+#include <errno.h>
+
 static void verify(char *expected, char *format, ...)
 {
        FILE *mem;
@@ -45,6 +47,13 @@ START_TEST(test_printf_strings)
 }
 END_TEST
 
+START_TEST(test_printf_err)
+{
+       errno = EINVAL;
+       verify((char*)strerror(errno), "%m");
+}
+END_TEST
+
 START_TEST(test_printf_unsigned)
 {
        verify("1 23 456", "%u %lu %llu", 1, (u_long)23, (u_int64_t)456);
@@ -93,6 +102,10 @@ Suite *printf_suite_create()
        tcase_add_test(tc, test_printf_strings);
        suite_add_tcase(s, tc);
 
+       tc = tcase_create("err");
+       tcase_add_test(tc, test_printf_err);
+       suite_add_tcase(s, tc);
+
        tc = tcase_create("unsiged");
        tcase_add_test(tc, test_printf_unsigned);
        suite_add_tcase(s, tc);
index 5f49079..1c266d4 100644 (file)
@@ -46,6 +46,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <string.h>
+#include <errno.h>
 
 #define PRINTF_BUF_LEN 8192
 #define ARGS_MAX 3
@@ -682,6 +683,13 @@ int builtin_vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
                                                                slen = strlen(sarg);
                                                                goto is_string;
                                                        }
+                                                       case 'm':
+                                                       {
+                                                               /* glibc error string */
+                                                               sarg = strerror(errno);
+                                                               slen = strlen(sarg);
+                                                               goto is_string;
+                                                       }
                                                        is_string:
                                                        {
                                                                char sch;