-
Notifications
You must be signed in to change notification settings - Fork 38
/
concat.c
64 lines (56 loc) · 1.52 KB
/
concat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* CONCAT.C
*
* Concatenates a variable number of strings. The argument list must be
* terminated with a NULL. Returns a pointer to malloc(3)'ed memory with
* the concatenated string, or NULL on error.
*
* This code deals gracefully with potential integer overflows (perhaps when
* input strings are maliciously long), as well as with input strings changing
* from under it (perhaps because of misbehavior of another thread). It does
* not depend on non-portable functions such as snprintf() and asprintf().
*
* Written by Solar Designer <solar at openwall.com> and placed in the
* public domain.
*
* Usage: result = concat(str1, "separator", str2, "separator", str3, NULL);
* Retrieved from https://openwall.info/wiki/people/solar/software/public-domain-source-code/concat
* See also http://seclists.org/bugtraq/2006/Nov/594
*
*/
#include "defs.h"
Prototype char *concat(const char *s1, ...);
char *
concat(const char *s1, ...)
{
va_list args;
const char *s;
char *p, *result;
size_t l, m, n;
m = n = strlen(s1);
va_start(args, s1);
while ((s = va_arg(args, char *))) {
l = strlen(s);
if ((m += l) < l) break;
}
va_end(args);
if (s || m >= INT_MAX) return NULL;
result = malloc(m + 1);
if (!result) return NULL;
memcpy(p = result, s1, n);
p += n;
va_start(args, s1);
while ((s = va_arg(args, char *))) {
l = strlen(s);
if ((n += l) < l || n > m) break;
memcpy(p, s, l);
p += l;
}
va_end(args);
if (s || m != n || p - result != n) {
free(result);
return NULL;
}
*p = 0;
return result;
}