|
| 1 | +#include <ctype.h> |
| 2 | +#include <limits.h> |
| 3 | +#include <math.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#include "caller.h" |
| 9 | +#include "callee.h" |
| 10 | + |
| 11 | +// ARR30-C: Violation example (Using Past-the-End Index) |
| 12 | +static int *table = NULL; |
| 13 | +static size_t size = 0; |
| 14 | + |
| 15 | +int insert_in_table(size_t pos, int value) { |
| 16 | + if (size < pos) { |
| 17 | + int *tmp; |
| 18 | + size = pos + 1; |
| 19 | + tmp = (int *)realloc(table, sizeof(*table) * size); |
| 20 | + if (tmp == NULL) { |
| 21 | + return -1; /* Failure */ |
| 22 | + } |
| 23 | + table = tmp; |
| 24 | + } |
| 25 | + |
| 26 | + table[pos] = value; |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +// ARR30-C: Violation example (Null Pointer Arithmetic) |
| 31 | +char *init_block(size_t block_size, size_t offset, |
| 32 | + char *data, size_t data_size) { |
| 33 | + char *buffer = malloc(block_size); |
| 34 | + if (data_size > block_size || block_size - data_size < offset) { |
| 35 | + /* Data won't fit in buffer, handle error */ |
| 36 | + } |
| 37 | + memcpy(buffer + offset, data, data_size); |
| 38 | + return buffer; |
| 39 | +} |
| 40 | + |
| 41 | +// ARR30-C: Violation example (Pointer Past Flexible Array Member) |
| 42 | +struct S { |
| 43 | + size_t len; |
| 44 | + char buf[]; /* Flexible array member */ |
| 45 | +}; |
| 46 | + |
| 47 | +const char *find(const struct S *s, int c) { |
| 48 | + const char *first = s->buf; |
| 49 | + const char *last = s->buf + s->len; |
| 50 | + |
| 51 | + while (first++ != last) { /* Undefined behavior */ |
| 52 | + if (*first == (unsigned char)c) { |
| 53 | + return first; |
| 54 | + } |
| 55 | + } |
| 56 | + return NULL; |
| 57 | +} |
| 58 | + |
| 59 | +void handle_error(void) { |
| 60 | + struct S *s = (struct S *)malloc(sizeof(struct S)); |
| 61 | + if (s == NULL) { |
| 62 | + /* Handle error */ |
| 63 | + } |
| 64 | + s->len = 0; |
| 65 | + find(s, 'a'); |
| 66 | +} |
| 67 | + |
| 68 | +// FLP30-C: Violation example |
| 69 | +void float_loop(void) { |
| 70 | + for (float x = 0.1f; x <= 1.0f; x += 0.1f) { |
| 71 | + /* Loop may iterate 9 or 10 times */ |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// FLP30-C: Violation example |
| 76 | +void flp30_2(void) { |
| 77 | + for (float x = 100000001.0f; x <= 100000010.0f; x += 1.0f) { |
| 78 | + /* Loop may not terminate */ |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// FLP32-C: Violation example (sqrt()) |
| 83 | +double sqroot(double x) { |
| 84 | + double result; |
| 85 | + result = sqrt(x); |
| 86 | + return result; |
| 87 | +} |
| 88 | + |
| 89 | +// FLP37-C: Violation example |
| 90 | +struct S2 { |
| 91 | + int i; |
| 92 | + float f; |
| 93 | +}; |
| 94 | + |
| 95 | +bool are_equal(const struct S2 *s1, const struct S2 *s2) { |
| 96 | + if (!s1 && !s2) |
| 97 | + return true; |
| 98 | + else if (!s1 || !s2) |
| 99 | + return false; |
| 100 | + return 0 == memcmp(s1, s2, sizeof(struct S2)); |
| 101 | +} |
| 102 | + |
| 103 | +// INT33-C: Violation example |
| 104 | +signed long func(signed long s_a, signed long s_b) { |
| 105 | + signed long result; |
| 106 | + if ((s_a == LONG_MIN) && (s_b == -1)) { |
| 107 | + /* Handle error */ |
| 108 | + } else { |
| 109 | + result = s_a / s_b; |
| 110 | + } |
| 111 | + /* ... */ |
| 112 | + return result; |
| 113 | +} |
| 114 | + |
| 115 | +// MEM34-C: Violation example |
| 116 | +enum { BUFSIZE = 256 }; |
| 117 | +int f1(void) { |
| 118 | + char *text_buffer = (char *)malloc(BUFSIZE); |
| 119 | + if (text_buffer == NULL) { |
| 120 | + return -1; |
| 121 | + } |
| 122 | + return 0; |
| 123 | +} |
| 124 | + |
| 125 | +enum { STR_SIZE = 32 }; |
| 126 | +size_t str32(const char *source) { |
| 127 | + char c_str[STR_SIZE]; |
| 128 | + size_t ret = 0; |
| 129 | + |
| 130 | + if (source) { |
| 131 | + c_str[sizeof(c_str) - 1] = '\0'; |
| 132 | + strncpy(c_str, source, sizeof(c_str)); |
| 133 | + ret = strlen(c_str); |
| 134 | + } else { |
| 135 | + /* Handle null pointer */ |
| 136 | + } |
| 137 | + return ret; |
| 138 | +} |
| 139 | + |
| 140 | +// DCL38-C: Violation example |
| 141 | +struct flexArrayStruct { |
| 142 | + int num; |
| 143 | + int data[1]; |
| 144 | +}; |
| 145 | + |
| 146 | +void dcl38(size_t array_size) { |
| 147 | + /* Space is allocated for the struct */ |
| 148 | + struct flexArrayStruct *structP |
| 149 | + = (struct flexArrayStruct *) |
| 150 | + malloc(sizeof(struct flexArrayStruct) |
| 151 | + + sizeof(int) * (array_size - 1)); |
| 152 | + if (structP == NULL) { |
| 153 | + /* Handle malloc failure */ |
| 154 | + } |
| 155 | + |
| 156 | + structP->num = array_size; |
| 157 | + |
| 158 | + /* |
| 159 | + * Access data[] as if it had been allocated |
| 160 | + * as data[array_size]. |
| 161 | + */ |
| 162 | + for (size_t i = 0; i < array_size; ++i) { |
| 163 | + structP->data[i] = 1; |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +size_t count_preceding_whitespace(const char *s) { |
| 168 | + const char *t = s; |
| 169 | + size_t length = strlen(s) + 1; |
| 170 | + while (isspace(*t) && (t - s < length)) { |
| 171 | + ++t; |
| 172 | + } |
| 173 | + return t - s; |
| 174 | +} |
| 175 | + |
| 176 | +int main() { |
| 177 | + caller(); |
| 178 | +} |
| 179 | + |
0 commit comments