diff --git a/src/ciderlib/oned/onecont.c b/src/ciderlib/oned/onecont.c index 4660d8cf6..8a70d2c3e 100644 --- a/src/ciderlib/oned/onecont.c +++ b/src/ciderlib/oned/onecont.c @@ -266,7 +266,7 @@ ONEbindCSC (ONEdevice *pDevice) BindStruct = pDevice->matrix->SMPkluMatrix->KLUmatrixBindStructForCIDER ; nz = pDevice->matrix->SMPkluMatrix->KLUmatrixNZ ; - BindStructCSC = (BindElementKLUforCIDER *) malloc (nz * sizeof (BindElementKLUforCIDER)) ; + BindStructCSC = TMALLOC(BindElementKLUforCIDER, nz); for (index = 0 ; index < (int)nz ; index++) { BindStructCSC [index] = BindStruct [index] ; } diff --git a/src/ciderlib/oned/onepoiss.c b/src/ciderlib/oned/onepoiss.c index 37d48328e..6ad209224 100644 --- a/src/ciderlib/oned/onepoiss.c +++ b/src/ciderlib/oned/onepoiss.c @@ -82,7 +82,7 @@ ONEQbindCSC (ONEdevice *pDevice) BindStruct = pDevice->matrix->SMPkluMatrix->KLUmatrixBindStructForCIDER ; nz = pDevice->matrix->SMPkluMatrix->KLUmatrixNZ ; - BindStructCSC = (BindElementKLUforCIDER *) malloc (nz * sizeof (BindElementKLUforCIDER)) ; + BindStructCSC = TMALLOC (BindElementKLUforCIDER, nz) ; for (index = 0 ; index < (int)nz ; index++) { BindStructCSC [index] = BindStruct [index] ; } diff --git a/src/ciderlib/twod/twocont.c b/src/ciderlib/twod/twocont.c index 1fce53439..09006bbe4 100644 --- a/src/ciderlib/twod/twocont.c +++ b/src/ciderlib/twod/twocont.c @@ -905,7 +905,7 @@ TWObindCSC (TWOdevice *pDevice) BindStruct = pDevice->matrix->SMPkluMatrix->KLUmatrixBindStructForCIDER ; nz = pDevice->matrix->SMPkluMatrix->KLUmatrixNZ ; - BindStructCSC = (BindElementKLUforCIDER *) malloc (nz * sizeof (BindElementKLUforCIDER)) ; + BindStructCSC = TMALLOC (BindElementKLUforCIDER, nz) ; for (index = 0 ; index < (int)nz ; index++) { BindStructCSC [index] = BindStruct [index] ; } diff --git a/src/ciderlib/twod/twoncont.c b/src/ciderlib/twod/twoncont.c index 58c984d00..9b3a85dac 100644 --- a/src/ciderlib/twod/twoncont.c +++ b/src/ciderlib/twod/twoncont.c @@ -585,7 +585,7 @@ TWONbindCSC (TWOdevice *pDevice) BindStruct = pDevice->matrix->SMPkluMatrix->KLUmatrixBindStructForCIDER ; nz = pDevice->matrix->SMPkluMatrix->KLUmatrixNZ ; - BindStructCSC = (BindElementKLUforCIDER *) malloc (nz * sizeof (BindElementKLUforCIDER)) ; + BindStructCSC = TMALLOC (BindElementKLUforCIDER, nz) ; for (index = 0 ; index < (int)nz ; index++) { BindStructCSC [index] = BindStruct [index] ; } diff --git a/src/ciderlib/twod/twopcont.c b/src/ciderlib/twod/twopcont.c index 41e8811f8..ed82d0ded 100644 --- a/src/ciderlib/twod/twopcont.c +++ b/src/ciderlib/twod/twopcont.c @@ -585,7 +585,7 @@ TWOPbindCSC (TWOdevice *pDevice) BindStruct = pDevice->matrix->SMPkluMatrix->KLUmatrixBindStructForCIDER ; nz = pDevice->matrix->SMPkluMatrix->KLUmatrixNZ ; - BindStructCSC = (BindElementKLUforCIDER *) malloc (nz * sizeof (BindElementKLUforCIDER)) ; + BindStructCSC = TMALLOC (BindElementKLUforCIDER, nz) ; for (index = 0 ; index < (int)nz ; index++) { BindStructCSC [index] = BindStruct [index] ; } diff --git a/src/ciderlib/twod/twopoiss.c b/src/ciderlib/twod/twopoiss.c index 1cb28d3bc..0223070cd 100644 --- a/src/ciderlib/twod/twopoiss.c +++ b/src/ciderlib/twod/twopoiss.c @@ -171,7 +171,7 @@ TWOQbindCSC (TWOdevice *pDevice) BindStruct = pDevice->matrix->SMPkluMatrix->KLUmatrixBindStructForCIDER ; nz = pDevice->matrix->SMPkluMatrix->KLUmatrixNZ ; - BindStructCSC = (BindElementKLUforCIDER *) malloc (nz * sizeof (BindElementKLUforCIDER)) ; + BindStructCSC = TMALLOC (BindElementKLUforCIDER, nz) ; for (index = 0 ; index < (int)nz ; index++) { BindStructCSC [index] = BindStruct [index] ; } diff --git a/src/frontend/inpcom.c b/src/frontend/inpcom.c index 0ec7189aa..4e706f737 100644 --- a/src/frontend/inpcom.c +++ b/src/frontend/inpcom.c @@ -1421,7 +1421,7 @@ static struct inp_read_t inp_read(FILE* fp, int call_depth, const char* dir_name } /* OK -- now we have loaded the next line into 'buffer'. Process it. * - /* If input line is blank, ignore it & continue looping. */ + * If input line is blank, ignore it & continue looping. */ if ((strcmp(buffer, "\n") == 0) || (strcmp(buffer, "\r\n") == 0)) if (call_depth != 0 || (call_depth == 0 && cc != NULL)) { line_number_orig++; diff --git a/src/frontend/postcoms.c b/src/frontend/postcoms.c index 425587071..a0323f672 100644 --- a/src/frontend/postcoms.c +++ b/src/frontend/postcoms.c @@ -678,7 +678,7 @@ com_write(wordlist *wl) } } } - end->v_next = NULL; + if (end) end->v_next = NULL; /* Maybe we shouldn't make sure that the default scale is * present if nobody uses it. @@ -843,7 +843,7 @@ com_write_sparam(wordlist *wl) } } } - end->v_next = NULL; + if (end) end->v_next = NULL; /* Maybe we shouldn't make sure that the default scale is * present if nobody uses it. diff --git a/src/frontend/sndprint.c b/src/frontend/sndprint.c index 9174a7c3b..7c0d29a12 100644 --- a/src/frontend/sndprint.c +++ b/src/frontend/sndprint.c @@ -67,9 +67,10 @@ typedef struct { } SSFILE; static void* my_open_sf(char* fn, int nchannel) { - SSFILE* d = calloc(1, sizeof(SSFILE)); + SSFILE* d; SF_INFO sfinfo; + XCALLOC(d, SSFILE, 1); sfinfo.samplerate = o_samplerate; sfinfo.channels = nchannel; sfinfo.frames = 0; @@ -77,7 +78,7 @@ static void* my_open_sf(char* fn, int nchannel) { d->sf_channels = nchannel; d->sf_bptr = 0; - d->sf_buf = calloc(nchannel, sizeof(float)); + XCALLOC(d->sf_buf, float, nchannel); if ((d->outfile = sf_open(fn, SFM_WRITE, &sfinfo)) == NULL) { fprintf(stderr, "Error: Not able to open output file '%s'\n", fn); @@ -225,15 +226,15 @@ void snd_init(int nchannel) { if (!filename) snd_configure("spice.wav", 48000, o_sndfmt, o_mult, o_off, oversampling); outfile = p_open(filename, nchannel); sp_nchannel = nchannel; - sp_buf = calloc(SP_MAX, sizeof(SP_BUF)); + XCALLOC(sp_buf, SP_BUF, SP_MAX); for (i = 0; i < SP_MAX; i++) { sp_buf[i].tme = 0.0; - sp_buf[i].val = calloc(nchannel, sizeof(double)); + XCALLOC(sp_buf[i].val, double, nchannel); } sample = 0; #ifdef HAVE_SRC - interleaved = calloc(nchannel * OBUFSIZE * oversampling, sizeof(float)); - resampled = calloc(nchannel * OBUFSIZE, sizeof(float)); + XCALLOC(interleaved, float, nchannel * OBUFSIZE * oversampling); + XCALLOC(resampled, float, nchannel * OBUFSIZE); rabbit = src_new(SRC_SINC_BEST_QUALITY, nchannel, &rabbit_err); src_set_ratio(rabbit, 1.0 / OVERSAMPLING); src_reset(rabbit); diff --git a/src/frontend/subckt.c b/src/frontend/subckt.c index a0fed2696..18dc28546 100644 --- a/src/frontend/subckt.c +++ b/src/frontend/subckt.c @@ -1050,7 +1050,7 @@ bxx_printf(struct bxx_buffer *t, const char *fmt, ...) } } - va_end(ap); + // va_end(ap); /* already matched with va_start in for loop */ } diff --git a/src/frontend/vectors.c b/src/frontend/vectors.c index 147884158..b3d0d4098 100644 --- a/src/frontend/vectors.c +++ b/src/frontend/vectors.c @@ -98,20 +98,20 @@ enum ALL_TYPE_ENUM { static enum ALL_TYPE_ENUM get_all_type(const char *word) { /* Check for start of "all" */ - if (tolower(word[0] != 'a')) { + if (word[0] == '\0' || tolower(word[0]) != 'a') { return ALL_TYPE_NONE; } - if (tolower(word[1] != 'l')) { + if (word[1] == '\0' || tolower(word[1]) != 'l') { return ALL_TYPE_NONE; } - if (tolower(word[2] != 'l')) { + if (word[2] == '\0' || tolower(word[2]) != 'l') { return ALL_TYPE_NONE; } - + if (word[3] == '\0') { + return ALL_TYPE_ALL; + } /* It may be some type of all */ switch (tolower(word[3])) { - case '\0': - return ALL_TYPE_ALL; case 'v': if (word[4] == '\0') { return ALL_TYPE_ALLV; @@ -142,7 +142,7 @@ static enum ALL_TYPE_ENUM get_all_type(const char *word) } default: return ALL_TYPE_NONE; - } /* end of swith over char after "all" */ + } /* end of switch over char after "all" */ } /* end of function get_all_type */ diff --git a/src/xspice/icm/analog/astate/cfunc.mod b/src/xspice/icm/analog/astate/cfunc.mod index 58028cedb..319b74190 100644 --- a/src/xspice/icm/analog/astate/cfunc.mod +++ b/src/xspice/icm/analog/astate/cfunc.mod @@ -102,6 +102,7 @@ void cm_astate(ARGS) sizeof(stLocal_Data_t)))) == (stLocal_Data_t *) NULL) { cm_message_send("Unable to allocate Local_Data_t " "in cm_astate()"); + cm_cexit(1); return; } loc->state1 = 0; diff --git a/src/xspice/icm/analog/delay/cfunc.mod b/src/xspice/icm/analog/delay/cfunc.mod index 8ff4057c8..d3380505c 100644 --- a/src/xspice/icm/analog/delay/cfunc.mod +++ b/src/xspice/icm/analog/delay/cfunc.mod @@ -131,6 +131,7 @@ void cm_delay(ARGS) sizeof(mLocal_Data_t)))) == (mLocal_Data_t *) NULL) { cm_message_send("Unable to allocate Local_Data_t " "in cm_delay()"); + cm_cexit(1); return; } /*** allocate static storage for the delay buffer ***/ @@ -138,6 +139,7 @@ void cm_delay(ARGS) if (loc->buffer == (double *) NULL) { cm_message_send("Unable to allocate delay buffer " "in cm_delay()"); + cm_cexit(1); return; } loc->buffer_size = buffer_size; diff --git a/src/xspice/icm/analog/file_source/cfunc.mod b/src/xspice/icm/analog/file_source/cfunc.mod index 0b5dfe723..bf6c7c7b1 100644 --- a/src/xspice/icm/analog/file_source/cfunc.mod +++ b/src/xspice/icm/analog/file_source/cfunc.mod @@ -268,6 +268,10 @@ static char *CNVgettok(char **s) /* allocate space big enough for the whole string */ buf = (char *) malloc(strlen(*s) + 1); + if (!buf) { + cm_message_send("cannot allocate enough memory in file_source"); + cm_cexit(1); + } /* skip over any white space */ @@ -311,6 +315,10 @@ static char *CNVgettok(char **s) ret_str = (char *) malloc(strlen(buf) + 1); + if (!ret_str) { + cm_message_send("cannot allocate enough memory in file_source"); + cm_cexit(1); + } ret_str = strcpy(ret_str,buf); if(buf) free(buf); @@ -397,6 +405,7 @@ void cm_filesource(ARGS) /* structure holding parms, inputs, outputs, etc. sizeof(Local_Data_t)))) == (Local_Data_t *) NULL) { cm_message_send("Unable to allocate Local_Data_t " "in cm_filesource()"); + cm_cexit(1); return; } @@ -408,6 +417,10 @@ void cm_filesource(ARGS) /* structure holding parms, inputs, outputs, etc. sizeof(struct filesource_state)); /* calloc to null fp */ loc->indata = (struct infiledata *) malloc( sizeof(struct infiledata)); + if (!loc->indata) { + cm_message_send("cannot allocate enough memory in file_source"); + cm_cexit(1); + } loc->indata->datavec = (double *) malloc(sizeof(double) * (size_t) (stepsize * 1000)); @@ -415,10 +428,10 @@ void cm_filesource(ARGS) /* structure holding parms, inputs, outputs, etc. if (loc->timeinterval == (double *) NULL || loc->amplinterval == (double *) NULL || loc->state == (struct filesource_state *) NULL || - loc->indata == (struct infiledata *) NULL || loc->indata->datavec == (double *) NULL) { cm_message_send("Unable to allocate Local_Data_t fields " "in cm_filesource()"); + cm_cexit(1); cm_filesource_callback(mif_private, MIF_CB_DESTROY); return; } @@ -443,6 +456,7 @@ void cm_filesource(ARGS) /* structure holding parms, inputs, outputs, etc. (char *) NULL) { cm_message_send("Unable to allocate buffer " "for building file name in cm_filesource()"); + cm_cexit(1); } else { sprintf(p, "%s%s%s", lbuffer, DIR_PATHSEP, PARAM(file)); @@ -464,7 +478,6 @@ void cm_filesource(ARGS) /* structure holding parms, inputs, outputs, etc. while (!loc->state->atend) { char line[512]; char *cp, *cpdel; - char *cp2; double t = 0, d = 0; int i; if (!fgets(line, sizeof(line), loc->state->fp)) { @@ -513,7 +526,9 @@ void cm_filesource(ARGS) /* structure holding parms, inputs, outputs, etc. void * const p = realloc(loc->indata->datavec, sizeof(double) * loc->indata->vecallocated); if (p == NULL) { - cm_message_printf("cannot allocate enough memory"); + cm_message_send("cannot allocate enough memory" + " in file_source"); + cm_cexit(1); break; // loc->state->atend = 1; } loc->indata->datavec = (double *) p; @@ -524,10 +539,10 @@ void cm_filesource(ARGS) /* structure holding parms, inputs, outputs, etc. for (i = 0; i < size; ++i) { while (*cp && (isspace_c(*cp) || *cp == ',')) ++cp; - char *ncp = CNVgettok(&cp); - int ret = cnv_get_spice_value(ncp, &d); - free(ncp); - if (ret == FAIL) { + char *nncp = CNVgettok(&cp); + int rret = cnv_get_spice_value(nncp, &d); + free(nncp); + if (rret == FAIL) { derr = MIF_TRUE; break; } @@ -550,9 +565,9 @@ void cm_filesource(ARGS) /* structure holding parms, inputs, outputs, etc. loc->timeinterval[1] = loc->indata->datavec[loc->indata->actpointer + stepsize]; if (terr) - cm_message_printf("WARNING: some error occurred during reading the time values"); + cm_message_send("WARNING: some error occurred during reading the time values"); if (derr) - cm_message_printf("WARNING: some error occurred during reading the data values"); + cm_message_send("WARNING: some error occurred during reading the data values"); } loc = STATIC_VAR (locdata); diff --git a/src/xspice/icm/analog/oneshot/cfunc.mod b/src/xspice/icm/analog/oneshot/cfunc.mod index 3166ad0bc..5d5984f74 100644 --- a/src/xspice/icm/analog/oneshot/cfunc.mod +++ b/src/xspice/icm/analog/oneshot/cfunc.mod @@ -270,17 +270,24 @@ void cm_oneshot(ARGS) /* structure holding parms, /*** allocate static storage for *loc ***/ STATIC_VAR (locdata) = calloc (1 , sizeof ( Local_Data_t )); loc = STATIC_VAR (locdata); + if (!loc) { + cm_message_send(oneshot_allocation_error); + cm_cexit(1); + return; + } CALLBACK = oneshot_callback; /* Allocate storage for breakpoint domain & pulse width values */ x = loc->control = (double *) calloc((size_t) cntl_size, sizeof(double)); if (!x) { cm_message_send(oneshot_allocation_error); + cm_cexit(1); return; } y = loc->pw = (double *) calloc((size_t) pw_size, sizeof(double)); if (!y) { cm_message_send(oneshot_allocation_error); + cm_cexit(1); return; } diff --git a/src/xspice/icm/analog/pwl/cfunc.mod b/src/xspice/icm/analog/pwl/cfunc.mod index 855859a1d..f9b1e9fff 100644 --- a/src/xspice/icm/analog/pwl/cfunc.mod +++ b/src/xspice/icm/analog/pwl/cfunc.mod @@ -277,7 +277,7 @@ void cm_pwl(ARGS) /* structure holding parms, CALLBACK = cm_pwl_callback; - char *allocation_error="\n***ERROR***\nPWL: Allocation calloc failed!\n"; + char *allocation_error="\n***ERROR***\nPWL: Allocation malloc/calloc failed!\n"; char *limit_error="\n***ERROR***\nPWL: Violation of 50% rule in breakpoints!\n"; /* Retrieve frequently used parameters... */ @@ -299,18 +299,24 @@ void cm_pwl(ARGS) /* structure holding parms, /* Allocate storage for last_x_value */ STATIC_VAR(last_x_value) = (double *) malloc(sizeof(double)); last_x_value = (double *) STATIC_VAR(last_x_value); + if (!last_x_value) { + cm_message_send(allocation_error); + cm_cexit(1); + } /* Allocate storage for breakpoint domain & range values */ STATIC_VAR(x) = (double *) calloc((size_t) size, sizeof(double)); x = (double *) STATIC_VAR(x); if (!x) { cm_message_send(allocation_error); + cm_cexit(1); } STATIC_VAR(y) = (double *) calloc((size_t) size, sizeof(double)); y = (double *) STATIC_VAR(y); if (!y) { cm_message_send(allocation_error); + cm_cexit(1); } /* Retrieve x and y values. */ diff --git a/src/xspice/icm/analog/pwlts/cfunc.mod b/src/xspice/icm/analog/pwlts/cfunc.mod index c26cbd89e..56beaf602 100644 --- a/src/xspice/icm/analog/pwlts/cfunc.mod +++ b/src/xspice/icm/analog/pwlts/cfunc.mod @@ -159,7 +159,7 @@ void cm_pwlts(ARGS) /* structure holding parms, CALLBACK = cm_pwlts_callback; - char *allocation_error="\n***ERROR***\nPWL: Allocation calloc failed!\n"; + char *allocation_error="\n***ERROR***\nPWL: Allocation malloc/calloc failed!\n"; char *limit_error="\n***ERROR***\nPWL: Violation of 50% rule in breakpoints!\n"; /* Retrieve frequently used parameters... */ @@ -180,18 +180,24 @@ void cm_pwlts(ARGS) /* structure holding parms, /* Allocate storage for last_x_value */ STATIC_VAR(last_x_value) = (double *) malloc(sizeof(double)); last_x_value = (double *) STATIC_VAR(last_x_value); + if (!last_x_value) { + cm_message_send(allocation_error); + cm_cexit(1); + } /* Allocate storage for breakpoint domain & range values */ STATIC_VAR(x) = (double *) calloc((size_t) size, sizeof(double)); x = (double *) STATIC_VAR(x); if (!x) { cm_message_send(allocation_error); + cm_cexit(1); } STATIC_VAR(y) = (double *) calloc((size_t) size, sizeof(double)); y = (double *) STATIC_VAR(y); if (!y) { cm_message_send(allocation_error); + cm_cexit(1); } /* Retrieve x and y values. */ diff --git a/src/xspice/icm/analog/s_xfer/cfunc.mod b/src/xspice/icm/analog/s_xfer/cfunc.mod index 4844312d7..71663eb35 100644 --- a/src/xspice/icm/analog/s_xfer/cfunc.mod +++ b/src/xspice/icm/analog/s_xfer/cfunc.mod @@ -64,8 +64,12 @@ NON-STANDARD FEATURES /*=== MACROS ===========================*/ - - +#define CHECK_ALLOC(p) { \ +if (!p) { \ + cm_message_send("out of memory in s_xfer"); \ + cm_cexit(1); \ +} \ +} /*=== LOCAL VARIABLES & TYPEDEFS =======*/ @@ -281,15 +285,21 @@ void cm_s_xfer(ARGS) /* structure holding parms, inputs, outputs, etc. */ are not functional */ integrator = (double **) calloc((size_t) den_size, sizeof(double *)); + CHECK_ALLOC(integrator); old_integrator = (double **) calloc((size_t) den_size, sizeof(double *)); + CHECK_ALLOC(old_integrator); /* Allocate storage for coefficient values */ den_coefficient = (double **) calloc((size_t) den_size, sizeof(double *)); + CHECK_ALLOC(den_coefficient); old_den_coefficient = (double **) calloc((size_t) den_size, sizeof(double *)); + CHECK_ALLOC(old_den_coefficient); num_coefficient = (double **) calloc((size_t) num_size, sizeof(double *)); + CHECK_ALLOC(num_coefficient); old_num_coefficient = (double **) calloc((size_t) num_size, sizeof(double *)); + CHECK_ALLOC(old_num_coefficient); for (i=0; i < (2*den_size + num_size + 3); i++) cm_analog_alloc(i,sizeof(double)); @@ -297,6 +307,7 @@ void cm_s_xfer(ARGS) /* structure holding parms, inputs, outputs, etc. */ /* ITP_VAR_SIZE(den) = den_size; */ /* gain = (double *) calloc(1,sizeof(double)); + CHECK_ALLOC(gain); ITP_VAR(total_gain) = gain; ITP_VAR_SIZE(total_gain) = 1.0; */ @@ -327,7 +338,9 @@ void cm_s_xfer(ARGS) /* structure holding parms, inputs, outputs, etc. */ /* Set pointers to storage locations for in, out, and integrators...*/ integrator = (double **) calloc((size_t) den_size, sizeof(double *)); + CHECK_ALLOC(integrator); old_integrator = (double **) calloc((size_t) den_size, sizeof(double *)); + CHECK_ALLOC(old_integrator); for (i=0; i size) { size += ALLOC; file_data = realloc(file_data, size * sizeof(double)); - if (!file_data) + if (!file_data) { + cm_message_send("out of memory in xfer"); + cm_cexit(1); goto bad; + } } while (j < count) { @@ -251,18 +257,26 @@ void cm_xfer(ARGS) /* structure holding parms, inputs, outputs, etc. */ /* Allocate the internal table. */ table = (struct data *)malloc(sizeof(struct data)); - if (!table) + if (!table) { + cm_message_send("out of memory in xfer"); + cm_cexit(1); return; + } table->size = size; table->f = (double*)malloc(size * sizeof (double)); + /* no need for frees when out of memory */ if (!table->f) { - free(table); + //free(table); + cm_message_send("out of memory in xfer"); + cm_cexit(1); return; } table->s = ( Mif_Complex_t *)malloc(size * sizeof (Mif_Complex_t)); if (!table->s) { - free(table->f); - free(table); + //free(table->f); + //free(table); + cm_message_send("out of memory in xfer"); + cm_cexit(1); return; } STATIC_VAR(table) = table; diff --git a/src/xspice/icm/digital/d_genlut/cfunc.mod b/src/xspice/icm/digital/d_genlut/cfunc.mod index ff8cd1424..5e5dccd0d 100644 --- a/src/xspice/icm/digital/d_genlut/cfunc.mod +++ b/src/xspice/icm/digital/d_genlut/cfunc.mod @@ -184,6 +184,10 @@ void cm_d_genlut(ARGS) /* allocate storage for the lookup table */ STATIC_VAR (locdata) = calloc((size_t) tablelen, sizeof(Digital_t)); lookup_table = STATIC_VAR (locdata); + if (!lookup_table) { + cm_message_send("out of memory in d_genlut"); + cm_cexit(1); + } CALLBACK = genlut_callback; /* allocate storage for the outputs */ diff --git a/src/xspice/icm/digital/d_lut/cfunc.mod b/src/xspice/icm/digital/d_lut/cfunc.mod index 19c3879eb..5069bab89 100644 --- a/src/xspice/icm/digital/d_lut/cfunc.mod +++ b/src/xspice/icm/digital/d_lut/cfunc.mod @@ -150,6 +150,10 @@ void cm_d_lut(ARGS) /* allocate storage for the lookup table */ STATIC_VAR (locdata) = calloc((size_t) tablelen, sizeof(Digital_State_t)); lookup_table = STATIC_VAR (locdata); + if (!lookup_table) { + cm_message_send("out of memory in d_lut"); + cm_cexit(1); + } CALLBACK = lut_callback; /* allocate storage for the outputs */ diff --git a/src/xspice/icm/digital/d_osc/cfunc.mod b/src/xspice/icm/digital/d_osc/cfunc.mod index a9c13bd7e..ae261851b 100644 --- a/src/xspice/icm/digital/d_osc/cfunc.mod +++ b/src/xspice/icm/digital/d_osc/cfunc.mod @@ -94,8 +94,11 @@ void cm_d_osc(ARGS) table = malloc(csize * sizeof (struct pwl)); STATIC_VAR(locdata) = table; - if (!table) + if (!table) { + cm_message_send("out of memory in d_osc"); + cm_cexit(1); return; + } for (i = 0; i < csize; ++i) { table[i].ctl = PARAM(cntl_array[i]); diff --git a/src/xspice/icm/digital/d_process/cfunc.mod b/src/xspice/icm/digital/d_process/cfunc.mod index 854e75b01..e12f6b136 100644 --- a/src/xspice/icm/digital/d_process/cfunc.mod +++ b/src/xspice/icm/digital/d_process/cfunc.mod @@ -285,12 +285,14 @@ static int start(char *system_command, char * c_argv[], Process_t * process) filename_in = (char *) calloc(1, syscmd_len + 5); if (!filename_in) { cm_message_send("ERROR: No memory"); + cm_cexit(1); return 1; } filename_out = (char *) calloc(1, syscmd_len + 5); if (!filename_out) { free(filename_in); cm_message_send("ERROR: No memory"); + cm_cexit(1); return 1; } filename_in[0] = '\0'; diff --git a/src/xspice/icm/digital/d_pwm/cfunc.mod b/src/xspice/icm/digital/d_pwm/cfunc.mod index 6204faa39..301deb80e 100644 --- a/src/xspice/icm/digital/d_pwm/cfunc.mod +++ b/src/xspice/icm/digital/d_pwm/cfunc.mod @@ -102,8 +102,11 @@ void cm_d_pwm(ARGS) table = malloc(csize * sizeof (struct pwl)); STATIC_VAR(locdata) = table; - if (!table) + if (!table) { + cm_message_send("out of memory in d_pwm"); + cm_cexit(1); return; + } for (i = 0; i < csize; ++i) { table[i].ctl = PARAM(cntl_array[i]); diff --git a/src/xspice/icm/digital/d_source/cfunc.mod b/src/xspice/icm/digital/d_source/cfunc.mod index a17f3d83d..42a09db71 100644 --- a/src/xspice/icm/digital/d_source/cfunc.mod +++ b/src/xspice/icm/digital/d_source/cfunc.mod @@ -72,6 +72,13 @@ NON-STANDARD FEATURES #define DIR_PATHSEP "/" #endif +#define CHECK_ALLOC(p) { \ +if (!p) { \ + cm_message_send("out of memory in d_source"); \ + cm_cexit(1); \ +} \ +} + /*=== LOCAL VARIABLES & TYPEDEFS =======*/ @@ -171,6 +178,7 @@ static char *CNVgettok(char **s) /* allocate space big enough for the whole string */ buf = (char *) malloc(strlen(*s) + 1); + CHECK_ALLOC(buf); /* skip over any white space */ @@ -214,6 +222,7 @@ static char *CNVgettok(char **s) ret_str = (char *) malloc(strlen(buf) + 1); + CHECK_ALLOC(ret_str); ret_str = strcpy(ret_str,buf); if(buf) free(buf); @@ -758,6 +767,7 @@ static int cm_read_source(FILE *source, Local_Data_t *loc) /* set storage space for bits in a row and set them to 0*/ loc->all_data[i] = (char*)malloc(sizeof(char) * (size_t) loc->width); + CHECK_ALLOC(loc->all_data[i]); loc->imal = i; for (n = 0; n < loc->width; n++) loc->all_data[i][n] = 0; @@ -956,6 +966,7 @@ void cm_d_source(ARGS) lbuffer = getenv("NGSPICE_INPUT_DIR"); if (lbuffer && *lbuffer) { p = (char*) malloc(strlen(lbuffer) + strlen(DIR_PATHSEP) + strlen(PARAM(input_file)) + 1); + CHECK_ALLOC(p); sprintf(p, "%s%s%s", lbuffer, DIR_PATHSEP, PARAM(input_file)); source = fopen(p, "r"); free(p); @@ -981,6 +992,7 @@ void cm_d_source(ARGS) /*** allocate static storage for *loc ***/ STATIC_VAR (locdata) = calloc (1 , sizeof ( Local_Data_t )); loc = STATIC_VAR (locdata); + CHECK_ALLOC(loc); CALLBACK = cm_d_source_callback; /*** allocate storage for *index, *bits & *timepoint ***/ @@ -1005,7 +1017,9 @@ void cm_d_source(ARGS) /*** allocate storage for **all_data, & *all_timepoints ***/ loc->all_timepoints = (double*)calloc((size_t) i, sizeof(double)); + CHECK_ALLOC(loc->all_timepoints); loc->all_data = (char**)calloc((size_t) i, sizeof(char*)); + CHECK_ALLOC(loc->all_data); /* Send file pointer and the two array storage pointers */ /* to "cm_read_source()". This will return after */ diff --git a/src/xspice/icm/digital/d_state/cfunc.mod b/src/xspice/icm/digital/d_state/cfunc.mod index 64dd971a0..e11fa1284 100644 --- a/src/xspice/icm/digital/d_state/cfunc.mod +++ b/src/xspice/icm/digital/d_state/cfunc.mod @@ -66,6 +66,12 @@ NON-STANDARD FEATURES #define FAIL 1 /*=== MACROS ===========================*/ +#define CHECK_ALLOC(p) { \ +if (!p) { \ + cm_message_send("out of memory in d_state"); \ + cm_cexit(1); \ +} \ +} /*=== LOCAL VARIABLES & TYPEDEFS =======*/ @@ -183,6 +189,7 @@ static char *CNVgettok(char **s) /* allocate space big enough for the whole string */ buf = (char *) malloc(strlen(*s) + 1); + CHECK_ALLOC(buf); /* skip over any white space */ @@ -226,6 +233,7 @@ static char *CNVgettok(char **s) ret_str = (char *) malloc(strlen(buf) + 1); + CHECK_ALLOC(ret_str); ret_str = strcpy(ret_str,buf); if(buf) free(buf); @@ -1707,6 +1715,7 @@ void cm_d_state(ARGS) /* Allocate storage for the state transition table. */ table = calloc(1, sizeof (State_Table_t)); + CHECK_ALLOC(table); STATIC_VAR(table) = table; /* increment counter if not a comment until EOF reached... */ @@ -1732,14 +1741,18 @@ void cm_d_state(ARGS) /* Assign storage for arrays to pointers in state table. */ table->state = (int *)calloc((size_t) (table->depth + 1), sizeof(int)); + CHECK_ALLOC(table->state); table->bits =(short *)calloc( (size_t)(table->num_outputs * table->depth / 4 + 1), sizeof (short)); + CHECK_ALLOC(table->bits); table->inputs = (short *)calloc( (size_t)(table->num_inputs * table->depth / 8 + 1), sizeof (short)); + CHECK_ALLOC(table->inputs); table->next_state = (int *)calloc((size_t)(table->depth + 1), sizeof (int)); + CHECK_ALLOC(table->next_state); CALLBACK = callback; // To free those allocations. /*** allocate storage for *states... ***/ diff --git a/src/xspice/icm/spice2poly/icm_spice2poly/cfunc.mod b/src/xspice/icm/spice2poly/icm_spice2poly/cfunc.mod index b869d0845..c9be059ec 100644 --- a/src/xspice/icm/spice2poly/icm_spice2poly/cfunc.mod +++ b/src/xspice/icm/spice2poly/icm_spice2poly/cfunc.mod @@ -118,6 +118,10 @@ void spice2poly (ARGS) Mif_Inst_Var_Data_t *p = STATIC_VAR_INST(acgains); p -> size = num_inputs; p -> element = (Mif_Value_t *) malloc((size_t) num_inputs * sizeof(Mif_Value_t)); + if (!p->element) { + cm_message_send("out of memory in spice2poly"); + cm_cexit(1); + } for(i = 0; i < num_inputs; i++) STATIC_VAR(acgains[i]) = 0.0; } @@ -136,18 +140,30 @@ void spice2poly (ARGS) /* Get input values and coefficients to local storage for faster access */ in = (double *) malloc((size_t) num_inputs * sizeof(double)); + if (!in) { + cm_message_send("out of memory in spice2poly"); + cm_cexit(1); + } for(i = 0; i < num_inputs; i++) in[i] = INPUT(in[i]); num_coefs = PARAM_SIZE(coef); coef = (double *) malloc((size_t) num_coefs * sizeof(double)); + if (!coef) { + cm_message_send("out of memory in spice2poly"); + cm_cexit(1); + } for(i = 0; i < num_coefs; i++) coef[i] = PARAM(coef[i]); /* Allocate the array of exponents used in computing the poly terms */ exp = (int *) malloc((size_t) num_inputs * sizeof(int)); + if (!exp) { + cm_message_send("out of memory in spice2poly"); + cm_cexit(1); + } /* Initialize the exponents to zeros */ for(i = 0; i < num_inputs; i++) diff --git a/src/xspice/icm/table/support/gettokens.c b/src/xspice/icm/table/support/gettokens.c index 18ff08ea8..05a2f45ad 100644 --- a/src/xspice/icm/table/support/gettokens.c +++ b/src/xspice/icm/table/support/gettokens.c @@ -14,6 +14,12 @@ is returned. The original input string is undisturbed. #include "gettokens.h" +#define CHECK_ALLOC(p) { \ +if (!p) { \ + cm_message_send("out of memory in gettokens"); \ + cm_cexit(1); \ +} \ +} char *CNVgettok(char **s) @@ -27,6 +33,7 @@ char *CNVgettok(char **s) /* allocate space big enough for the whole string */ buf = (char *) malloc(strlen(*s) + 1); + CHECK_ALLOC(buf); /* skip over any white space */ @@ -71,6 +78,7 @@ char *CNVgettok(char **s) ret_str = (char *) malloc(strlen(buf) + 1); + CHECK_ALLOC(ret_str); ret_str = strcpy(ret_str,buf); if (buf) free(buf); diff --git a/src/xspice/icm/xtradev/pswitch/cfunc.mod b/src/xspice/icm/xtradev/pswitch/cfunc.mod index 3b8d0b6a3..f496a75e9 100644 --- a/src/xspice/icm/xtradev/pswitch/cfunc.mod +++ b/src/xspice/icm/xtradev/pswitch/cfunc.mod @@ -192,6 +192,10 @@ void cm_pswitch(ARGS) /* structure holding parms, /*** allocate static storage for *loc ***/ STATIC_VAR (locdata) = calloc (1 , sizeof ( Local_Data_t )); loc = STATIC_VAR (locdata); + if (!loc) { + cm_message_send("out of memory in pswitch"); + cm_cexit(1); + } loc->cntl_on = cntl_on; loc->cntl_off = cntl_off; diff --git a/src/xspice/icm/xtradev/seegenerator/cfunc.mod b/src/xspice/icm/xtradev/seegenerator/cfunc.mod index 08c93b5ec..280cc74ba 100644 --- a/src/xspice/icm/xtradev/seegenerator/cfunc.mod +++ b/src/xspice/icm/xtradev/seegenerator/cfunc.mod @@ -202,12 +202,14 @@ void cm_seegen(ARGS) /* structure holding parms, CALLBACK = cm_seegen_callback; if (have_scaled) { - int j; - double del = 1e12; cm_message_send("Use the scaling option\n"); allpulses = STATIC_VAR(pulses) = (pulse_info_t *) malloc(ports * sizeof(pulse_info_t)); + if (!allpulses) { + cm_message_send("out of memory in seegenerator"); + cm_cexit(1); + } /* parameter inull not specified, calculate it */ if (inull == 0) { @@ -237,6 +239,10 @@ void cm_seegen(ARGS) /* structure holding parms, /* Allocate storage for last_t_value */ STATIC_VAR(last_t_value) = (double *) malloc(sizeof(double)); last_t_value = (double *) STATIC_VAR(last_t_value); + if (!last_t_value) { + cm_message_send("out of memory in seegenerator"); + cm_cexit(1); + } /* no start if ctrl is set */ if (PORT_NULL(ctrl)) *last_t_value = tdelay; @@ -244,9 +250,17 @@ void cm_seegen(ARGS) /* structure holding parms, *last_t_value = 1e12; STATIC_VAR(last_ctrl) = (double *) malloc(sizeof(double)); last_ctrl = (double *) STATIC_VAR(last_ctrl); + if (!last_ctrl) { + cm_message_send("out of memory in seegenerator"); + cm_cexit(1); + } *last_ctrl = ctrl; STATIC_VAR(pulse_number) = (int *) malloc(sizeof(int)); pulse_number = (int *) STATIC_VAR(pulse_number); + if (!pulse_number) { + cm_message_send("out of memory in seegenerator"); + cm_cexit(1); + } *pulse_number = 1; /* set breakpoints at first pulse start and pulse maximum times */ diff --git a/src/xspice/icm/xtradev/sidiode/cfunc.mod b/src/xspice/icm/xtradev/sidiode/cfunc.mod index 2b1a955b7..521b5e63a 100644 --- a/src/xspice/icm/xtradev/sidiode/cfunc.mod +++ b/src/xspice/icm/xtradev/sidiode/cfunc.mod @@ -140,6 +140,10 @@ void cm_sidiode(ARGS) /* structure holding parms, /* allocate static storage for *loc */ STATIC_VAR(locdata) = calloc (1, sizeof(Local_Data_t)); loc = STATIC_VAR(locdata); + if (!loc) { + cm_message_send("out of memory in sidiode"); + cm_cexit(1); + } goff = 1./PARAM(roff); gon = 1./PARAM(ron); diff --git a/src/xspice/icm/xtradev/zener/cfunc.mod b/src/xspice/icm/xtradev/zener/cfunc.mod index 169cc28db..8318294f6 100644 --- a/src/xspice/icm/xtradev/zener/cfunc.mod +++ b/src/xspice/icm/xtradev/zener/cfunc.mod @@ -191,6 +191,10 @@ void cm_zener(ARGS) /* structure holding parms, /* Allocate storage for frequencies */ STATIC_VAR(previous_voltage) = (double *) malloc(sizeof(double)); previous_voltage = (double *) STATIC_VAR(previous_voltage); + if (!previous_voltage) { + cm_message_send("out of memory in zener"); + cm_cexit(1); + } /* Set previous_voltage value to zero... */ *previous_voltage = 0.0; diff --git a/src/xspice/tlines/tline_common.c b/src/xspice/tlines/tline_common.c index 90f652688..b4f214477 100644 --- a/src/xspice/tlines/tline_common.c +++ b/src/xspice/tlines/tline_common.c @@ -20,13 +20,21 @@ #include #include - +#include "ngspice/cmproto.h" #include "tline_common.h" +#define CHECK_ALLOC(p) { \ +if (!p) { \ + cm_message_send("out of memory in tline_common"); \ + cm_cexit(1); \ +} \ +} + void append_state(tline_state_t **first, double time, double V1, double V2, double I1, double I2, double tmax) { tline_state_t *pp = (tline_state_t *) malloc(sizeof(tline_state_t)); + CHECK_ALLOC(pp); pp->next = NULL; pp->time = time; @@ -106,6 +114,7 @@ void delete_tline_states(tline_state_t **first) void append_cpline_state(cpline_state_t **first, double time, double *Vp, double *Ip, double tmax) { cpline_state_t *pp = (cpline_state_t *) malloc(sizeof(cpline_state_t)); + CHECK_ALLOC(pp); pp->next = NULL; pp->time = time;