From: Christian Kastner <ckk@kvr.at>
Date: Tue, 22 Dec 2015 18:53:23 +0100
Subject: Proper use of error functions

perror is called after library functions besides the one that failed,
and without saving/restoring errno or otherwise referencing the
correct value.

Fix provided by Justin Pryzby <justinpryzby@users.sourceforge.net>.

Bug-Debian: https://bugs.debian/org/470587
Forwarded: no
Last Update: 2015-12-22
Index: cron/do_command.c
===================================================================
--- cron.orig/do_command.c
+++ cron/do_command.c
@@ -245,8 +245,7 @@ child_process(e, u)
 			}
 # endif /*DEBUGGING*/
 			execle(shell, shell, "-c", e->cmd, (char *)0, e->envp);
-			fprintf(stderr, "execl: couldn't exec `%s'\n", shell);
-			perror("execl");
+			fprintf(stderr, "%s: execle: %s\n", shell, strerror(errno));
 			_exit(ERROR_EXIT);
 		}
 		break;
Index: cron/misc.c
===================================================================
--- cron.orig/misc.c
+++ cron/misc.c
@@ -204,8 +204,7 @@ set_cron_cwd()
 			fprintf(stderr, "%s: created\n", CRONDIR);
 			stat(CRONDIR, &sb);
 		} else {
-			fprintf(stderr, "%s: ", CRONDIR);
-			perror("mkdir");
+			fprintf(stderr, "%s: mkdir: %s\n", CRONDIR, strerror(errno));
 			exit(ERROR_EXIT);
 		}
 	}
@@ -215,8 +214,7 @@ set_cron_cwd()
 		exit(ERROR_EXIT);
 	}
 	if (chdir(CRONDIR) < OK) {
-		fprintf(stderr, "cannot chdir(%s), bailing out.\n", CRONDIR);
-		perror(CRONDIR);
+		fprintf(stderr, "%s: chdir: %s\n", CRONDIR, strerror(errno));
 		exit(ERROR_EXIT);
 	}
 
@@ -228,8 +226,7 @@ set_cron_cwd()
 			fprintf(stderr, "%s: created\n", SPOOL_DIR);
 			stat(SPOOL_DIR, &sb);
 		} else {
-			fprintf(stderr, "%s: ", SPOOL_DIR);
-			perror("mkdir");
+			fprintf(stderr, "%s: mkdir: %s\n", SPOOL_DIR, strerror(errno));
 			exit(ERROR_EXIT);
 		}
 	}
@@ -512,9 +509,8 @@ log_it(username, xpid, event, detail)
 	if (LogFD < OK) {
 		LogFD = open(LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, 0600);
 		if (LogFD < OK) {
-			fprintf(stderr, "%s: can't open log file\n",
-				ProgramName);
-			perror(LOG_FILE);
+			fprintf(stderr, "%s: %s: open: %s\n",
+				ProgramName, LOG_FILE, strerror(errno));
 		} else {
 			(void) fcntl(LogFD, F_SETFD, 1);
 		}
Index: cron/crontab.c
===================================================================
--- cron.orig/crontab.c
+++ cron/crontab.c
@@ -265,8 +265,9 @@ list_cmd() {
 	if (!(f = fopen(n, "r"))) {
 		if (errno == ENOENT)
 			fprintf(stderr, "no crontab for %s\n", User);
-		else
-			perror(n);
+		else {
+			fprintf(stderr, "%s/: fopen: %s\n", n, strerror(errno));
+		}
 		exit(ERROR_EXIT);
 	}
 
@@ -288,8 +289,9 @@ delete_cmd() {
 	if (unlink(n)) {
 		if (errno == ENOENT)
 			fprintf(stderr, "no crontab for %s\n", User);
-		else
-			perror(n);
+		else {
+			fprintf(stderr, "%s/: unlink: %s\n", CRONDIR, strerror(errno));
+		}
 		exit(ERROR_EXIT);
 	}
 	poke_daemon();
@@ -319,7 +321,7 @@ edit_cmd() {
 	(void) snprintf(n, MAX_FNAME, CRON_TAB(User));
 	if (!(f = fopen(n, "r"))) {
 		if (errno != ENOENT) {
-			perror(n);
+			fprintf(stderr, "%s/: fdopen: %s", n, strerror(errno));
 			exit(ERROR_EXIT);
 		}
 		fprintf(stderr, "no crontab for %s - using an empty one\n",
@@ -542,8 +544,8 @@ replace_cmd() {
 	fflush(tmp);  rewind(tmp);
 
 	if (ferror(tmp)) {
-		fprintf(stderr, "%s: error while writing new crontab to %s\n",
-			ProgramName, tn);
+		fprintf(stderr, "%s: %s: %s\n",
+			ProgramName, tn, strerror(errno));
 		fclose(tmp);  unlink(tn);
 		return (-2);
 	}
@@ -617,9 +619,8 @@ replace_cmd() {
 
 	(void) snprintf(n, sizeof(n), CRON_TAB(User));
 	if (rename(tn, n)) {
-		fprintf(stderr, "%s: error renaming %s to %s\n",
-			ProgramName, tn, n);
-		perror("rename");
+		fprintf(stderr, "%s: %s: rename: %s\n",
+			ProgramName, n, strerror(errno));
 		unlink(tn);
 		return (-2);
 	}
@@ -640,14 +641,14 @@ poke_daemon() {
 	(void) gettimeofday(&tvs[0], &tz);
 	tvs[1] = tvs[0];
 	if (utimes(SPOOL_DIR, tvs) < OK) {
-		fprintf(stderr, "crontab: can't update mtime on spooldir\n");
-		perror(SPOOL_DIR);
+		fprintf(stderr, "%s/: utimes: %s", CRONDIR, strerror(errno));
+		fputs("crontab: can't update mtime on spooldir\n", stderr);
 		return;
 	}
 #else
 	if (utime(SPOOL_DIR, NULL) < OK) {
-		fprintf(stderr, "crontab: can't update mtime on spooldir\n");
-		perror(SPOOL_DIR);
+		fprintf(stderr, "%s: utime: %s\n", CRONDIR, strerror(errno));
+		fputs("crontab: can't update mtime on spooldir\n", stderr);
 		return;
 	}
 #endif /*USE_UTIMES*/
