Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added nul checks to new CS

...

Code Block
bgColor#ccccff
langc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *get_filled_buffer(void) {
  char temp[32];
  char *ret = NULLmalloc(sizeof(temp));
  char *end = NULL;
  char temp[32];ret;
  if (!ret) {
    return NULL;
  }

  while (fgets(temp, sizeof(temp), stdin)) {
    size_t len = strlen(temp);
    const size_t full_size = end - ret + len;
    char *r_temp = realloc(ret, full_size + 1); /* NTBS */
    if (r_temp) {
      ret = r_temp;
      strcat(ret, temp);
      end = ret + full_size;
    } else {
      break;
    }

    if (feof(stdin) || temp[len] == '\n') {
      return ret;
    }
  }
  free(ret);
  return NULL;
}

...