Newer
Older
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
// Based on MITRE's CWE-761, demonstrative example 1
// https://cwe.mitre.org/data/definitions/761.html
#include <stdlib.h>
#include <string.h>
#ifdef FC_EVA_PRECISE
// Inclusion of Frama-C's libc 'string.c' improves precision of the analysis
// during call to strcpy()
#include "string.c"
#endif
#define SUCCESS (1)
#define FAILURE (0)
int contains_char(char c) {
char *str;
str = malloc(20*sizeof(char));
if (!str) return FAILURE;
strcpy(str, "Search Me!");
while(*str != '\0') {
if( *str == c ){
/* matched char, free string and return success */
free(str);
return SUCCESS;
}
/* didn't match yet, increment pointer and try next char */
str = str + 1;
}
/* we did not match the char in the string, free mem and return failure */
free(str);
return FAILURE;
}
int main() {
return contains_char('e');
}