From 253df5b08a3b317eaf4a167361219cc2de05aa31 Mon Sep 17 00:00:00 2001 From: pst Date: Tue, 17 Dec 1996 00:55:20 +0000 Subject: Replace my "inane" usage of snprintf to copy strings with strncpy as used by OpenBSD. (Quite frankly, I think it's perfectly reasonable to use snprintf to copy strings, given that the semantics for strncpy() are utterly idiotic and there is no POSIX sstrncpy().) While I'm at it, incorporate some of OpenBSD's bugfixes to cron. NOT for 2.2 --- usr.sbin/cron/lib/compat.c | 15 +++++++++------ usr.sbin/cron/lib/env.c | 34 +++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 11 deletions(-) (limited to 'usr.sbin/cron/lib') diff --git a/usr.sbin/cron/lib/compat.c b/usr.sbin/cron/lib/compat.c index 87eeacc..eb69fcd 100644 --- a/usr.sbin/cron/lib/compat.c +++ b/usr.sbin/cron/lib/compat.c @@ -16,7 +16,7 @@ */ #if !defined(lint) && !defined(LINT) -static char rcsid[] = "$Id: compat.c,v 1.1.1.1 1994/08/27 13:43:02 jkh Exp $"; +static char rcsid[] = "$Id: compat.c,v 1.2 1996/11/01 23:27:28 millert Exp $"; #endif /* vix 30dec93 [broke this out of misc.c - see RCS log for history] @@ -53,7 +53,10 @@ strdup(str) { char *temp; - temp = malloc(strlen(str) + 1); + if ((temp = malloc(strlen(str) + 1)) == NULL) { + errno = ENOMEM; + return NULL; + } (void) strcpy(temp, str); return temp; } @@ -143,7 +146,7 @@ getdtablesize() { * Snarfage done by Jarkko Hietaniemi * *) well, almost, had to K&R the function entry, HPUX "cc" * does not grok ANSI function prototypes */ - + /* * flock (fd, operation) * @@ -199,13 +202,13 @@ flock(fd, operation) case LOCK_UN: /* unlock */ i = lockf (fd, F_ULOCK, 0); break; - + default: /* can't decipher operation */ i = -1; errno = EINVAL; break; } - + return (i); } #endif /*NEED_FLOCK*/ @@ -227,7 +230,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/usr.sbin/cron/lib/env.c b/usr.sbin/cron/lib/env.c index f370601..6537262 100644 --- a/usr.sbin/cron/lib/env.c +++ b/usr.sbin/cron/lib/env.c @@ -16,7 +16,7 @@ */ #if !defined(lint) && !defined(LINT) -static char rcsid[] = "$Id: env.c,v 1.1.1.1 1994/08/27 13:43:02 jkh Exp $"; +static char rcsid[] = "$Id: env.c,v 1.2 1996/12/16 18:21:00 pst Exp $"; #endif @@ -28,7 +28,8 @@ env_init() { register char **p = (char **) malloc(sizeof(char **)); - p[0] = NULL; + if (p) + p[0] = NULL; return (p); } @@ -55,8 +56,18 @@ env_copy(envp) for (count = 0; envp[count] != NULL; count++) ; p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */ + if (p == NULL) { + errno = ENOMEM; + return NULL; + } for (i = 0; i < count; i++) - p[i] = strdup(envp[i]); + if ((p[i] = strdup(envp[i])) == NULL) { + while (--i >= 0) + (void) free(p[i]); + free(p); + errno = ENOMEM; + return NULL; + } p[count] = NULL; return (p); } @@ -87,7 +98,11 @@ env_set(envp, envstr) * save our new one there, and return the existing array. */ free(envp[found]); - envp[found] = strdup(envstr); + if ((envp[found] = strdup(envstr)) == NULL) { + envp[found] = ""; + errno = ENOMEM; + return NULL; + } return (envp); } @@ -98,8 +113,15 @@ env_set(envp, envstr) */ p = (char **) realloc((void *) envp, (unsigned) ((count+1) * sizeof(char **))); + if (p == NULL) { + errno = ENOMEM; + return NULL; + } p[count] = p[count-1]; - p[count-1] = strdup(envstr); + if ((p[count-1] = strdup(envstr)) == NULL) { + errno = ENOMEM; + return NULL; + } return (p); } @@ -154,6 +176,8 @@ load_env(envstr, f) } } + if (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1) + return (FALSE); (void) sprintf(envstr, "%s=%s", name, val); Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr)) return (TRUE); -- cgit v1.1