]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
Check for memory allocation errors
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 22 Sep 2014 14:09:35 +0000 (16:09 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 23 Sep 2014 13:47:02 +0000 (15:47 +0200)
Makefile.in
client.c
conf.c
local.c
logging.h
memory.c [new file with mode: 0644]
memory.h
mkdirpp.c

index 4ffd5fdf6c57bc31444ed50f2ba345d981297b86..b43970e257be20911e0bee2e511490aeee132cc9 100644 (file)
@@ -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@
 
index 0008857b1502ca2ac846d5c04590b4a90b60a1ce..ea83702b0de717e0c26ae61c94e268db8adfc0f1 100644 (file)
--- 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 46a765d4d4f44d61e5352b9381d3776ab4e7845c..06241571b549660cb90f6a24e0e9a441f5191d45 100644 (file)
--- 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 5a3fb4360fb53088ef1f1bda571c930c462f7f47..f6e87936b8be3183b851d07ecfc6adcc6fdc2f84 100644 (file)
--- 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);
 }
 
 /* ================================================== */
index 1b8236da60745bff1e6dafd34e90bb17213e1904..aa221fa6e955e4745d0e5a7a4c0b79bd47107853 100644 (file)
--- 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 (file)
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;
+}
index 5c65272ef190e53bdc0ce49b497d3dc453013171..3ec0f04ac51c46497b865ebdb482adf809e36272 100644 (file)
--- a/memory.h
+++ b/memory.h
 #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 */
index 88e29da94026898b33e520f6827211f993f18a6a..ed4b23d48c851136725a363cabb6417ed8bd3e27 100644 (file)
--- 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;
 
 }