Skip to content

Commit

Permalink
add alloc error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
amin committed Oct 11, 2020
1 parent 6b9a2da commit 8b69bcc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
14 changes: 10 additions & 4 deletions element.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#include <stdlib.h>
#include <string.h>
#include "element.h"
#include "util.h"

void
addel(Elements *e, const char *s)
{
if(e->n == 0) {
if(e->el == 0) {
e->el = malloc(sizeof(char *));
e->el = emalloc(sizeof(char *));
}
}
else
e->el = realloc(e->el, (e->n + 1) * sizeof(char *));
e->el = erealloc(e->el, (e->n + 1) * sizeof(char *));
e->n++;
e->el[e->n - 1] = malloc(strlen(s) + 1);
e->el[e->n - 1] = emalloc(strlen(s) + 1);
strcpy(e->el[e->n - 1], s);
}

Expand All @@ -24,7 +25,12 @@ rmel(Elements *e)
return;
free(e->el[e->n - 1]);
e->n--;
e->el = realloc(e->el, e->n * sizeof(char*));
if(e->n == 0) {
free(e->el);
e->el = 0;
return;
}
e->el = erealloc(e->el, e->n * sizeof(char*));
}

char *
Expand Down
5 changes: 3 additions & 2 deletions fb2ms.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unistd.h>
#include "element.h"
#include "handler.h"
#include "util.h"

#define LEN(X) (sizeof X / sizeof X[0])

Expand Down Expand Up @@ -76,12 +77,12 @@ datahandler(void *ud, const XML_Char *s, int l)
break;
if(repeat) {
newlen = strlen(curdata) + l + 1;
curdata = realloc(curdata, newlen + 1);
curdata = erealloc(curdata, newlen + 1);
strncat(curdata, s, l);
curdata[newlen] = '\0';
}
else {
curdata = malloc(l + 1);
curdata = emalloc(l + 1);
strncpy(curdata, s, l);
curdata[l] = '\0';
}
Expand Down
19 changes: 10 additions & 9 deletions handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdio.h>
#include <string.h>
#include "element.h"
#include "util.h"

extern Elements els;

Expand Down Expand Up @@ -115,7 +116,7 @@ descr_st()
{
descr.title = descr.annotation = 0;

descr.authors = malloc(sizeof(Authorlist));
descr.authors = emalloc(sizeof(Authorlist));
memset(descr.authors, 0, sizeof(Authorlist));
return;
}
Expand Down Expand Up @@ -154,13 +155,13 @@ author_st()
for(al = descr.authors; al->next; al = al->next)
printf("author skipped\n");
if(al->a == 0) {
al->a= malloc(sizeof(Author));
al->a= emalloc(sizeof(Author));
memset(al->a, 0, sizeof(Author));
}
else {
al->next = malloc(sizeof(Authorlist));
al->next = emalloc(sizeof(Authorlist));
memset(al->next, 0, sizeof(Authorlist));
al->next->a = malloc(sizeof(Author));
al->next->a = emalloc(sizeof(Author));
memset(al->next->a, 0, sizeof(Author));
}
return;
Expand All @@ -169,7 +170,7 @@ author_st()
void
booktitle_dat(char *data)
{
descr.title = malloc(strlen(data) + 1);
descr.title = emalloc(strlen(data) + 1);
strcpy(descr.title, data);
return;
}
Expand All @@ -182,7 +183,7 @@ firstname_dat(char *data)
if(isin(&els, "document-info"))
return;
for(al = descr.authors; al->next; al = al->next);
al->a->first = malloc(strlen(data) + 1);
al->a->first = emalloc(strlen(data) + 1);
strcpy(al->a->first, data);
return;
}
Expand All @@ -196,7 +197,7 @@ midname_dat(char *data)
return;
for(al = descr.authors; al->next; al = al->next)
printf("!author skipped\n");
al->a->middle = malloc(strlen(data) + 1);
al->a->middle = emalloc(strlen(data) + 1);
strcpy(al->a->middle, data);
return;
}
Expand All @@ -209,7 +210,7 @@ lastname_dat(char *data)
if(isin(&els, "document-info"))
return;
for(al = descr.authors; al->next; al = al->next);
al->a->last = malloc(strlen(data) + 1);
al->a->last = emalloc(strlen(data) + 1);
strcpy(al->a->last, data);
return;
}
Expand All @@ -222,7 +223,7 @@ nickname_dat(char *data)
if(isin(&els, "document-info"))
return;
for(al = descr.authors; al->next; al = al->next);
al->a->nick = malloc(strlen(data) + 1);
al->a->nick = emalloc(strlen(data) + 1);
strcpy(al->a->nick, data);
return;
}
Expand Down
6 changes: 5 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CC=cc
LIBS=-lexpat

fb2ms: fb2ms.o handler.o element.o
fb2ms: fb2ms.o handler.o element.o util.o
$(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) $^ -o fb2ms

fb2ms.o: fb2ms.c handler.h element.h
Expand All @@ -14,5 +14,9 @@ element.o: element.c element.h
handler.o: handler.c handler.h element.h
$(CC) $(CFLAGS) -c handler.c

util.o: util.c util.h
$(CC) $(CFLAGS) -c util.c


clean:
rm -f fb2ms *.o
23 changes: 23 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <err.h>
#include <errno.h>
#include <stdlib.h>

void *
emalloc(size_t size)
{
void *ret;
ret = malloc(size);
if(ret == 0)
err(errno, NULL);
return ret;
}

void *
erealloc(void *ptr, size_t size)
{
void *ret;
ret = realloc(ptr, size);
if(ret == 0)
err(errno, NULL);
return ret;
}
2 changes: 2 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
void *emalloc(size_t size);
void *erealloc(void *ptr, size_t size);

0 comments on commit 8b69bcc

Please sign in to comment.