From 9e5c74c5711557b120945b5a2c0d6723d3427c20 Mon Sep 17 00:00:00 2001 From: Tharre Date: Wed, 16 Apr 2014 22:13:05 +0200 Subject: Rewrote malloc() and other wrappers --- src/build.c | 39 ++++++++++++++++----------------------- src/dbg.h | 7 ++++++- src/util.c | 25 ++++++++++++++++++++++--- src/util.h | 11 ++++++++++- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/src/build.c b/src/build.c index 89417fa..dc67b0f 100644 --- a/src/build.c +++ b/src/build.c @@ -35,7 +35,7 @@ int build_target(const char *target) { /* TODO: write dependencies */ goto exit; } - fprintf(stderr, "%s couldn't be built because no " + log_err("%s couldn't be built because no " "suitable do-file exists\n", target); retval = 1; goto exit; @@ -66,9 +66,7 @@ int build_target(const char *target) { char *btemp_output = xbasename(temp_output); /* read and parse shebang */ - FILE *fp = fopen(bdo_file, "rb+"); - if (!fp) - fatal("redo: failed to open %s", bdo_file); + FILE *fp = safe_fopen(bdo_file, "rb+"); const size_t bufsize = 1024; char buf[bufsize]; @@ -84,7 +82,7 @@ int build_target(const char *target) { if (buf[0] == '#' && buf[1] == '!') { argv = parsecmd(&buf[2], &i, 5); } else { - argv = ec_malloc(7 * sizeof(char*)); + argv = safe_malloc(7 * sizeof(char*)); argv[i++] = "/bin/sh"; argv[i++] = "-e"; } @@ -108,16 +106,16 @@ int build_target(const char *target) { } /* parent */ - int returncode; - waitpid(pid, &returncode, 0); + int status; + waitpid(pid, &status, 0); bool remove_temp = true; - if (WIFEXITED(returncode)) { - if (0 != returncode) { - fprintf(stderr, "redo: invoked do-file %s failed with %d\n", - do_file, WEXITSTATUS(returncode)); + if (WIFEXITED(status)) { + if (WEXITSTATUS(status)) { // TODO: check that + log_err("redo: invoked do-file %s failed: %d\n", + do_file, WEXITSTATUS(status)); } else { - /* successfull */ + /* successful */ /* if the file is 0 bytes long we delete it */ off_t f_size = fsize(temp_output); @@ -129,7 +127,7 @@ int build_target(const char *target) { } } else { /* something very wrong happened with the child */ - fprintf(stderr, "redo: invoked do-file did not terminate correctly\n"); + log_err("redo: invoked do-file did not terminate correctly\n"); } if (remove_temp) { @@ -164,7 +162,7 @@ char *get_do_file(const char *target) { free(temp); /* Redofile */ - temp = strdup("Redofile"); + temp = strdup("Redofile"); /* TODO: */ if (file_exists(temp)) return temp; free(temp); @@ -208,7 +206,7 @@ char *remove_ext(const char *str) { if (dot) /* recalculate length to only reach just before the last dot */ len = dot - str; - ret = ec_malloc(len+1); + ret = safe_malloc(len+1); memcpy(ret, str, len); ret[len] = '\0'; @@ -221,7 +219,7 @@ char *remove_ext(const char *str) { char **parsecmd(char *cmd, size_t *i, size_t keep_free) { assert(cmd); size_t argv_len = 16; - char **argv = ec_malloc(argv_len * sizeof(char*)); + char **argv = safe_malloc(argv_len * sizeof(char*)); size_t j = 0; bool prev_space = true; for (;; ++j) { @@ -241,13 +239,8 @@ char **parsecmd(char *cmd, size_t *i, size_t keep_free) { /* check if we have enough space */ while (*i+keep_free >= argv_len) { argv_len *= 2; - debug("Reallocating memory (now %zu)\n", argv_len); - /* TODO: replace realloc? */ - char **new_ptr = realloc(argv, argv_len * sizeof(char*)); - if (!new_ptr) - fatal("redo: couldn't realloc %zu bytes", - argv_len * sizeof(char*)); - argv = new_ptr; + debug("Reallocating memory (now %zu elements)\n", argv_len); + argv = safe_realloc(argv, argv_len * sizeof(char*)); } prev_space = false; diff --git a/src/dbg.h b/src/dbg.h index 014c358..41bebb9 100644 --- a/src/dbg.h +++ b/src/dbg.h @@ -15,6 +15,8 @@ #define STRINGIFY(x) #x #define LOG_HELPER(f,l,...) fprintf(stderr, "("f":"STRINGIFY(l)"): "__VA_ARGS__) #define FATAL_HELPER(M, ...) log_err(M ": %s\n", __VA_ARGS__) +#define FATAL_HELPER_(f,l,M,...) \ + fprintf(stderr, "(%s:%u): " M ": %s\n", f, l, __VA_ARGS__) #ifdef NDEBUG #define debug(...) @@ -23,6 +25,9 @@ #endif #define log_err(...) LOG_HELPER(__FILENAME__, __LINE__, __VA_ARGS__) -#define fatal(...) {FATAL_HELPER(__VA_ARGS__, strerror(errno)); exit(EXIT_FAILURE);} +#define fatal(...) \ + {FATAL_HELPER(__VA_ARGS__, strerror(errno)); exit(EXIT_FAILURE);} +#define fatal_(f,l,...) \ + {FATAL_HELPER_(f, l, __VA_ARGS__, strerror(errno)); exit(EXIT_FAILURE);} #endif diff --git a/src/util.c b/src/util.c index 80734c5..a0e05f5 100644 --- a/src/util.c +++ b/src/util.c @@ -11,14 +11,32 @@ #include "../config.h" /* A safe malloc wrapper. */ -void *ec_malloc(size_t size) { +void *safe_malloc_(size_t size, const char *file, unsigned int line) { void *ptr = malloc(size); + if (!ptr) - fatal("redo: cannot allocate %zu bytes", size); + fatal_(file, line, "redo: cannot allocate %zu bytes", size); return ptr; } +void *safe_realloc_(void *ptr, size_t size, const char *file, unsigned int line) { + void *ptr2 = realloc(ptr, size); + if (!ptr2) + fatal_(file, line, "redo: cannot reallocate %zu bytes", size); + + return ptr2; +} + +FILE *safe_fopen_(const char *path, const char *mode, const char *file, + unsigned int line) { + FILE *temp = fopen(path, mode); + if (!temp) + fatal_(file, line, "redo: failed to open %s", path); + + return temp; +} + /* For concating multiple strings into a single larger one. */ char *concat(size_t count, ...) { assert(count > 0); @@ -31,7 +49,7 @@ char *concat(size_t count, ...) { size += args_len[i]; } size++; - char *result = ec_malloc(size); + char *result = safe_malloc(size); /* debug("Allocated %zu bytes at %p\n", size, result); */ uintptr_t offset = 0; for (i = 0; i < count; ++i) { @@ -49,6 +67,7 @@ char *xbasename(const char *path) { return ptr? ptr+1 : (char*) path; } +/* TODO: REIMPLEMENT */ char *ec_strdup(const char* str) { assert(str); size_t len = strlen(str) + 1; diff --git a/src/util.h b/src/util.h index 40780d8..abfb666 100644 --- a/src/util.h +++ b/src/util.h @@ -1,11 +1,20 @@ #ifndef __RUTIL_H__ #define __RUTIL_H__ +#include #include +#include #include "../config.h" +#define safe_malloc(size) safe_malloc_(size, __FILENAME__, __LINE__) +#define safe_realloc(ptr, size) safe_realloc_(ptr, size, __FILENAME__, __LINE__) +#define safe_fopen(path, mode) safe_fopen_(path, mode, __FILENAME__, __LINE__) + +extern void *safe_malloc_(size_t size, const char *file, unsigned int line); +extern void *safe_realloc_(void *ptr, size_t size, const char *file, unsigned int line); +extern FILE *safe_fopen_(const char *path, const char *mode, + const char *file, unsigned int line); extern char *concat(size_t count, ...); -extern void *ec_malloc(size_t size); extern char *ec_strdup(const char* str); extern char *strdup(const char *str); extern char *xbasename(const char *path); -- cgit v1.2.3-70-g09d2