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
65
66
67
|
typedef __SIZE_TYPE__ size_t;
#define NULL ((void *)0)
/* Concatenating a pair of strings. */
/* Correct but poor implementation with repeated __builtin_strlen calls. */
char *
alloc_dup_of_concatenated_pair_1_correct (const char *x, const char *y)
{
size_t sz = __builtin_strlen (x) + __builtin_strlen (y) + 1;
char *result = __builtin_malloc (sz);
if (!result)
return NULL;
__builtin_memcpy (result, x, __builtin_strlen (x));
__builtin_memcpy (result + __builtin_strlen (x), y, __builtin_strlen (y));
result[__builtin_strlen(x) + __builtin_strlen (y)] = '\0';
return result;
}
/* Incorrect version: forgetting to add space for terminator. */
char *
alloc_dup_of_concatenated_pair_1_incorrect (const char *x, const char *y)
{
/* Forgetting to add space for the terminator here. */
size_t sz = __builtin_strlen (x) + __builtin_strlen (y);
char *result = __builtin_malloc (sz);
if (!result)
return NULL;
__builtin_memcpy (result, x, __builtin_strlen (x));
__builtin_memcpy (result + __builtin_strlen (x), y, __builtin_strlen (y));
result[__builtin_strlen(x) + __builtin_strlen (y)] = '\0'; /* { dg-warning "heap-based buffer overflow" "PR analyzer/105899" { xfail *-*-* } } */
return result;
}
/* As above, but only calling __builtin_strlen once on each input. */
char *
alloc_dup_of_concatenated_pair_2_correct (const char *x, const char *y)
{
size_t len_x = __builtin_strlen (x);
size_t len_y = __builtin_strlen (y);
size_t sz = len_x + len_y + 1;
char *result = __builtin_malloc (sz);
if (!result)
return NULL;
__builtin_memcpy (result, x, len_x);
__builtin_memcpy (result + len_x, y, len_y);
result[len_x + len_y] = '\0';
return result;
}
char *
alloc_dup_of_concatenated_pair_2_incorrect (const char *x, const char *y)
{
size_t len_x = __builtin_strlen (x);
size_t len_y = __builtin_strlen (y);
size_t sz = len_x + len_y; /* Forgetting to add space for the terminator. */
char *result = __builtin_malloc (sz); /* { dg-message "capacity: 'len_x \\+ len_y' bytes" } */
if (!result)
return NULL;
__builtin_memcpy (result, x, len_x);
__builtin_memcpy (result + len_x, y, len_y);
result[len_x + len_y] = '\0'; /* { dg-warning "heap-based buffer overflow" } */
return result;
}
|