From 336473398ab330df4bdb36dc38e7cb4f998ed511 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Mon, 22 Sep 2014 16:09:35 +0200 Subject: [PATCH] Check for memory allocation errors --- Makefile.in | 6 ++--- client.c | 5 ++-- conf.c | 10 ++++---- local.c | 4 ++-- logging.h | 1 + memory.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ memory.h | 14 +++++++---- mkdirpp.c | 7 +++--- 8 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 memory.c diff --git a/Makefile.in b/Makefile.in index 4ffd5fd..b43970e 100644 --- a/Makefile.in +++ b/Makefile.in @@ -38,9 +38,9 @@ DESTDIR= HASH_OBJ = @HASH_OBJ@ -OBJS = cmdparse.o conf.o local.o logging.o main.o mkdirpp.o reference.o \ - regress.o rtc.o sched.o sources.o sourcestats.o stubs.o sys.o \ - tempcomp.o util.o $(HASH_OBJ) +OBJS = cmdparse.o conf.o local.o logging.o main.o memory.o mkdirpp.o \ + reference.o regress.o rtc.o sched.o sources.o sourcestats.o stubs.o \ + sys.o tempcomp.o util.o $(HASH_OBJ) EXTRA_OBJS=@EXTRA_OBJECTS@ diff --git a/client.c b/client.c index 0008857..ea83702 100644 --- a/client.c +++ b/client.c @@ -36,7 +36,6 @@ #include "getdate.h" #include "cmdparse.h" #include "pktlength.h" -#include "memory.h" #include "util.h" #ifdef FEAT_READLINE @@ -972,11 +971,11 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line) switch (status) { case CPS_Success: if (DNS_Name2IPAddress(data.name, &ip_addr) != DNS_Success) { - Free(data.name); + free(data.name); fprintf(stderr, "Invalid host/IP address\n"); break; } - Free(data.name); + free(data.name); if (data.params.min_stratum != SRC_DEFAULT_MINSTRATUM) { fprintf(stderr, "Option minstratum not supported\n"); diff --git a/conf.c b/conf.c index 46a765d..0624157 100644 --- a/conf.c +++ b/conf.c @@ -462,7 +462,7 @@ static int parse_string(char *line, char **result) { check_number_of_args(line, 1); - *result = strdup(line); + *result = Strdup(line); return 1; } @@ -627,11 +627,11 @@ parse_refclock(char *line) return; } - name = strdup(p); + name = Strdup(p); p = line; line = CPS_SplitWord(line); - param = strdup(p); + param = Strdup(p); while (*line) { cmd = line; @@ -856,7 +856,7 @@ parse_mailonchange(char *line) address = line; line = CPS_SplitWord(line); if (sscanf(line, "%lf", &mail_change_threshold) == 1) { - mail_user_on_change = strdup(address); + mail_user_on_change = Strdup(address); } else { mail_user_on_change = NULL; command_parse_error(); @@ -1138,7 +1138,7 @@ parse_tempcomp(char *line) return; } - tempcomp_file = strdup(p); + tempcomp_file = Strdup(p); } /* ================================================== */ diff --git a/local.c b/local.c index 5a3fb43..f6e8793 100644 --- a/local.c +++ b/local.c @@ -247,7 +247,7 @@ void LCL_RemoveParameterChangeHandler(LCL_ParameterChangeHandler handler, void * ptr->next->prev = ptr->prev; ptr->prev->next = ptr->next; - free(ptr); + Free(ptr); } /* ================================================== */ @@ -324,7 +324,7 @@ void LCL_RemoveDispersionNotifyHandler(LCL_DispersionNotifyHandler handler, void ptr->next->prev = ptr->prev; ptr->prev->next = ptr->next; - free(ptr); + Free(ptr); } /* ================================================== */ diff --git a/logging.h b/logging.h index 1b8236d..aa221fa 100644 --- a/logging.h +++ b/logging.h @@ -80,6 +80,7 @@ typedef enum { LOGF_Local, LOGF_Util, LOGF_Main, + LOGF_Memory, LOGF_ClientLog, LOGF_Configure, LOGF_CmdMon, diff --git a/memory.c b/memory.c new file mode 100644 index 0000000..7ad27d4 --- /dev/null +++ b/memory.c @@ -0,0 +1,67 @@ +/* + chronyd/chronyc - Programs for keeping computer clocks accurate. + + ********************************************************************** + * Copyright (C) Miroslav Lichvar 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + ********************************************************************** + + ======================================================================= + + Utility functions for memory allocation. + + */ + +#include "config.h" + +#include "logging.h" +#include "memory.h" + +void * +Malloc(size_t size) +{ + void *r; + + r = malloc(size); + if (!r && size) + LOG_FATAL(LOGF_Memory, "Could not allocate memory"); + + return r; +} + +void * +Realloc(void *ptr, size_t size) +{ + void *r; + + r = realloc(ptr, size); + if (!r && size) + LOG_FATAL(LOGF_Memory, "Could not allocate memory"); + + return r; +} + +char * +Strdup(const char *s) +{ + void *r; + + r = strdup(s); + if (!r) + LOG_FATAL(LOGF_Memory, "Could not allocate memory"); + + return r; +} diff --git a/memory.h b/memory.h index 5c65272..3ec0f04 100644 --- a/memory.h +++ b/memory.h @@ -27,11 +27,15 @@ #ifndef GOT_MEMORY_H #define GOT_MEMORY_H -#define Malloc(x) malloc(x) -#define MallocNew(T) ((T *) malloc(sizeof(T))) -#define MallocArray(T, n) ((T *) malloc((n) * sizeof(T))) -#define Realloc(x,y) realloc(x,y) -#define ReallocArray(T,n,x) ((T *) realloc((void *)(x), (n)*sizeof(T))) +/* Wrappers checking for errors */ +extern void *Malloc(size_t size); +extern void *Realloc(void *ptr, size_t size); +extern char *Strdup(const char *s); + +/* Convenient macros */ +#define MallocNew(T) ((T *) Malloc(sizeof(T))) +#define MallocArray(T, n) ((T *) Malloc((n) * sizeof(T))) +#define ReallocArray(T,n,x) ((T *) Realloc((void *)(x), (n)*sizeof(T))) #define Free(x) free(x) #endif /* GOT_MEMORY_H */ diff --git a/mkdirpp.c b/mkdirpp.c index 88e29da..ed4b23d 100644 --- a/mkdirpp.c +++ b/mkdirpp.c @@ -30,6 +30,7 @@ #include "sysincl.h" +#include "memory.h" #include "mkdirpp.h" static int @@ -74,7 +75,7 @@ mkdir_and_parents(const char *path) int i, j, k, last; len = strlen(path); - p = (char *) malloc(1 + len); + p = (char *)Malloc(1 + len); i = k = 0; while (1) { @@ -84,7 +85,7 @@ mkdir_and_parents(const char *path) p[i] = 0; if (do_dir(p) < 0) { - free(p); + Free(p); return 0; } @@ -114,7 +115,7 @@ mkdir_and_parents(const char *path) } - free(p); + Free(p); return 1; }