static char * getline_alloc(FILE *fh) { int ch; size_t i = 0; size_t len = 16; char *buf = xosmalloc(len); if (!buf) return NULL; ch = getc(fh); while (ch != '\n' && ch != '\r' && ch != EOF) { buf[i++] = ch; if (i == len - 1) { char *p; len += len; p = xosrealloc(buf, len); if (!p) { osfree(buf); return NULL; } buf = p; } ch = getc(fh); } if (ch == '\n' || ch == '\r') { int otherone = ch ^ ('\n' ^ '\r'); ch = getc(fh); /* if it's not the other eol character, put it back */ if (ch != otherone) ungetc(ch, fh); } buf[i++] = '\0'; return buf; }
static bool check_label_space(img *pimg, size_t len) { if (len > pimg->buf_len) { char *b = xosrealloc(pimg->label_buf, len); if (!b) return fFalse; pimg->label = (pimg->label - pimg->label_buf) + b; pimg->label_buf = b; pimg->buf_len = len; } return fTrue; }
/* realloc with error catching if it fails. */ void * osrealloc(void *p, OSSIZE_T size) { /* some pre-ANSI realloc implementations don't cope with a NULL pointer */ if (p == NULL) { p = xosmalloc(size); } else { #ifdef TOMBSTONES int true_size; size += TOMBSTONE_SIZE * 2; p -= TOMBSTONE_SIZE; true_size = *(size_t *)p; printf("osrealloc (in truep=%p truesize=%d)\n", p, true_size); if (memcmp(p + sizeof(size_t), tombstone + sizeof(size_t), TOMBSTONE_SIZE - sizeof(size_t)) != 0) { printf("start tombstone for block %p, size %d corrupted!", p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); } if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone, TOMBSTONE_SIZE) != 0) { printf("end tombstone for block %p, size %d corrupted!", p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); } p = realloc(p, size); if (p == NULL) outofmem(size); printf("osrealloc truep=%p truesize=%d\n", p, size); memcpy(p, tombstone, TOMBSTONE_SIZE); memcpy(p + size - TOMBSTONE_SIZE, tombstone, TOMBSTONE_SIZE); *(size_t *)p = size; p += TOMBSTONE_SIZE; #else p = xosrealloc(p, size); #endif } if (p == NULL) outofmem(size); return p; }