Package: asmail
Version: 2.0-1
Severity: normal
Tags: patch

The asmail.c file from the asmail source distribution
repeatedly contains the following two lines:

        printf(<some custom error message);
        perror("asmail: <routine name>");

For instance, as

        if ( ! tmp_pixfile ) {
                printf("asmail: parse_cfg_animate: failed to allocate memory 
for the pixfile\n");
                perror("asmail: parse_cfg_animate: ");
                return(-1);
        }

on line 262. This is wrong, because the printf may cause
errno to be set to a value different from the one
corresponding w/ the original failure, causing the
perror to print a wrong message. An especially nasty
variant of this is on lines 670 - 681:

        if ( stat(filename, &f_stat) ) {
                printf("asmail: Cannot stat the config file (%s).\n", filename);
                /* if the file does not exist, we still can run with
                 * the default settings using $MAIL unless a config
                 * file was given on command line. */
                if ( ! flag_config_specified )
                        if ( ENOENT == errno )
                                return(-2);
                perror("asmail: parse_cfg");
                return(-1);
        }
                
The intent is to treat a missing configuration file
as non-fatal error, ie to print a warning and to continue
processing after that. If the stat fails because
the configuration file does not exist and stdout is
redirected to /dev/null, an ioctl done internally as
part of the printf-processing causes errno to be
set to ENOTTY before the nested if below the printf
tests for errno == ENOENT. The program then terminates
itself because of the 'unexpected' error.

A patch that fixes this:

diff -rNu asmail-2.0.orig/asmail.c asmail-2.0/asmail.c
--- asmail-2.0.orig/asmail.c    2007-04-03 18:04:15.000000000 +0200
+++ asmail-2.0/asmail.c 2007-04-03 18:39:54.000000000 +0200
@@ -6,6 +6,7 @@
  * This software is distributed under GPL. For details see LICENSE file.
  */
 
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -52,6 +53,19 @@
        printf("\n");
 }
 
+static void err_printf(char const *tmpl, ...) {
+       va_list val;
+       int errno_;
+
+       errno_ = errno;
+       
+       va_start(val, tmpl);
+       vprintf(tmpl, val);
+       va_end(val);
+
+       errno = errno_;
+}
+
 /*
  * Set up the defaults that cannot be determined at compile time.
  */
@@ -260,7 +274,7 @@
                                tmp_pixfile = (struct pixfile *) 
                                        malloc(sizeof(struct pixfile));
                                if ( ! tmp_pixfile ) {
-                                       printf("asmail: parse_cfg_animate: 
failed to allocate memory for the pixfile\n");
+                                       err_printf("asmail: parse_cfg_animate: 
failed to allocate memory for the pixfile\n");
                                        perror("asmail: parse_cfg_animate: ");
                                        return(-1);
                                }
@@ -273,7 +287,7 @@
                                tmp_pixfile = (struct pixfile *) 
                                        malloc(sizeof(struct pixfile));
                                if ( ! tmp_pixfile ) {
-                                       printf("asmail: parse_cfg_animate: 
failed to allocate memory for the pixfile\n");
+                                       err_printf("asmail: parse_cfg_animate: 
failed to allocate memory for the pixfile\n");
                                        perror("asmail: parse_cfg_animate: ");
                                        return(-1);
                                }
@@ -286,7 +300,7 @@
                                tmp_pixfile = (struct pixfile *) 
                                        malloc(sizeof(struct pixfile));
                                if ( ! tmp_pixfile ) {
-                                       printf("asmail: parse_cfg_animate: 
failed to allocate memory for the pixfile\n");
+                                       err_printf("asmail: parse_cfg_animate: 
failed to allocate memory for the pixfile\n");
                                        perror("asmail: parse_cfg_animate: ");
                                        return(-1);
                                }
@@ -302,7 +316,7 @@
                                        tmp_pixfile = (struct pixfile *) 
                                                malloc(sizeof(struct pixfile));
                                        if ( ! tmp_pixfile ) {
-                                               printf("asmail: 
parse_cfg_animate: failed to allocate memory for the pixfile\n");
+                                               err_printf("asmail: 
parse_cfg_animate: failed to allocate memory for the pixfile\n");
                                                perror("asmail: 
parse_cfg_animate: ");
                                                return(-1);
                                        }
@@ -668,7 +682,7 @@
        char rest[MAX_INPUT_LENGTH+1];
 
        if ( stat(filename, &f_stat) ) {
-               printf("asmail: Cannot stat the config file (%s).\n", filename);
+               err_printf("asmail: Cannot stat the config file (%s).\n", 
filename);
                /* if the file does not exist, we still can run with
                 * the default settings using $MAIL unless a config
                 * file was given on command line. */
@@ -679,7 +693,7 @@
                return(-1);
        }
        if ( ( f = fopen(filename, "r") ) == NULL ) {
-               printf("asmail: Cannot open the config file (%s).\n", filename);
+               err_printf("asmail: Cannot open the config file (%s).\n", 
filename);
                perror("asmail: parse_cfg");
                return(-1);
        }
@@ -719,7 +733,7 @@
        }
 
        if ( fclose(f) ) {
-               printf("asmail: Cannot close the config file (%s).\n", 
filename);
+               err_printf("asmail: Cannot close the config file (%s).\n", 
filename);
                perror("asmail: parse_cfg");
                return(-1);
        }


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to