From 8e40ecb049dbf63e398417491b0f23afbc3fee7a Mon Sep 17 00:00:00 2001 From: obrien Date: Sat, 13 Apr 2002 10:13:39 +0000 Subject: [PATCH] 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(-) diff --git a/usr.bin/make/str.c b/usr.bin/make/str.c index d4f84ea6e438..ad7f864d52ab 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); }