This patch modifies lib/error.c so that should someone
call it without having set program_name, then a message
is printed and abort() called instead of seg faulting.
>From 70e49cdeba9becf444c6b6a6c6bd74e8fe6b80e4 Mon Sep 17 00:00:00 2001
From: Bruce Korb <bk...@gnu.org>
Date: Thu, 11 Nov 2010 08:46:00 -0800
Subject: [PATCH 2/3] give a sensible error message when error() is called without a set program name

---
 lib/error.c |   50 +++++++++++++++++++++++++++++---------------------
 1 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/lib/error.c b/lib/error.c
index ed9dba0..19065d8 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -106,7 +106,10 @@ char *strerror_r ();
 
 /* The calling program should define program_name and set it to the
    name of the executing program.  */
-extern char *program_name;
+#ifdef HPUX
+extern
+#endif
+char *program_name;
 
 # if HAVE_STRERROR_R || defined strerror_r
 #  define __strerror_r strerror_r
@@ -157,6 +160,29 @@ flush_stdout (void)
     fflush (stdout);
 }
 
+static inline void
+pr_prog_name (void)
+
+  if (error_print_progname)
+    (*error_print_progname) ();
+  else if (program_name == NULL)
+    {
+#if _LIBC
+      __fxprintf (NULL, "program_name not set\n");
+#else
+      fprintf (stderr, "program_name not set\n");
+#endif
+      abort ();
+    }
+  else
+    {
+#if _LIBC
+      __fxprintf (NULL, "%s: ", program_name);
+#else
+      fprintf (stderr, "%s: ", program_name);
+#endif
+    }
+
 static void
 print_errno_message (int errnum)
 {
@@ -296,16 +322,7 @@ error (int status, int errnum, const char *message, ...)
 #ifdef _LIBC
   _IO_flockfile (stderr);
 #endif
-  if (error_print_progname)
-    (*error_print_progname) ();
-  else
-    {
-#if _LIBC
-      __fxprintf (NULL, "%s: ", program_name);
-#else
-      fprintf (stderr, "%s: ", program_name);
-#endif
-    }
+  pr_prog_name ();
 
   va_start (args, message);
   error_tail (status, errnum, message, args);
@@ -355,16 +372,7 @@ error_at_line (int status, int errnum, const char *file_name,
 #ifdef _LIBC
   _IO_flockfile (stderr);
 #endif
-  if (error_print_progname)
-    (*error_print_progname) ();
-  else
-    {
-#if _LIBC
-      __fxprintf (NULL, "%s:", program_name);
-#else
-      fprintf (stderr, "%s:", program_name);
-#endif
-    }
+  pr_prog_name ();
 
 #if _LIBC
   __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
-- 
1.7.1

Reply via email to