Index: usr.bin/xargs/strnsubst.c =================================================================== --- usr.bin/xargs/strnsubst.c +++ usr.bin/xargs/strnsubst.c @@ -16,7 +16,13 @@ #include #include -void strnsubst(char **, const char *, const char *, size_t); +enum error { + STRNSUBST_NOERROR = 0, + STRNSUBST_ERROR, + STRNSUBST_TRUNCATED +}; + +int strnsubst(char **, const char *, const char *, size_t); /* * Replaces str with a string consisting of str with match replaced with @@ -28,14 +34,15 @@ * that we can still pretend to do somewhat meaningful substitution. * No value is returned. */ -void +int strnsubst(char **str, const char *match, const char *replstr, size_t maxsize) { char *s1, *s2, *this; + int error = STRNSUBST_NOERROR; s1 = *str; if (s1 == NULL) - return; + return STRNSUBST_ERROR; /* * If maxsize is 0 then set it to the length of s1, because we have * to duplicate s1. XXX we maybe should double-check whether the match @@ -67,6 +74,7 @@ if ((strlen(s2) + strlen(s1) + strlen(replstr) - strlen(match) + 1) > maxsize) { strlcat(s2, s1, maxsize); + error = STRNSUBST_TRUNCATED; goto done; } strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1); @@ -76,7 +84,7 @@ strcat(s2, s1); done: *str = s2; - return; + return error; } #ifdef TEST Index: usr.bin/xargs/xargs.c =================================================================== --- usr.bin/xargs/xargs.c +++ usr.bin/xargs/xargs.c @@ -73,7 +73,7 @@ static int prompt(void); static void run(char **); static void usage(void); -void strnsubst(char **, const char *, const char *, size_t); +int strnsubst(char **, const char *, const char *, size_t); static pid_t xwait(int block, int *status); static void xexit(const char *, const int); static void waitchildren(const char *, int); @@ -517,7 +517,10 @@ while (--argc) { *tmp = *avj++; if (repls && strstr(*tmp, replstr) != NULL) { - strnsubst(tmp++, replstr, inpline, (size_t)Sflag); + if (strnsubst(tmp++, replstr, inpline, (size_t)Sflag) > 0) { + warnx("comamnd line cannot be assembled, too long"); + xexit(*argv, 1); + } if (repls > 0) repls--; } else {