tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

gcc syslog printf attribute



Background:

Our libc printf does not handle %m. glibc's printf does. The printf attribute
of gcc, assumes that %m is handled by printf. The following patch adds a
separate syslog attribute and includes %m in the accepted formats, and makes
the existing printf attribute warn if %m is present.

Here's the patch I am planning to commit unless you think this is a bad idea.

christos

Index: c-family/c-format.c
===================================================================
RCS file: /cvsroot/src/external/gpl3/gcc/dist/gcc/c-family/c-format.c,v
retrieving revision 1.1.1.1
diff -u -u -r1.1.1.1 c-format.c
--- c-family/c-format.c	1 Mar 2014 08:43:01 -0000	1.1.1.1
+++ c-family/c-format.c	13 Oct 2015 14:34:39 -0000
@@ -860,14 +860,20 @@
   },
   { "gnu_strftime", NULL,                 time_char_table,  "_-0^#", "EO",
     strftime_flag_specs, strftime_flag_pairs,
-    FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
+    FMT_FLAG_FANCY_PERCENT_OK|FMT_FLAG_M_OK, 'w', 0, 0, 0, 0, 0,
     NULL, NULL
   },
   { "gnu_strfmon",  strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
     strfmon_flag_specs, strfmon_flag_pairs,
     FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
     NULL, NULL
-  }
+  },
+  { "gnu_syslog",   printf_length_specs,  print_char_table, " +#0-'I", NULL,
+    printf_flag_specs, printf_flag_pairs,
+    FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK|FMT_FLAG_M_OK,
+    'w', 0, 'p', 0, 'L', 0,
+    &integer_type_node, &integer_type_node
+  },
 };
 
 /* This layer of indirection allows GCC to reassign format_types with
@@ -1983,6 +1989,13 @@
 	  warning (OPT_Wformat_, "conversion lacks type at end of format");
 	  continue;
 	}
+
+      if (format_char == 'm' && !(fki->flags & FMT_FLAG_M_OK))
+        {
+	  warning (OPT_Wformat_, "%%m is only allowed in syslog(3)");
+	  continue;
+	}
+	
       format_chars++;
       fci = fki->conversion_specs;
       while (fci->format_chars != 0
@@ -2854,6 +2867,7 @@
 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
 {
   { "gnu_printf",   "printf" },
+  { "gnu_syslog",   "syslog" },
   { "gnu_scanf",    "scanf" },
   { "gnu_strftime", "strftime" },
   { "gnu_strfmon",  "strfmon" },
Index: c-family/c-format.h
===================================================================
RCS file: /cvsroot/src/external/gpl3/gcc/dist/gcc/c-family/c-format.h,v
retrieving revision 1.1.1.1
diff -u -u -r1.1.1.1 c-format.h
--- c-family/c-format.h	1 Mar 2014 08:43:01 -0000	1.1.1.1
+++ c-family/c-format.h	13 Oct 2015 14:34:39 -0000
@@ -75,11 +75,13 @@
   FMT_FLAG_DOLLAR_GAP_POINTER_OK = 128,
   /* The format arg is an opaque object that will be parsed by an external
      facility.  */
-  FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL = 256
+  FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL = 256,
   /* Not included here: details of whether width or precision may occur
      (controlled by width_char and precision_char); details of whether
      '*' can be used for these (width_type and precision_type); details
      of whether length modifiers can occur (length_char_specs).  */
+  FMT_FLAG_M_OK = 512
+  /* %m is only allowed in syslog */
 };
 
 /* Structure describing a length modifier supported in format checking, and
Index: config/netbsd.h
===================================================================
RCS file: /cvsroot/src/external/gpl3/gcc/dist/gcc/config/netbsd.h,v
retrieving revision 1.14
diff -u -u -r1.14 netbsd.h
--- config/netbsd.h	22 Oct 2014 23:17:24 -0000	1.14
+++ config/netbsd.h	13 Oct 2015 14:34:39 -0000
@@ -23,6 +23,7 @@
     {						\
       builtin_define ("__NetBSD__");		\
       builtin_define ("__unix__");		\
+      builtin_define ("__syslog_attribute__");	\
       builtin_assert ("system=bsd");		\
       builtin_assert ("system=unix");		\
       builtin_assert ("system=NetBSD");		\


Home | Main Index | Thread Index | Old Index