Browse Source

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.
pre-master-46
Holger Vogt 2 years ago
parent
commit
45850c07dc
  1. 29
      src/spicelib/parser/inp2c.c
  2. 30
      src/spicelib/parser/inp2l.c
  3. 24
      src/spicelib/parser/inp2r.c

29
src/spicelib/parser/inp2c.c

@ -32,7 +32,7 @@ void INP2C(CKTcircuit *ckt, INPtables * tab, struct card *current)
int error1; /* secondary error code temporary */ int error1; /* secondary error code temporary */
INPmodel *thismodel; /* pointer to model structure describing our model */ INPmodel *thismodel; /* pointer to model structure describing our model */
GENmodel *mdfast = NULL; /* pointer to the actual 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 */ IFvalue ptemp; /* a value structure to package resistance into */
int waslead; /* flag to indicate that funny unlabeled number was found */ int waslead; /* flag to indicate that funny unlabeled number was found */
double leadval; /* actual value of unlabeled number */ double leadval; /* actual value of unlabeled number */
@ -49,11 +49,25 @@ void INP2C(CKTcircuit *ckt, INPtables * tab, struct card *current)
} }
} }
line = current->line; 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); /* <node> */
if (*line == '\0') {
fprintf(stderr, "\nWarning: '%s' is not a valid capacitor instance line, ignored!\n\n", current->line);
return;
}
INPgetNetTok(&line, &nname2, 1); /* <node> */
if (*line == '\0') {
fprintf(stderr, "\nWarning: '%s' is not a valid capacitor instance line, ignored!\n\n", current->line);
return;
}
INPinsert(&name, tab); INPinsert(&name, tab);
INPgetNetTok(&line, &nname1, 1);
INPtermInsert(ckt, &nname1, tab, &node1); INPtermInsert(ckt, &nname1, tab, &node1);
INPgetNetTok(&line, &nname2, 1);
INPtermInsert(ckt, &nname2, tab, &node2); INPtermInsert(ckt, &nname2, tab, &node2);
/* enable reading values like 4u7 */ /* enable reading values like 4u7 */
@ -109,7 +123,12 @@ void INP2C(CKTcircuit *ckt, INPtables * tab, struct card *current)
#endif #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 */ if (error1 == 0) { /* Looks like a number */
ptemp.rValue = val; ptemp.rValue = val;
GCA(INPpName, ("capacitance", &ptemp, ckt, type, fast)); GCA(INPpName, ("capacitance", &ptemp, ckt, type, fast));

30
src/spicelib/parser/inp2l.c

@ -32,7 +32,7 @@ void INP2L(CKTcircuit *ckt, INPtables * tab, struct card *current)
int error1; /* secondary error code temporary */ int error1; /* secondary error code temporary */
INPmodel *thismodel; /* pointer to model structure describing our model */ INPmodel *thismodel; /* pointer to model structure describing our model */
GENmodel *mdfast = NULL; /* pointer to the actual 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 */ IFvalue ptemp; /* a value structure to package inductance into */
int waslead; /* flag to indicate that funny unlabeled number was found */ int waslead; /* flag to indicate that funny unlabeled number was found */
double leadval; /* actual value of unlabeled number */ double leadval; /* actual value of unlabeled number */
@ -49,15 +49,26 @@ void INP2L(CKTcircuit *ckt, INPtables * tab, struct card *current)
} }
} }
line = current->line; line = current->line;
INPgetNetTok(&line, &name, 1);
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); /* <node> */
if (*line == '\0') {
fprintf(stderr, "\nWarning: '%s' is not a valid inductor instance line, ignored!\n\n", current->line);
return;
}
INPgetNetTok(&line, &nname2, 1); /* <node> */
if (*line == '\0') {
fprintf(stderr, "\nWarning: '%s' is not a valid inductor instance line, ignored!\n\n", current->line);
return;
}
INPinsert(&name, tab); INPinsert(&name, tab);
INPgetNetTok(&line, &nname1, 1);
INPtermInsert(ckt, &nname1, tab, &node1); INPtermInsert(ckt, &nname1, tab, &node1);
INPgetNetTok(&line, &nname2, 1);
INPtermInsert(ckt, &nname2, tab, &node2); INPtermInsert(ckt, &nname2, tab, &node2);
// val = INPevaluate(&line, &error1, 1);
/* enable reading values like 4u7 */ /* enable reading values like 4u7 */
if (newcompat.lt) if (newcompat.lt)
val = INPevaluateRKM_L(&line, &error1, 1); /* [<val>] */ val = INPevaluateRKM_L(&line, &error1, 1); /* [<val>] */
@ -110,7 +121,12 @@ void INP2L(CKTcircuit *ckt, INPtables * tab, struct card *current)
#endif #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 */ if (error1 == 0) { /* Looks like a number */
ptemp.rValue = val; ptemp.rValue = val;
GCA(INPpName, ("inductance", &ptemp, ckt, type, fast)); GCA(INPpName, ("inductance", &ptemp, ckt, type, fast));

24
src/spicelib/parser/inp2r.c

@ -39,7 +39,7 @@ void INP2R(CKTcircuit *ckt, INPtables * tab, struct card *current)
int error1; /* secondary error code temporary */ int error1; /* secondary error code temporary */
INPmodel *thismodel; /* pointer to model structure describing our model */ INPmodel *thismodel; /* pointer to model structure describing our model */
GENmodel *mdfast = NULL; /* pointer to the actual 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 */ IFvalue ptemp; /* a value structure to package resistance into */
int waslead; /* flag to indicate that funny unlabeled number was found */ int waslead; /* flag to indicate that funny unlabeled number was found */
double leadval; /* actual value of unlabeled number */ double leadval; /* actual value of unlabeled number */
@ -59,10 +59,23 @@ void INP2R(CKTcircuit *ckt, INPtables * tab, struct card *current)
} }
line = current->line; line = current->line;
INPgetNetTok(&line, &name, 1); /* Rname */ 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); /* <node> */ INPgetNetTok(&line, &nname1, 1); /* <node> */
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); /* <node> */ INPgetNetTok(&line, &nname2, 1); /* <node> */
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); INPtermInsert(ckt, &nname2, tab, &node2);
/* enable reading values like 4k7 */ /* 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 */ if (error1 == 0) { /* got a resistance above */
ptemp.rValue = val; ptemp.rValue = val;
GCA(INPpName, ("resistance", &ptemp, ckt, type, fast)); GCA(INPpName, ("resistance", &ptemp, ckt, type, fast));

Loading…
Cancel
Save