...
Code Block | ||||
---|---|---|---|---|
| ||||
struct S { size_t len; char buf[]; /* flexible array member */ }; char *find(const struct S *s, int c) { char *first = s->buf; char *last = s->buf + s->len; while (first++ != last) /* undefined behavior here */ if (*first == (unsigned char)c) return first; return NULL; } int g() { struct S *s = (struct S*)malloc(sizeof (struct S)); if (s == NULL) { return -1; /* indicate errorfailure */ } s->len = 0; /* ... */ char *where = find(s, '.'); /* ... */ return 0; } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
struct S { size_t len; char buf[]; /* flexible array member */ }; char *find(const struct S *s, int c) { char *first = s->buf; char *last = s->buf + s->len; while (first != last) /* avoid incrementing here */ if (*++first == (unsigned char)c) return first; return NULL; } int g() { struct S *s = (struct S*)malloc(sizeof (struct S)); if (s == NULL) { return -1; /* indicate errorfailure */ } s->len = 0; /* ... */ char *where = find(s, '.'); /* ... */ return 0; } |
...