5 * Concatenates a variable number of strings. The argument list must be
6 * terminated with a NULL. Returns a pointer to malloc(3)'ed memory with
7 * the concatenated string, or NULL on error.
9 * This code deals gracefully with potential integer overflows (perhaps when
10 * input strings are maliciously long), as well as with input strings changing
11 * from under it (perhaps because of misbehavior of another thread). It does
12 * not depend on non-portable functions such as snprintf() and asprintf().
14 * Written by Solar Designer <solar at openwall.com> and placed in the
17 * retrieved from http://cvsweb.openwall.com/cgi/cvsweb.cgi/Owl/packages/popa3d/popa3d/misc.c
18 * see also http://seclists.org/bugtraq/2006/Nov/594
24 Prototype
char *concat(const char *s1
, ...);
27 concat(const char *s1
, ...)
31 unsigned long l
, m
, n
;
35 while ((s
= va_arg(args
, char *))) {
37 if ((m
+= l
) < l
) break;
40 if (s
|| m
>= INT_MAX
) return NULL
;
42 result
= malloc(m
+ 1);
43 if (!result
) return NULL
;
45 memcpy(p
= result
, s1
, n
);
48 while ((s
= va_arg(args
, char *))) {
50 if ((n
+= l
) < l
|| n
> m
) break;
55 if (s
|| m
!= n
|| p
- result
!= n
) {