<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/129951>129951</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
off-by-one error in -fsanitizer=bounds when addressing a pointer instead of an integral
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
kees
</td>
</tr>
</table>
<pre>
The bounds sanitizer does not trip when accessing the address of the last array element (but does if it accessed as an integral). For example:
#include <stdlib.h>
#include <stdio.h>
#define SIZE 3
struct foo {
int count;
int array[SIZE];
};
volatile int zero = 0; // hide const expression size from optimizer
int main(int argc, char *argv[]) {
int size = SIZE + zero;
// include trailing space to avoid segfaults on "out of bounds" access
struct foo *p = calloc(1, sizeof(*p) + sizeof(int) + sizeof(int));
// this correctly trips sanitizer:
int val = p->array[size];
printf("%d\n", val);
// this does not?!
int *valp = &p->array[size];
printf("%p %d\n", valp, *valp);
// but this does...
int *val2 = &p->array[size + 1];
printf("%p %d\n", val2, *val2);
return 0;
}
Built with: -O2 -Wall -fstrict-flex-arrays=3 -fsanitize=bounds
./example.c:19:23: runtime error: index 3 out of bounds for type 'int [3]'
0
0xd0b42c0 0
./example.c:27:26: runtime error: index 4 out of bounds for type 'int [3]'
0xd0b42c4 0
This was noticed while using the "counted_by" attribute on a flexible array, but it is present even with fixed-size arrays.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVcGO4zYM_RrmQsSwJXuSHHxIJhugpx66QIFeClmiY7WKZEhyJtmvLyQ7M9nt7gILeMaOKJFPj4-kCEGfLVELzQGa40pMcXC-_ZcorDqn7u3ngbBzk1UBg7A66i_kUTkKaF3E6PWIbwNZFFJSCNqeMQ6EQilPIaDr808jQkThvbgjGbqQjQhs201x9qR71HHxQApFQGFR20hnLwywXYEn55Fu4jIaAr6HMj-MayvNpAiBv4aojO6KAfin79m0ezdlq6JeW8I_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