Bug 2997

Summary: error_at_line vs. GNU Coding Standards
Product: glibc Reporter: Eric Blake <ebb9>
Component: libcAssignee: Ulrich Drepper <drepper.fsp>
Status: RESOLVED FIXED    
Severity: minor CC: glibc-bugs
Priority: P2 Flags: fweimer: security-
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:
Attachments: Proposed patch for case 1

Description Eric Blake 2006-08-03 22:25:46 UTC
In misc/error.c, using error_at_line does not always comply with GNU Coding 
Standards, which request that messages be in the form "program: message" 
or "program:file:line: message".

$ cat err.c
sh-3.00$ cat err.c
#include <error.h>
#include <stdio.h>

static int space;

static void
epp (void)
{
  fprintf (stderr, "%s:", "program");
  if (space)
    fputc (' ', stderr);
}

int
main(int argc, char** argv)
{
  if (argc > 1)
    {
      puts ("Using error_print_progname");
      error_print_progname = epp;
      if (*argv[1])
	{
	  puts ("Printing space");
	  space = 1;
	}
      else
	puts ("Omitting space");
    }
  else
    puts ("Letting error print progname");
  error(0, 0, "message1");
  error_at_line(0, 0, NULL, 0, "message2");
  error_at_line(0, 0, "file", 1, "message3");
  return 0;
}

$ # Case 1.
$ ./err
Letting error print progname
./err: message1
./err:message2
./err:file:1: message3
$ # Oops, missing a space before message2

$ # Case 2.
$ ./err ' '
Using error_print_progname
Printing space
program: message1
program: message2
program: file:1: message3
$ # Oops, extra space before filename

$ # Case 3.
$ ./err ''
Using error_print_progname
Omitting space
program:message1
program:message2
program:file:1: message3
$ # Oops, missing space before message1 and 2


I will attach a patch for case 1, when not using error_print_progname.

But when using error_print_progname (cases 2 and 3), I'm not sure how to tackle 
the issue.  One approach would be to document that error_print_progname should 
not append a space, and change error and error_at_line to unconditionally 
provide the space.  But for programs that want to use error_print_progname as a 
nop so that output looks like "file:line: message" for the sake of emacs 
compilation mode, this would break existing programs because error_at_line 
would then be outputting " file:line: message".

Another approach would be to add a new global variable, void 
(*error_print_progname_space)(int), and use the parameter as a flag to the the 
user's callback that if the callback prints to stderr, it should end with a 
space.  Then error would invoke error_print_progname_space(1), while 
error_at_line would invoke error_print_progname_space(file == NULL).
Comment 1 Eric Blake 2006-08-03 22:27:32 UTC
Created attachment 1194 [details]
Proposed patch for case 1
Comment 2 Eric Blake 2006-08-03 22:33:40 UTC
ChangeLog entry for libc.patch1:
2006-08-03  Eric Blake  <ebb9@byu.net>

	* error.c (error_at_line): When file is NULL, print space between
	progname and message.
Comment 3 Ulrich Drepper 2006-08-12 17:58:30 UTC
I added a different patch which leads to less code.