From 1ea4b4414a7622a4123df29ae4fabe35395e9447 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 5 May 2009 15:35:05 +0000 Subject: [PATCH] Memory allocation checks from Micheal Davidsaver. --- src/catools/caget.c | 27 +++++++++++++++++++-------- src/catools/caput.c | 23 +++++++++++++++++++++-- src/catools/tool_lib.c | 13 ++++++++----- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/catools/caget.c b/src/catools/caget.c index 8c17b0c4c..3acef9331 100644 --- a/src/catools/caget.c +++ b/src/catools/caget.c @@ -7,8 +7,7 @@ * Operator of Los Alamos National Laboratory. * Copyright (c) 2002 Berliner Elektronenspeicherringgesellschaft fuer * Synchrotronstrahlung. -* EPICS BASE Versions 3.13.7 -* and higher are distributed subject to a Software License Agreement found +* EPICS BASE is distributed subject to a Software License Agreement found * in file LICENSE that is included with this distribution. \*************************************************************************/ @@ -204,6 +203,10 @@ static int caget (pv *pvs, int nPvs, RequestT request, OutputT format, } else { /* Allocate value structure */ pvs[n].value = calloc(1, dbr_size_n(pvs[n].dbrType, pvs[n].reqElems)); + if(!pvs[n].value) { + fprintf(stderr,"Allocation failed\n"); + return 1; + } result = ca_array_get(pvs[n].dbrType, pvs[n].reqElems, pvs[n].chid, @@ -268,9 +271,13 @@ static int caget (pv *pvs, int nPvs, RequestT request, OutputT format, dbr_char_t *s = (dbr_char_t*) dbr_value_ptr(pvs[n].value, pvs[n].dbrType); int dlen = epicsStrnEscapedFromRawSize((char*)s, strlen((char*)s)); char *d = calloc(dlen+1, sizeof(char)); - epicsStrnEscapedFromRaw(d, dlen+1, (char*)s, strlen((char*)s)); - printf("%s", d); - free(d); + if(d) { + epicsStrnEscapedFromRaw(d, dlen+1, (char*)s, strlen((char*)s)); + printf("%s", d); + free(d); + } else { + fprintf(stderr,"Failed to allocate space for escaped string\n"); + } } else { if (reqElems || pvs[n].nElems > 1) printf("%lu%c", pvs[n].reqElems, fieldSeparator); for (i=0; i optind+1) { @@ -416,6 +427,10 @@ int main (int argc, char *argv[]) sbuf = calloc (count, sizeof(EpicsStr)); dbuf = calloc (count, sizeof(double)); + if(!sbuf || !dbuf) { + fprintf(stderr, "Memory allocation failed\n"); + return 1; + } /* ENUM? Special treatment */ @@ -481,6 +496,10 @@ int main (int argc, char *argv[]) count = len; dbrType = DBR_CHAR; ebuf = calloc(strlen(cbuf), sizeof(char)); + if(!ebuf) { + fprintf(stderr, "Memory allocation failed\n"); + return 1; + } epicsStrnRawFromEscaped(ebuf, strlen(cbuf), cbuf, strlen(cbuf)); } else { for (i = 0; i < count; ++i) { diff --git a/src/catools/tool_lib.c b/src/catools/tool_lib.c index 2d7449004..7ffc9efdd 100644 --- a/src/catools/tool_lib.c +++ b/src/catools/tool_lib.c @@ -6,8 +6,7 @@ * Operator of Los Alamos National Laboratory. * Copyright (c) 2002 Berliner Elektronenspeicherringgesellschaft fuer * Synchrotronstrahlung. -* EPICS BASE Versions 3.13.7 -* and higher are distributed subject to a Software License Agreement found +* EPICS BASE is distributed subject to a Software License Agreement found * in file LICENSE that is included with this distribution. \*************************************************************************/ @@ -430,9 +429,13 @@ char *dbr2str (const void *value, unsigned type) dbr_char_t *s = (dbr_char_t*) dbr_value_ptr(pv->value, pv->dbrType); \ int dlen = epicsStrnEscapedFromRawSize((char*)s, strlen((char*)s)); \ char *d = calloc(dlen+1, sizeof(char)); \ - epicsStrnEscapedFromRaw(d, dlen+1, (char*)s, strlen((char*)s)); \ - printf("%c%s", fieldSeparator, d); \ - free(d); \ + if(d) { \ + epicsStrnEscapedFromRaw(d, dlen+1, (char*)s, strlen((char*)s));\ + printf("%c%s", fieldSeparator, d); \ + free(d); \ + } else { \ + printf("Failed to allocate for print_time_val_sts\n"); \ + } \ } else { \ if (reqElems || pv->nElems > 1) printf("%c%lu", fieldSeparator, pv->reqElems); \ for (i=0; ireqElems; ++i) { \