From e0ee0bf131dbee8a27d5fdad71284cd7af0de3dc Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Sun, 10 Dec 2023 15:18:17 +0100 Subject: [PATCH] Don't silently accept wrong user input (missing nodes, values). Check for at leat 4 tokens (name, n1, n2, val/model/...). If the instance has not been set up correctly, bail out, prevent crash. --- src/spicelib/parser/inp2c.c | 29 ++++++++++++++++++++++++----- src/spicelib/parser/inp2l.c | 34 +++++++++++++++++++++++++--------- src/spicelib/parser/inp2r.c | 24 +++++++++++++++++++++--- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/src/spicelib/parser/inp2c.c b/src/spicelib/parser/inp2c.c index 7ef15766f..3fc4da4ed 100644 --- a/src/spicelib/parser/inp2c.c +++ b/src/spicelib/parser/inp2c.c @@ -32,7 +32,7 @@ void INP2C(CKTcircuit *ckt, INPtables * tab, struct card *current) int error1; /* secondary error code temporary */ INPmodel *thismodel; /* pointer to model structure describing our model */ GENmodel *mdfast = NULL; /* pointer to the actual model */ - GENinstance *fast; /* pointer to the actual instance */ + GENinstance *fast = NULL;/* pointer to the actual instance */ IFvalue ptemp; /* a value structure to package resistance into */ int waslead; /* flag to indicate that funny unlabeled number was found */ double leadval; /* actual value of unlabeled number */ @@ -49,11 +49,25 @@ void INP2C(CKTcircuit *ckt, INPtables * tab, struct card *current) } } line = current->line; - INPgetNetTok(&line, &name, 1); + + INPgetNetTok(&line, &name, 1); /* Cname */ + if (*line == '\0') { + fprintf(stderr, "\nWarning: '%s' is not a valid capacitor instance line, ignored!\n\n", current->line); + return; + } + INPgetNetTok(&line, &nname1, 1); /* */ + if (*line == '\0') { + fprintf(stderr, "\nWarning: '%s' is not a valid capacitor instance line, ignored!\n\n", current->line); + return; + } + INPgetNetTok(&line, &nname2, 1); /* */ + if (*line == '\0') { + fprintf(stderr, "\nWarning: '%s' is not a valid capacitor instance line, ignored!\n\n", current->line); + return; + } + INPinsert(&name, tab); - INPgetNetTok(&line, &nname1, 1); INPtermInsert(ckt, &nname1, tab, &node1); - INPgetNetTok(&line, &nname2, 1); INPtermInsert(ckt, &nname2, tab, &node2); /* enable reading values like 4u7 */ @@ -109,7 +123,12 @@ void INP2C(CKTcircuit *ckt, INPtables * tab, struct card *current) #endif } } - + + if (!fast || !fast->GENmodPtr) { + fprintf(stderr, "\nWarning: Instance for capacitor '%s' could not be set up properly, ignored!\n\n", current->line); + return; + } + if (error1 == 0) { /* Looks like a number */ ptemp.rValue = val; GCA(INPpName, ("capacitance", &ptemp, ckt, type, fast)); diff --git a/src/spicelib/parser/inp2l.c b/src/spicelib/parser/inp2l.c index da66650f0..adfd885b3 100644 --- a/src/spicelib/parser/inp2l.c +++ b/src/spicelib/parser/inp2l.c @@ -32,7 +32,7 @@ void INP2L(CKTcircuit *ckt, INPtables * tab, struct card *current) int error1; /* secondary error code temporary */ INPmodel *thismodel; /* pointer to model structure describing our model */ GENmodel *mdfast = NULL; /* pointer to the actual model */ - GENinstance *fast; /* pointer to the actual instance */ + GENinstance *fast = NULL;/* pointer to the actual instance */ IFvalue ptemp; /* a value structure to package inductance into */ int waslead; /* flag to indicate that funny unlabeled number was found */ double leadval; /* actual value of unlabeled number */ @@ -49,14 +49,25 @@ void INP2L(CKTcircuit *ckt, INPtables * tab, struct card *current) } } line = current->line; - INPgetNetTok(&line, &name, 1); - INPinsert(&name, tab); - INPgetNetTok(&line, &nname1, 1); - INPtermInsert(ckt, &nname1, tab, &node1); - INPgetNetTok(&line, &nname2, 1); - INPtermInsert(ckt, &nname2, tab, &node2); + INPgetNetTok(&line, &name, 1); /* Lname */ + if (*line == '\0') { + fprintf(stderr, "\nWarning: '%s' is not a valid inductor instance line, ignored!\n\n", current->line); + return; + } + INPgetNetTok(&line, &nname1, 1); /* */ + if (*line == '\0') { + fprintf(stderr, "\nWarning: '%s' is not a valid inductor instance line, ignored!\n\n", current->line); + return; + } + INPgetNetTok(&line, &nname2, 1); /* */ + if (*line == '\0') { + fprintf(stderr, "\nWarning: '%s' is not a valid inductor instance line, ignored!\n\n", current->line); + return; + } -// val = INPevaluate(&line, &error1, 1); + INPinsert(&name, tab); + INPtermInsert(ckt, &nname1, tab, &node1); + INPtermInsert(ckt, &nname2, tab, &node2); /* enable reading values like 4u7 */ if (newcompat.lt) @@ -110,7 +121,12 @@ void INP2L(CKTcircuit *ckt, INPtables * tab, struct card *current) #endif } } - + + if (!fast || !fast->GENmodPtr) { + fprintf(stderr, "\nWarning: Instance for inductor '%s' could not be set up properly, ignored!\n\n", current->line); + return; + } + if (error1 == 0) { /* Looks like a number */ ptemp.rValue = val; GCA(INPpName, ("inductance", &ptemp, ckt, type, fast)); diff --git a/src/spicelib/parser/inp2r.c b/src/spicelib/parser/inp2r.c index 587f940a5..5241b3035 100644 --- a/src/spicelib/parser/inp2r.c +++ b/src/spicelib/parser/inp2r.c @@ -39,7 +39,7 @@ void INP2R(CKTcircuit *ckt, INPtables * tab, struct card *current) int error1; /* secondary error code temporary */ INPmodel *thismodel; /* pointer to model structure describing our model */ GENmodel *mdfast = NULL; /* pointer to the actual model */ - GENinstance *fast; /* pointer to the actual instance */ + GENinstance *fast = NULL; /* pointer to the actual instance */ IFvalue ptemp; /* a value structure to package resistance into */ int waslead; /* flag to indicate that funny unlabeled number was found */ double leadval; /* actual value of unlabeled number */ @@ -59,10 +59,23 @@ void INP2R(CKTcircuit *ckt, INPtables * tab, struct card *current) } line = current->line; INPgetNetTok(&line, &name, 1); /* Rname */ - INPinsert(&name, tab); + if (*line == '\0') { + fprintf(stderr, "\nWarning: '%s' is not a valid resistor instance line, ignored!\n\n", current->line); + return; + } INPgetNetTok(&line, &nname1, 1); /* */ - INPtermInsert(ckt, &nname1, tab, &node1); + if (*line == '\0') { + fprintf(stderr, "\nWarning: '%s' is not a valid resistor instance line, ignored!\n\n", current->line); + return; + } INPgetNetTok(&line, &nname2, 1); /* */ + if (*line == '\0') { + fprintf(stderr, "\nWarning: '%s' is not a valid resistor instance line, ignored!\n\n", current->line); + return; + } + + INPinsert(&name, tab); + INPtermInsert(ckt, &nname1, tab, &node1); INPtermInsert(ckt, &nname2, tab, &node2); /* enable reading values like 4k7 */ @@ -197,6 +210,11 @@ void INP2R(CKTcircuit *ckt, INPtables * tab, struct card *current) } } + if (!fast || !fast->GENmodPtr) { + fprintf(stderr, "\nWarning: Instance for resistor '%s' could not be set up properly, ignored!\n\n", current->line); + return; + } + if (error1 == 0) { /* got a resistance above */ ptemp.rValue = val; GCA(INPpName, ("resistance", &ptemp, ckt, type, fast));