[hackers] [scron] [PATCH 2/3] Handle out-of-memory

From: Michael Forney <mforney_AT_mforney.org>
Date: Mon, 10 Jul 2017 21:55:41 -0700

---
 crond.c | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)
diff --git a/crond.c b/crond.c
index 32a170e..7974b1f 100644
--- a/crond.c
+++ b/crond.c
_AT_@ -101,12 +101,8 @@ emalloc(size_t size)
 {
 	void *p;
 	p = malloc(size);
-	if (!p) {
+	if (!p)
 		logerr("error: out of memory\n");
-		if (nflag == 0)
-			unlink(pidfile);
-		exit(EXIT_FAILURE);
-	}
 	return p;
 }
 
_AT_@ -116,12 +112,8 @@ estrdup(const char *s)
 	char *p;
 
 	p = strdup(s);
-	if (!p) {
+	if (!p)
 		logerr("error: out of memory\n");
-		if (nflag == 0)
-			unlink(pidfile);
-		exit(EXIT_FAILURE);
-	}
 	return p;
 }
 
_AT_@ -157,7 +149,8 @@ runjob(char *cmd)
 		       cmd, ctime(&t));
 		_exit(EXIT_FAILURE);
 	} else {
-		je = emalloc(sizeof(*je));
+		if (!(je = emalloc(sizeof(*je))))
+			return;
 		je->cmd = estrdup(cmd);
 		je->pid = pid;
 		je->next = jobs;
_AT_@ -297,7 +290,8 @@ parsefield(const char *field, long low, long high, struct field *f)
 			f->len = 0;
 			f->type = WILDCARD;
 		} else if (strncmp(field, "*/", 2) == 0) {
-			f->val = emalloc(sizeof(*f->val));
+			if (!(f->val = emalloc(sizeof(*f->val))))
+				return -1;
 			f->len = 1;
 
 			errno = 0;
_AT_@ -309,7 +303,8 @@ parsefield(const char *field, long low, long high, struct field *f)
 		}
 		break;
 	case '\0':
-		f->val = emalloc(sizeof(*f->val));
+		if (!(f->val = emalloc(sizeof(*f->val))))
+			return -1;
 		f->len = 1;
 
 		errno = 0;
_AT_@ -320,7 +315,8 @@ parsefield(const char *field, long low, long high, struct field *f)
 		f->type = NUMBER;
 		break;
 	case '-':
-		f->val = emalloc(2 * sizeof(*f->val));
+		if (!(f->val = emalloc(2 * sizeof(*f->val))))
+			return -1;
 		f->len = 2;
 
 		errno = 0;
_AT_@ -339,7 +335,8 @@ parsefield(const char *field, long low, long high, struct field *f)
 		for (i = 1; isdigit(*p) || *p == ','; p++)
 			if (*p == ',')
 				i++;
-		f->val = emalloc(i * sizeof(*f->val));
+		if (!(f->val = emalloc(i * sizeof(*f->val))))
+			return -1;
 		f->len = i;
 
 		errno = 0;
_AT_@ -438,7 +435,10 @@ loadentries(void)
 		if (line[0] == '#' || line[0] == '\n' || line[0] == '\0')
 			continue;
 
-		cte = emalloc(sizeof(*cte));
+		if (!(cte = emalloc(sizeof(*cte)))) {
+			r = -1;
+			break;
+		}
 		flim[0].f = &cte->min;
 		flim[1].f = &cte->hour;
 		flim[2].f = &cte->mday;
_AT_@ -473,7 +473,11 @@ loadentries(void)
 			r = -1;
 			break;
 		}
-		cte->cmd = estrdup(col);
+		if (!(cte->cmd = estrdup(col))) {
+			freecte(cte, 5);
+			r = -1;
+			break;
+		}
 		cte->next = ctab;
 		ctab = cte;
 	}
-- 
2.13.2
Received on Tue Jul 11 2017 - 06:55:41 CEST

This archive was generated by hypermail 2.3.0 : Tue Jul 11 2017 - 07:01:27 CEST