Browse Source

[PATCH #59] Made INP*Cat() functions more modular and efficient. Also

eliminated the possibility of a buffer overrun in INPstrCat() due to an error
 in the calling funciton.
pre-master-46
Jim Monte 7 years ago
committed by Holger Vogt
parent
commit
1665b53b84
  1. 13
      src/frontend/inpcom.c
  2. 2
      src/include/ngspice/inpdefs.h
  3. 91
      src/spicelib/parser/inperrc.c

13
src/frontend/inpcom.c

@ -5617,15 +5617,16 @@ inp_temper_compat(struct card *card)
modified_exp = inp_modify_exp(exp_str);
tfree(exp_str);
/* add the intermediate string between previous and next expression to the new line */
new_str = INPstrCat(new_str, copy_substring(beg_str, end_str), " ");
new_str = INPstrCat(new_str, ' ',
copy_substring(beg_str, end_str));
/* add the modified expression string to the new line */
new_str = INPstrCat(new_str, modified_exp, " ");
new_str = INPstrCat(new_str, copy(" "), " ");
new_str = INPstrCat(new_str, ' ', modified_exp);
new_str = INPstrCat(new_str, ' ', copy(" "));
/* move on to the next intermediate string */
beg_str = beg_tstr = end_tstr;
}
if (*beg_str)
new_str = INPstrCat(new_str, copy(beg_str), " ");
new_str = INPstrCat(new_str, ' ', copy(beg_str));
tfree(card->line);
card->line = inp_remove_ws(new_str);
}
@ -6230,7 +6231,7 @@ inp_fix_temper_in_param(struct card *deck)
}
/* restore first part of the line */
new_str = INPstrCat(firsttok_str, new_str, " ");
new_str = INPstrCat(firsttok_str, ' ', new_str);
new_str = inp_remove_ws(new_str);
/* if we have inserted into a .param line, convert to .func */
@ -6448,7 +6449,7 @@ inp_fix_agauss_in_param(struct card *deck, char *fcn)
}
/* restore first part of the line */
new_str = INPstrCat(firsttok_str, new_str, " ");
new_str = INPstrCat(firsttok_str, ' ', new_str);
new_str = inp_remove_ws(new_str);
*card->line = '*';

2
src/include/ngspice/inpdefs.h

@ -94,7 +94,7 @@ char *INPdevParse(char **, CKTcircuit *, int, GENinstance *, double *, int *, IN
char *INPdomodel(CKTcircuit *, struct card *, INPtables *);
void INPdoOpts(CKTcircuit *, JOB *, struct card *, INPtables *);
char *INPerrCat(char *, char *);
char *INPstrCat(char *, char *, char *);
char *INPstrCat(char *, char, char *);
char *INPerror(int);
double INPevaluate(char **, int *, int);
char *INPfindLev(char *, int *);

91
src/spicelib/parser/inperrc.c

@ -8,50 +8,73 @@ Author: 1985 Thomas L. Quarles
a new string is malloced, they are combined, both input strings
are freed, and the new string is returned.
*/
#include <stdio.h>
#include <string.h>
#include "ngspice/ngspice.h"
#include <stdio.h>
#include "ngspice/inpdefs.h"
#include "inpxx.h"
static char *INPcat(size_t n_a, const char *a, char sep_char,
size_t n_b, const char *b);
/* This function returns the non-null string a or b if only one of them
* is not null. Otherwise it returns NULL if both are null or
* <a>'\n'<b> if both are non-null. */
char *INPerrCat(char *a, char *b)
{
if (a != NULL) {
if (b == NULL) { /* a valid, b null, return a */
return (a);
} else { /* both valid - hard work... */
register char *errtmp;
errtmp =
TMALLOC(char, strlen(a) + strlen(b) + 2);
(void) strcpy(errtmp, a);
(void) strcat(errtmp, "\n");
(void) strcat(errtmp, b);
FREE(a);
FREE(b);
return (errtmp);
}
} else /* a null, so return b */
return (b);
}
return INPstrCat(a, '\n', b);
} /* end of function INPerrCat */
char *INPstrCat(char *a, char *b, char *c)
/* This function returns the non-null string a or b if only one of them
* is not null. Otherwise it returns NULL if both are null or
* <a><seppchar><b> if both are non-null. */
char *INPstrCat(char *a, char sepchar, char *b)
{
if (a != NULL) {
if (b == NULL) { /* a valid, b null, return a */
return (a);
} else { /* both valid - hard work... */
register char *strtmp;
strtmp =
TMALLOC(char, strlen(a) + strlen(b) + 2);
(void) strcpy(strtmp, a);
(void) strcat(strtmp, c); /* single character only! */
(void) strcat(strtmp, b);
FREE(a);
FREE(b);
return (strtmp);
if (b == NULL) { /* a valid, b null, return a */
return a;
}
} else /* a null, so return b */
return (b);
}
else { /* both valid - hard work... */
char *a_ch_b = INPcat(strlen(a), a, sepchar,
strlen(b), b);
txfree(a);
txfree(b);
return a_ch_b;
}
}
else { /* a null, so return b */
return b;
}
} /* end of function INPstrCat */
/* This function concatenates strings a and b with sep_char added
* between them. Strings a and b need not be null-terminated. */
static char *INPcat(size_t n_a, const char *a, char sepchar,
size_t n_b, const char *b)
{
char *a_ch_b = TMALLOC(char, n_a + n_b + 2);
/* Build string. Check not really requied since program exits
* if allocation in TMALLOC fails but would be if this behavior
* is changed. */
if (a_ch_b != (char *) NULL) {
char *p_cur = a_ch_b;
(void) memcpy(p_cur, a, n_a);
p_cur += n_a;
*p_cur++ = sepchar;
(void) memcpy(p_cur, b, n_b);
p_cur += n_b;
*p_cur = '\0';
}
return a_ch_b;
} /* end of function INPcat */
Loading…
Cancel
Save