nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1/* an example that should be protected by FORTIFY_SOURCE=2 but
2 * only if the appropriate -fstrict-flex-arrays= argument is used
3 * for the corresponding value used for BUFFER_DEF_SIZE
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9struct buffer_with_header {
10 char header[1];
11 char buffer[BUFFER_DEF_SIZE];
12};
13
14int main(int argc, char *argv[]) {
15 /* use volatile pointer to prevent compiler
16 * using the outer allocation length with a
17 * fortified strcpy, which would throw off
18 * the function-name-sniffing fortify-detecting
19 * approaches
20 */
21 struct buffer_with_header *volatile b = \
22 (struct buffer_with_header *)malloc(sizeof(struct buffer_with_header)+1);
23
24 /* if there are no arguments, skip the write to allow
25 * builds with BUFFER_DEF_SIZE=0 to have a case where
26 * the program passes even with strict protection.
27 */
28 if (argc > 1) {
29 strcpy(b->buffer, argv[1]);
30 puts(b->buffer);
31 }
32 return 0;
33}