Check for memory allocation errors

This commit is contained in:
Miroslav Lichvar 2014-09-22 16:09:35 +02:00
parent bb16c81e7b
commit 336473398a
8 changed files with 93 additions and 21 deletions

View 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@

View file

@ -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");

10
conf.c
View file

@ -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);
}
/* ================================================== */

View file

@ -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);
}
/* ================================================== */

View file

@ -80,6 +80,7 @@ typedef enum {
LOGF_Local,
LOGF_Util,
LOGF_Main,
LOGF_Memory,
LOGF_ClientLog,
LOGF_Configure,
LOGF_CmdMon,

67
memory.c Normal file
View file

@ -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;
}

View file

@ -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 */

View file

@ -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;
}