From e2a368995a1373f9bd14d34997f119346f009a72 Mon Sep 17 00:00:00 2001 From: obrien Date: Sat, 13 Apr 2002 10:13:39 +0000 Subject: Make str_concat handle NULL arguments properly (a-la how ODE-2.3.6 make does). Submitted by: jmallett Inspired by: CMU BUILDTOOLS4 coredumping, ODE-2.3.6 make(1) --- usr.bin/make/str.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/make/str.c b/usr.bin/make/str.c index d4f84ea..ad7f864 100644 --- a/usr.bin/make/str.c +++ b/usr.bin/make/str.c @@ -96,31 +96,32 @@ str_concat(s1, s2, flags) char *result; /* get the length of both strings */ - len1 = strlen(s1); - len2 = strlen(s2); + len1 = s1 == NULL ? 0 : strlen(s1); + len2 = s2 == NULL ? 0 : strlen(s2); /* allocate length plus separator plus EOS */ result = emalloc((u_int)(len1 + len2 + 2)); /* copy first string into place */ - memcpy(result, s1, len1); + if (len1) + memcpy(result, s1, len1); /* add separator character */ - if (flags & STR_ADDSPACE) { - result[len1] = ' '; - ++len1; - } else if (flags & STR_ADDSLASH) { - result[len1] = '/'; - ++len1; + if (len1 && len2) { + if (flags & STR_ADDSPACE) + result[len1++] = ' '; + else if (flags & STR_ADDSLASH) + result[len1++] = '/'; } /* copy second string plus EOS into place */ - memcpy(result + len1, s2, len2 + 1); + if (len2) + memcpy(result + len1, s2, len2 + 1); /* free original strings */ if (flags & STR_DOFREE) { - (void)free(__DECONST(void *, s1)); - (void)free(__DECONST(void *, s2)); + (void)efree(__DECONST(void *, s1)); + (void)efree(__DECONST(void *, s2)); } return(result); } -- cgit v1.1