Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved handling if realloc fails. #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions ape_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ static void zbuffer_prepapre_buf(buffer *b, size_t input_size)
bufsize <<= 1;
}

printf("zbuf prepared for size %ld\n", bufsize);

b->zbuf->buf_size = bufsize;

if (b->zbuf->buf) {
b->zbuf->buf = realloc(b->zbuf->buf, bufsize);
printf("Realloc input buffer for size %ld\n", bufsize);
if (b->zbuf->buf == NULL) {
printf("Could not reallocate memory for zbuffer\n");
exit(1);
}
} else {
b->zbuf->buf = malloc(bufsize);
printf("Alloc input buffer for size %ld\n", bufsize);
}

b->zbuf->zstream.avail_out = bufsize - b->zbuf->zstream.avail_in;
Expand Down Expand Up @@ -229,6 +229,10 @@ void buffer_prepare(buffer *b, size_t size)
}
b->size += size;
b->data = realloc(b->data, sizeof(char) * b->size);
if (b->data == NULL) {
printf("Could not reallocate memory for buffer\n");
exit(1);
}
}
#if APE_USE_ZLIB
if (b->zbuf) {
Expand All @@ -249,6 +253,10 @@ static void buffer_prepare_for(buffer *b, size_t size, size_t forsize)
}
b->size += size;
b->data = realloc(b->data, sizeof(char) * b->size);
if (b->data == NULL) {
printf("Could not reallocate memory for buffer\n");
exit(1);
}
}
#if APE_USE_ZLIB
if (b->zbuf) {
Expand Down Expand Up @@ -399,6 +407,10 @@ buffer *buffer_to_buffer_utf8(buffer *b)
if (newb->size > newb->used+1) {
newb->size = newb->used+1;
newb->data = realloc(newb->data, newb->size);
if (newb->data == NULL) {
printf("Could not reallocate memory for utf8 buffer\n");
exit(1);
}
}

return newb;
Expand Down Expand Up @@ -453,6 +465,10 @@ buffer *buffer_utf8_to_buffer(buffer *b)
if (newb->size > newb->used+1) {
newb->size = newb->used+1;
newb->data = realloc(newb->data, newb->size);
if (newb->data == NULL) {
printf("Could not reallocate memory for utf8 buffer\n");
exit(1);
}
}

return newb;
Expand Down
4 changes: 4 additions & 0 deletions ape_event_epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ static void event_epoll_setsize(struct _fdevent *ev, int size)
{
ev->events = realloc(ev->events,
sizeof(struct epoll_event) * (size));
if (ev->events == NULL) {
printf("Could not reallocate memory for epoll\n");
exit(1);
}
}

static int event_epoll_revent(struct _fdevent *ev, int i)
Expand Down
4 changes: 4 additions & 0 deletions ape_event_kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ static ape_event_descriptor *event_kqueue_get_evd(struct _fdevent *ev, int i)
static void event_kqueue_setsize(struct _fdevent *ev, int size)
{
ev->events = realloc(ev->events, sizeof(struct kevent) * (size * 2));
if (ev->events == NULL) {
printf("Could not reallocate memory for kqueue\n");
exit(1);
}
}

static int event_kqueue_revent(struct _fdevent *ev, int i)
Expand Down
2 changes: 1 addition & 1 deletion ape_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct _fdevent {
struct epoll_event *events;
int epoll_fd;
#elif defined USE_SELECT_HANDLER
struct select_fdinfo_t **events; /* Pointers into fds */
struct select_fdinfo_t **events; /* Pointers into fds */
ape_htable_t *fdhash;
#endif
int basemem; /* Number of elements in events */
Expand Down