Check for memory allocation errors
This commit is contained in:
parent
bb16c81e7b
commit
336473398a
8 changed files with 93 additions and 21 deletions
|
@ -38,9 +38,9 @@ DESTDIR=
|
||||||
|
|
||||||
HASH_OBJ = @HASH_OBJ@
|
HASH_OBJ = @HASH_OBJ@
|
||||||
|
|
||||||
OBJS = cmdparse.o conf.o local.o logging.o main.o mkdirpp.o reference.o \
|
OBJS = cmdparse.o conf.o local.o logging.o main.o memory.o mkdirpp.o \
|
||||||
regress.o rtc.o sched.o sources.o sourcestats.o stubs.o sys.o \
|
reference.o regress.o rtc.o sched.o sources.o sourcestats.o stubs.o \
|
||||||
tempcomp.o util.o $(HASH_OBJ)
|
sys.o tempcomp.o util.o $(HASH_OBJ)
|
||||||
|
|
||||||
EXTRA_OBJS=@EXTRA_OBJECTS@
|
EXTRA_OBJS=@EXTRA_OBJECTS@
|
||||||
|
|
||||||
|
|
5
client.c
5
client.c
|
@ -36,7 +36,6 @@
|
||||||
#include "getdate.h"
|
#include "getdate.h"
|
||||||
#include "cmdparse.h"
|
#include "cmdparse.h"
|
||||||
#include "pktlength.h"
|
#include "pktlength.h"
|
||||||
#include "memory.h"
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#ifdef FEAT_READLINE
|
#ifdef FEAT_READLINE
|
||||||
|
@ -972,11 +971,11 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case CPS_Success:
|
case CPS_Success:
|
||||||
if (DNS_Name2IPAddress(data.name, &ip_addr) != DNS_Success) {
|
if (DNS_Name2IPAddress(data.name, &ip_addr) != DNS_Success) {
|
||||||
Free(data.name);
|
free(data.name);
|
||||||
fprintf(stderr, "Invalid host/IP address\n");
|
fprintf(stderr, "Invalid host/IP address\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Free(data.name);
|
free(data.name);
|
||||||
|
|
||||||
if (data.params.min_stratum != SRC_DEFAULT_MINSTRATUM) {
|
if (data.params.min_stratum != SRC_DEFAULT_MINSTRATUM) {
|
||||||
fprintf(stderr, "Option minstratum not supported\n");
|
fprintf(stderr, "Option minstratum not supported\n");
|
||||||
|
|
10
conf.c
10
conf.c
|
@ -462,7 +462,7 @@ static int
|
||||||
parse_string(char *line, char **result)
|
parse_string(char *line, char **result)
|
||||||
{
|
{
|
||||||
check_number_of_args(line, 1);
|
check_number_of_args(line, 1);
|
||||||
*result = strdup(line);
|
*result = Strdup(line);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -627,11 +627,11 @@ parse_refclock(char *line)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
name = strdup(p);
|
name = Strdup(p);
|
||||||
|
|
||||||
p = line;
|
p = line;
|
||||||
line = CPS_SplitWord(line);
|
line = CPS_SplitWord(line);
|
||||||
param = strdup(p);
|
param = Strdup(p);
|
||||||
|
|
||||||
while (*line) {
|
while (*line) {
|
||||||
cmd = line;
|
cmd = line;
|
||||||
|
@ -856,7 +856,7 @@ parse_mailonchange(char *line)
|
||||||
address = line;
|
address = line;
|
||||||
line = CPS_SplitWord(line);
|
line = CPS_SplitWord(line);
|
||||||
if (sscanf(line, "%lf", &mail_change_threshold) == 1) {
|
if (sscanf(line, "%lf", &mail_change_threshold) == 1) {
|
||||||
mail_user_on_change = strdup(address);
|
mail_user_on_change = Strdup(address);
|
||||||
} else {
|
} else {
|
||||||
mail_user_on_change = NULL;
|
mail_user_on_change = NULL;
|
||||||
command_parse_error();
|
command_parse_error();
|
||||||
|
@ -1138,7 +1138,7 @@ parse_tempcomp(char *line)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tempcomp_file = strdup(p);
|
tempcomp_file = Strdup(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
4
local.c
4
local.c
|
@ -247,7 +247,7 @@ void LCL_RemoveParameterChangeHandler(LCL_ParameterChangeHandler handler, void *
|
||||||
ptr->next->prev = ptr->prev;
|
ptr->next->prev = ptr->prev;
|
||||||
ptr->prev->next = ptr->next;
|
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->next->prev = ptr->prev;
|
||||||
ptr->prev->next = ptr->next;
|
ptr->prev->next = ptr->next;
|
||||||
|
|
||||||
free(ptr);
|
Free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
|
@ -80,6 +80,7 @@ typedef enum {
|
||||||
LOGF_Local,
|
LOGF_Local,
|
||||||
LOGF_Util,
|
LOGF_Util,
|
||||||
LOGF_Main,
|
LOGF_Main,
|
||||||
|
LOGF_Memory,
|
||||||
LOGF_ClientLog,
|
LOGF_ClientLog,
|
||||||
LOGF_Configure,
|
LOGF_Configure,
|
||||||
LOGF_CmdMon,
|
LOGF_CmdMon,
|
||||||
|
|
67
memory.c
Normal file
67
memory.c
Normal 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;
|
||||||
|
}
|
14
memory.h
14
memory.h
|
@ -27,11 +27,15 @@
|
||||||
#ifndef GOT_MEMORY_H
|
#ifndef GOT_MEMORY_H
|
||||||
#define GOT_MEMORY_H
|
#define GOT_MEMORY_H
|
||||||
|
|
||||||
#define Malloc(x) malloc(x)
|
/* Wrappers checking for errors */
|
||||||
#define MallocNew(T) ((T *) malloc(sizeof(T)))
|
extern void *Malloc(size_t size);
|
||||||
#define MallocArray(T, n) ((T *) malloc((n) * sizeof(T)))
|
extern void *Realloc(void *ptr, size_t size);
|
||||||
#define Realloc(x,y) realloc(x,y)
|
extern char *Strdup(const char *s);
|
||||||
#define ReallocArray(T,n,x) ((T *) realloc((void *)(x), (n)*sizeof(T)))
|
|
||||||
|
/* 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)
|
#define Free(x) free(x)
|
||||||
|
|
||||||
#endif /* GOT_MEMORY_H */
|
#endif /* GOT_MEMORY_H */
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
|
|
||||||
#include "sysincl.h"
|
#include "sysincl.h"
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
#include "mkdirpp.h"
|
#include "mkdirpp.h"
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -74,7 +75,7 @@ mkdir_and_parents(const char *path)
|
||||||
int i, j, k, last;
|
int i, j, k, last;
|
||||||
len = strlen(path);
|
len = strlen(path);
|
||||||
|
|
||||||
p = (char *) malloc(1 + len);
|
p = (char *)Malloc(1 + len);
|
||||||
|
|
||||||
i = k = 0;
|
i = k = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
|
@ -84,7 +85,7 @@ mkdir_and_parents(const char *path)
|
||||||
p[i] = 0;
|
p[i] = 0;
|
||||||
|
|
||||||
if (do_dir(p) < 0) {
|
if (do_dir(p) < 0) {
|
||||||
free(p);
|
Free(p);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,7 +115,7 @@ mkdir_and_parents(const char *path)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(p);
|
Free(p);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue