Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: changed error to failure in comments

...

Code Block
bgColor#ffcccc
langc
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
bgColor#ccccff
langc
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;
}

...