Followup to this patch from 2014-12-09: A bit more consistent coding style: - The argument 'pathname' must be non-NULL. No need to test it against NULL. - Make use of ISSLASH macro consistently. - For characters, I find a comparison with '\0' clearer than a boolean negation. Limit boolean expressions to booleans. - Better use the memcpy-before-strcpy idiom than strcpy-before-strcpy. The former makes it clear that the programmer has thought about the length of the strings.
2017-05-16 Bruno Haible <br...@clisp.org> relocate: Simplify EMX specific code. * lib/relocatable.c (relocate): Assume pathname is non-NULL. Use ISSLASH macro consistently. Avoid dangerous string concatenation idiom. diff --git a/lib/relocatable.c b/lib/relocatable.c index c42398e..189aee4 100644 --- a/lib/relocatable.c +++ b/lib/relocatable.c @@ -542,27 +542,26 @@ relocate (const char *pathname) # ifdef __KLIBC__ # undef strncmp - if (pathname && strncmp (pathname, "/@unixroot", 10) == 0 - && (pathname[10] == '\0' || pathname[10] == '/' || pathname[10] == '\\')) + if (strncmp (pathname, "/@unixroot", 10) == 0 + && (pathname[10] == '\0' || ISSLASH (pathname[10]))) { /* kLIBC itself processes /@unixroot prefix */ - return pathname; } else # endif - if (pathname && ISSLASH (pathname[0])) + if (ISSLASH (pathname[0])) { const char *unixroot = getenv ("UNIXROOT"); - if (unixroot && HAS_DEVICE (unixroot) && !unixroot[2]) + if (unixroot && HAS_DEVICE (unixroot) && unixroot[2] == '\0') { char *result = (char *) xmalloc (2 + strlen (pathname) + 1); #ifdef NO_XMALLOC if (result != NULL) #endif { - strcpy (result, unixroot); + memcpy (result, unixroot, 2); strcpy (result + 2, pathname); return result; }