1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
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
---
crontab.c | 29 +++++++++++++++--------------
do_command.c | 3 +--
misc.c | 14 +++++---------
3 files changed, 21 insertions(+), 25 deletions(-)
diff --git a/crontab.c b/crontab.c
index 3a7d8f1..a43d9ce 100644
--- a/crontab.c
+++ b/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*/
diff --git a/do_command.c b/do_command.c
index 6f6590b..f596252 100644
--- a/do_command.c
+++ b/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;
diff --git a/misc.c b/misc.c
index 93023e5..edf4ee6 100644
--- a/misc.c
+++ b/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);
}
|