File: Miscellaneous-coding-errors.patch

package info (click to toggle)
cron 3.0pl1-198
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,824 kB
  • sloc: ansic: 54,879; xml: 1,600; perl: 733; sh: 495; makefile: 446; python: 43
file content (378 lines) | stat: -rw-r--r-- 9,663 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
From: Christian Kastner <ckk@kvr.at>
Date: Sun, 20 Dec 2015 13:22:36 +0100
Subject: Miscellaneous code errors

These are mostly one-line fixes of obvious errors such as running into
undefined behaviour, missing arguments to functions, typos, name mix-ups, etc.
Creating separate patches for them would be overkill.

Originally by Steve Greenland <stevegr@debian.org>, occasionally modified to
match what upstream eventually did for ISC cron v4.1.

Forwarded: no
Last-Update: 2015-12-20
---
 compat.c     |  2 +-
 cron.c       |  4 ++--
 cron.h       |  5 +++--
 crontab.c    | 20 +++++++++++++-------
 database.c   |  4 ++--
 do_command.c | 22 ++++++++++------------
 env.c        |  5 ++++-
 misc.c       | 26 ++++++++++++++------------
 popen.c      |  4 ++--
 user.c       |  1 +
 10 files changed, 52 insertions(+), 41 deletions(-)

diff --git a/compat.c b/compat.c
index 205c731..1c7eb2d 100644
--- a/compat.c
+++ b/compat.c
@@ -227,7 +227,7 @@ setenv(name, value, overwrite)
 		return -1;
 	}
 
-	sprintf("%s=%s", name, value);
+	sprintf(tmp, "%s=%s", name, value);
 	return putenv(tmp);	/* intentionally orphan 'tmp' storage */
 }
 #endif
