check a realloc call, use a function wrapper: erealloc - saait - the most boring static page generator
 (HTM) git clone git://git.codemadness.org/saait
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 3d54db5f366efdf4612c9eaa537a22ad11035e45
 (DIR) parent 14e13f2b2eb2a5325046789df95d65cd7afc6e47
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Tue, 29 Oct 2019 17:55:26 +0100
       
       check a realloc call, use a function wrapper: erealloc
       
       Diffstat:
         M saait.c                             |      19 ++++++++++++++-----
       
       1 file changed, 14 insertions(+), 5 deletions(-)
       ---
 (DIR) diff --git a/saait.c b/saait.c
       @@ -73,6 +73,18 @@ ecalloc(size_t nmemb, size_t size)
                return p;
        }
        
       +void *
       +erealloc(void *ptr, size_t size)
       +{
       +        void *p;
       +
       +        if (!(p = realloc(ptr, size))) {
       +                fprintf(stderr, "realloc: %s\n", strerror(errno));
       +                exit(1);
       +        }
       +        return p;
       +}
       +
        FILE *
        efopen(const char *path, const char *mode)
        {
       @@ -125,10 +137,7 @@ readfile(const char *file)
                        if (len + READ_BUF_SIZ + 1 > size) {
                                /* allocate size: common case is small textfiles */
                                size += READ_BUF_SIZ;
       -                        if (!(buf = realloc(buf, size + 1))) {
       -                                fprintf(stderr, "realloc: %s\n", strerror(errno));
       -                                exit(1);
       -                        }
       +                        buf = erealloc(buf, size + 1);
                        }
                        if (!(n = fread(&buf[len], 1, READ_BUF_SIZ, fp)))
                                break;
       @@ -436,7 +445,7 @@ main(int argc, char *argv[])
                                fprintf(stderr, "realloc: too many templates: %zu\n", templateslen);
                                exit(1);
                        }
       -                templates = realloc(templates, templateslen * sizeof(*templates));
       +                templates = erealloc(templates, templateslen * sizeof(*templates));
                        t = &templates[templateslen - 1];
                        memset(t, 0, sizeof(struct template));
                        t->name = estrdup(br->d_name);