rpcgen: make error() helper into a varargs func

Message ID 20170228202007.13529-1-vapier@gentoo.org
State New, archived
Headers

Commit Message

Mike Frysinger Feb. 28, 2017, 8:20 p.m. UTC
  A minor clean up to the code to avoid fixed buffers for output messages
and to make error() more useful internally.
---
 sunrpc/proto.h     |  2 +-
 sunrpc/rpc_parse.c | 13 ++-----------
 sunrpc/rpc_scan.c  | 13 +++----------
 sunrpc/rpc_util.c  | 28 ++++++++++++----------------
 sunrpc/rpc_util.h  |  2 +-
 5 files changed, 19 insertions(+), 39 deletions(-)
  

Comments

Mike Frysinger March 17, 2017, 4:41 a.m. UTC | #1
On 28 Feb 2017 13:20, Mike Frysinger wrote:
> A minor clean up to the code to avoid fixed buffers for output messages
> and to make error() more useful internally.

ping ?
-mike
  
Florian Weimer June 25, 2017, 4:14 p.m. UTC | #2
On 02/28/2017 09:20 PM, Mike Frysinger wrote:
> A minor clean up to the code to avoid fixed buffers for output messages
> and to make error() more useful internally.
> ---
>  sunrpc/proto.h     |  2 +-
>  sunrpc/rpc_parse.c | 13 ++-----------
>  sunrpc/rpc_scan.c  | 13 +++----------
>  sunrpc/rpc_util.c  | 28 ++++++++++++----------------
>  sunrpc/rpc_util.h  |  2 +-
>  5 files changed, 19 insertions(+), 39 deletions(-)

Patch is okay (with a ChangeLog entry).  This seems to fix a
user-visible bug (sprintf crash), so it probably deserves a bug in Bugzilla.

Thanks,
Florian
  

Patch

diff --git a/sunrpc/proto.h b/sunrpc/proto.h
index ea28565b1e6e..ff94fe214a2b 100644
--- a/sunrpc/proto.h
+++ b/sunrpc/proto.h
@@ -45,7 +45,7 @@  void write_tables(void);
 /****** rpc_util.c ******/
 void reinitialize(void);
 int streq(const char *a, const char *b);
-void error(const char *msg) __attribute__ ((noreturn));
+void error(const char *msg, ...) __attribute__ ((noreturn, __format__ (__printf__, 1, 2)));
 void crash(void) __attribute__ ((noreturn));
 void tabify(FILE *f, int tab);
 char *make_argname(const char *pname, const char *vname);
diff --git a/sunrpc/rpc_parse.c b/sunrpc/rpc_parse.c
index 505a6554cfa3..be2eb9bd2b4e 100644
--- a/sunrpc/rpc_parse.c
+++ b/sunrpc/rpc_parse.c
@@ -409,27 +409,18 @@  static void
 check_type_name (const char *name, int new_type)
 {
   int i;
-  char tmp[100];
 
   for (i = 0; reserved_words[i] != NULL; i++)
     {
       if (strcmp (name, reserved_words[i]) == 0)
-	{
-	  sprintf (tmp,
-		"illegal (reserved) name :\'%s\' in type definition", name);
-	  error (tmp);
-	}
+	error ("illegal (reserved) name :\'%s\' in type definition", name);
     }
   if (new_type)
     {
       for (i = 0; reserved_types[i] != NULL; i++)
 	{
 	  if (strcmp (name, reserved_types[i]) == 0)
-	    {
-	      sprintf (tmp,
-		"illegal (reserved) name :\'%s\' in type definition", name);
-	      error (tmp);
-	    }
+	    error ("illegal (reserved) name :\'%s\' in type definition", name);
 	}
     }
 }
diff --git a/sunrpc/rpc_scan.c b/sunrpc/rpc_scan.c
index a230aa396b37..95405687f30f 100644
--- a/sunrpc/rpc_scan.c
+++ b/sunrpc/rpc_scan.c
@@ -303,19 +303,12 @@  get_token (token *tokp)
       if (!(isalpha (*where) || *where == '_'))
 	{
 	  char buf[100];
-	  char *p;
 
-	  s_print (buf, _("illegal character in file: "));
-	  p = buf + strlen (buf);
 	  if (isprint (*where))
-	    {
-	      s_print (p, "%c", *where);
-	    }
+	    s_print (buf, "%c", *where);
 	  else
-	    {
-	      s_print (p, "%d", *where);
-	    }
-	  error (buf);
+	    s_print (buf, "%d", *where);
+	  error (_("illegal character in file: %s"), buf);
 	}
       findkind (&where, tokp);
       break;
diff --git a/sunrpc/rpc_util.c b/sunrpc/rpc_util.c
index 52aa69757b30..7e62f95f2ac6 100644
--- a/sunrpc/rpc_util.c
+++ b/sunrpc/rpc_util.c
@@ -35,6 +35,7 @@ 
  */
 #include <stdio.h>
 #include <ctype.h>
+#include <stdarg.h>
 #include <string.h>
 #include <unistd.h>
 #include "rpc_scan.h"
@@ -270,11 +271,16 @@  pvname (const char *pname, const char *vnum)
  * print a useful (?) error message, and then die
  */
 void
-error (const char *msg)
+error (const char *msg, ...)
 {
+  va_list arg;
+
   printwhere ();
   f_print (stderr, "%s, line %d: ", infilename, linenum);
-  f_print (stderr, "%s\n", msg);
+  va_start (arg, msg);
+  vfprintf (stderr, msg, arg);
+  f_print (stderr, "\n");
+  va_end (arg);
   crash ();
 }
 
@@ -308,17 +314,13 @@  record_open (const char *file)
     }
 }
 
-static char expectbuf[100];
-
 /*
  * error, token encountered was not the expected one
  */
 void
 expected1 (tok_kind exp1)
 {
-  s_print (expectbuf, "expected '%s'",
-	   toktostr (exp1));
-  error (expectbuf);
+  error ("expected '%s'", toktostr (exp1));
 }
 
 /*
@@ -327,10 +329,7 @@  expected1 (tok_kind exp1)
 void
 expected2 (tok_kind exp1, tok_kind exp2)
 {
-  s_print (expectbuf, "expected '%s' or '%s'",
-	   toktostr (exp1),
-	   toktostr (exp2));
-  error (expectbuf);
+  error ("expected '%s' or '%s'", toktostr (exp1), toktostr (exp2));
 }
 
 /*
@@ -339,11 +338,8 @@  expected2 (tok_kind exp1, tok_kind exp2)
 void
 expected3 (tok_kind exp1, tok_kind exp2, tok_kind exp3)
 {
-  s_print (expectbuf, "expected '%s', '%s' or '%s'",
-	   toktostr (exp1),
-	   toktostr (exp2),
-	   toktostr (exp3));
-  error (expectbuf);
+  error ("expected '%s', '%s' or '%s'", toktostr (exp1), toktostr (exp2),
+	 toktostr (exp3));
 }
 
 void
diff --git a/sunrpc/rpc_util.h b/sunrpc/rpc_util.h
index 53316d9516db..e46a5d2a605a 100644
--- a/sunrpc/rpc_util.h
+++ b/sunrpc/rpc_util.h
@@ -115,7 +115,7 @@  void pvname(const char *pname, const char *vnum);
 void ptype(const char *prefix, const char *type, int follow);
 int isvectordef(const char *type, relation rel);
 int streq(const char *a, const char *b);
-void error(const char *msg);
+void error(const char *msg, ...);
 void tabify(FILE *f, int tab);
 void record_open(const char *file);
 bas_type *find_type(const char *type);