diff --git a/cron.c b/cron.c
index 2753a44..c2a7d18 100644
--- a/cron.c
+++ b/cron.c
@@ -246,7 +246,7 @@ cron_sleep() {
 
 #ifdef USE_SIGCHLD
 static void
-sigchld_handler(x) {
+sigchld_handler(int x) {
 	WAIT_T		waiter;
 	PID_T		pid;
 
@@ -276,7 +276,7 @@ sigchld_handler(x) {
 
 
 static void
-sighup_handler(x) {
+sighup_handler(int x) {
 	log_close();
 }
 
diff --git a/cron.h b/cron.h
index ebe45c7..a71dc17 100644
--- a/cron.h
+++ b/cron.h
@@ -105,7 +105,7 @@
 
 #if DEBUGGING
 # define Debug(mask, message) \
-			if ( (DebugFlags & (mask) ) == (mask) ) \
+			if ( (DebugFlags & (mask) )  ) \
 				printf message;
 #else /* !DEBUGGING */
 # define Debug(mask, message) \
@@ -205,6 +205,7 @@ int		job_runqueue __P((void)),
 		get_char __P((FILE *)),
 		get_string __P((char *, int, FILE *, char *)),
 		swap_uids __P((void)),
+		swap_uids_back __P((void)),
 		load_env __P((char *, FILE *)),
 		cron_pclose __P((FILE *)),
 		strcmp_until __P((char *, char *, int)),
@@ -220,7 +221,7 @@ char		*env_get __P((char *, char **)),
 		**env_set __P((char **, char *));
 
 user		*load_user __P((int, struct passwd *, char *)),
-		*find_user __P((cron_db *, char *));
+		*find_user __P((cron_db *, const char *));
 
 entry		*load_entry __P((FILE *, void (*)(),
 				 struct passwd *, char **));
diff --git a/crontab.c b/crontab.c
index 657af71..d8aecba 100644
--- a/crontab.c
+++ b/crontab.c
@@ -57,7 +57,7 @@ static char	*Options[] = { "???", "list", "delete", "edit", "replace" };
 static	PID_T		Pid;
 static	char		User[MAX_UNAME], RealUser[MAX_UNAME];
 static	char		Filename[MAX_FNAME];
-static	FILE		*NewCrontab;
+static	FILE		*NewCrontab = NULL;
 static	int		CheckErrorCount;
 static	enum opt_t	Option;
 static	struct passwd	*pw;
@@ -124,8 +124,14 @@ main(argc, argv)
 	case opt_replace:	if (replace_cmd() < 0)
 					exitstatus = ERROR_EXIT;
 				break;
-	}
-	exit(0);
+				/* The following was added to shut
+				 -Wall up, but it will never be hit,
+				 because the option parser will catch
+				 it */
+	case opt_unknown: usage("unknown option specified");
+	                  break;
+	}
+	exit(exitstatus);
 	/*NOTREACHED*/
 }
 	
@@ -227,7 +233,7 @@ parse_args(argc, argv)
 				perror(Filename);
 				exit(ERROR_EXIT);
 			}
-			if (swap_uids() < OK) {
+			if (swap_uids_back() < OK) {
 				perror("swapping uids back");
 				exit(ERROR_EXIT);
 			}
@@ -473,7 +479,8 @@ edit_cmd() {
 			ProgramName, Filename);
 		goto done;
 	default:
-		fprintf(stderr, "%s: panic: bad switch() in replace_cmd()\n");
+		fprintf(stderr, "%s: panic: bad switch() in replace_cmd()\n",
+			ProgramName);
 		goto fatal;
 	}
  remove:
@@ -517,7 +524,6 @@ replace_cmd() {
 	Set_LineNum(1)
 	while (EOF != (ch = get_char(NewCrontab)))
 		putc(ch, tmp);
-	ftruncate(fileno(tmp), ftell(tmp));
 	fflush(tmp);  rewind(tmp);
 
 	if (ferror(tmp)) {
@@ -574,7 +580,7 @@ replace_cmd() {
 	if (chmod(tn, 0600) < OK)
 #endif
 	{
-		perror("chown");
+		perror("chmod");
 		fclose(tmp);  unlink(tn);
 		return (-2);
 	}
diff --git a/database.c b/database.c
index 879aaaa..67b71a0 100644
--- a/database.c
+++ b/database.c
@@ -189,8 +189,8 @@ unlink_user(db, u)
 
 user *
 find_user(db, name)
-	cron_db	*db;
-	char	*name;
+	cron_db		*db;
+	const char	*name;
 {
 	char	*env_get();
 	user	*u;
diff --git a/do_command.c b/do_command.c
index 11e157a..b8e32df 100644
--- a/do_command.c
+++ b/do_command.c
@@ -99,10 +99,10 @@ child_process(e, u)
 #ifdef USE_SIGCHLD
 	/* our parent is watching for our death by catching SIGCHLD.  we
 	 * do not care to watch for our children's deaths this way -- we
-	 * use wait() explictly.  so we have to disable the signal (which
+	 * use wait() explictly.  so we have to reset the signal (which
 	 * was inherited from the parent).
 	 */
-	(void) signal(SIGCHLD, SIG_IGN);
+	(void) signal(SIGCHLD, SIG_DFL);
 #else
 	/* on system-V systems, we are ignoring SIGCLD.  we have to stop
 	 * ignoring it now or the wait() in cron_pclose() won't work.
@@ -146,13 +146,13 @@ child_process(e, u)
 
 	/* fork again, this time so we can exec the user's command.
 	 */
-	switch (vfork()) {
+	switch (fork()) {
 	case -1:
-		log_it("CRON",getpid(),"error","can't vfork");
+		log_it("CRON",getpid(),"error","can't fork");
 		exit(ERROR_EXIT);
 		/*NOTREACHED*/
 	case 0:
-		Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n",
+		Debug(DPROC, ("[%d] grandchild process fork()'ed\n",
 			      getpid()))
 
 		/* write a log message.  we've waited this long to do it
@@ -169,9 +169,7 @@ child_process(e, u)
 
 		/* that's the last thing we'll log.  close the log files.
 		 */
-#ifdef SYSLOG
-		closelog();
-#endif
+		log_close();
 
 		/* get new pgrp, void tty, etc.
 		 */
@@ -189,9 +187,9 @@ child_process(e, u)
 		/* grandchild process.  make std{in,out} be the ends of
 		 * pipes opened by our daddy; make stderr go to stdout.
 		 */
-		close(STDIN);	dup2(stdin_pipe[READ_PIPE], STDIN);
-		close(STDOUT);	dup2(stdout_pipe[WRITE_PIPE], STDOUT);
-		close(STDERR);	dup2(STDOUT, STDERR);
+		dup2(stdin_pipe[READ_PIPE], STDIN);
+		dup2(stdout_pipe[WRITE_PIPE], STDOUT);
+		dup2(STDOUT, STDERR);
 
 		/* close the pipes we just dup'ed.  The resources will remain.
 		 */
@@ -331,7 +329,7 @@ child_process(e, u)
 		register int	ch = getc(in);
 
 		if (ch != EOF) {
-			register FILE	*mail;
+			register FILE	*mail = NULL;
 			register int	bytes = 1;
 			int		status = 0;
 
diff --git a/env.c b/env.c
index 7eb53fa..b788e75 100644
--- a/env.c
+++ b/env.c
@@ -39,6 +39,9 @@ env_free(envp)
 {
 	char	**p;
 
+	if(!envp)
+		return;
+
 	for (p = envp;  *p;  p++)
 		free(*p);
 	free(envp);
@@ -168,7 +171,7 @@ env_get(name, envp)
 	register int	len = strlen(name);
 	register char	*p, *q;
 
-	while (p = *envp++) {
+	while ((p = *envp++) != NULL) {
 		if (!(q = strchr(p, '=')))
 			continue;
 		if ((q - p) == len && !strncmp(p, name, len))
diff --git a/misc.c b/misc.c
index c912ffb..c03f25f 100644
--- a/misc.c
+++ b/misc.c
@@ -44,6 +44,10 @@ static char rcsid[] = "$Id: misc.c,v 2.9 1994/01/15 20:43:43 vixie Exp $";
 #define LOG_CRON LOG_DAEMON
 #endif
 
+#if defined(SYSLOG)
+	static int		syslog_open = 0;
+#endif
+
 
 static int		LogFD = ERR;
 
@@ -468,10 +472,6 @@ log_it(username, xpid, event, detail)
 	register struct tm	*t = localtime(&now);
 #endif /*LOG_FILE*/
 
-#if defined(SYSLOG)
-	static int		syslog_open = 0;
-#endif
-
 #if defined(LOG_FILE)
 	/* we assume that MAX_TEMPSTR will hold the date, time, &punctuation.
 	 */
@@ -514,11 +514,7 @@ log_it(username, xpid, event, detail)
 
 #if defined(SYSLOG)
 	if (!syslog_open) {
-		/* we don't use LOG_PID since the pid passed to us by
-		 * our client may not be our own.  therefore we want to
-		 * print the pid ourselves.
-		 */
-# ifdef LOG_DAEMON
+# ifdef LOG_CRON
 		openlog(ProgramName, LOG_PID, LOG_CRON);
 # else
 		openlog(ProgramName, LOG_PID);
@@ -526,14 +522,14 @@ log_it(username, xpid, event, detail)
 		syslog_open = TRUE;		/* assume openlog success */
 	}
 
-	syslog(LOG_INFO, "(%s) %s (%s)\n", username, event, detail);
+	syslog(LOG_INFO, "(%s) %s (%s)", username, event, detail);
 
 #endif /*SYSLOG*/
 
 #if DEBUGGING
 	if (DebugFlags) {
 		fprintf(stderr, "log_it: (%s %d) %s (%s)\n",
-			username, pid, event, detail);
+			username, xpid, event, detail);
 	}
 #endif
 }
@@ -541,10 +537,16 @@ log_it(username, xpid, event, detail)
 
 void
 log_close() {
+#if defined(LOG_FILE)
 	if (LogFD != ERR) {
 		close(LogFD);
 		LogFD = ERR;
 	}
+#endif
+#if defined(SYSLOG)
+	closelog();
+	syslog_open = FALSE;
+#endif
 }
 
 
@@ -654,7 +656,7 @@ arpadate(clock)
 #endif /*MAIL_DATE*/
 
 
-#ifdef HAVE_SAVED_SUIDS
+#ifdef HAVE_SAVED_UIDS
 static int save_euid;
 int swap_uids() { save_euid = geteuid(); return seteuid(getuid()); }
 int swap_uids_back() { return seteuid(save_euid); }
diff --git a/popen.c b/popen.c
index d6c2c75..ba59b97 100644
--- a/popen.c
+++ b/popen.c
@@ -58,7 +58,7 @@ cron_popen(program, type)
 	extern char **glob(), **copyblk();
 #endif
 
-	if (*type != 'r' && *type != 'w' || type[1])
+	if ((*type != 'r' && *type != 'w') || type[1])
 		return(NULL);
 
 	if (!pids) {
@@ -93,7 +93,7 @@ cron_popen(program, type)
 #endif
 
 	iop = NULL;
-	switch(pid = vfork()) {
+	switch(pid = fork()) {
 	case -1:			/* error */
 		(void)close(pdes[0]);
 		(void)close(pdes[1]);
diff --git a/user.c b/user.c
index 9672dd5..e010f74 100644
--- a/user.c
+++ b/user.c
@@ -75,6 +75,7 @@ load_user(crontab_fd, pw, name)
 	/*
 	 * load the crontab
 	 */
+	Set_LineNum(1)
 	while ((status = load_env(envstr, file)) >= OK) {
 		switch (status) {
 		case ERR: