From aaf586ae8394426d5a5b09055b954c4c2c6ed718 Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Mon, 29 Nov 2021 11:11:58 +0100 Subject: [PATCH] .probe: Add more safeguards to prevent crashes in case of wrong user input due to obtain empty lines in shared ngspice --- examples/probe/mos-test.cir | 2 +- src/frontend/inpc_probe.c | 42 ++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/examples/probe/mos-test.cir b/examples/probe/mos-test.cir index 32b1622c7..231ea3868 100644 --- a/examples/probe/mos-test.cir +++ b/examples/probe/mos-test.cir @@ -17,7 +17,7 @@ mp1 out in dd dd pm .probe i(mp1:s) i(mn1:s) v(in) v(out) vd(mn1:d:s) vd(mp1:1, mn1:1) * buggy inputs -.probe i(mn1:z) vd(mp1:0:0) vd(mp1:1:1) hhhh) i(:u) VD(z) i(()) +.probe i(mn1:z) vd(mp1:0:0) vd(mp1:1:1) hhhh) i(:u) VD(z) i(()) vd(:0:0) i(:z) .save @mn1[id] diff --git a/src/frontend/inpc_probe.c b/src/frontend/inpc_probe.c index 727f2c86c..7264e3259 100644 --- a/src/frontend/inpc_probe.c +++ b/src/frontend/inpc_probe.c @@ -129,11 +129,15 @@ void inp_probe(struct card* deck) continue; if (*curr_line == '.') continue; + if (*curr_line == '\0') + continue; /* here we should go on with only true device instances at top level. Put all instance names as key into a hash table, with the address as parameter. */ /* Get the instance name as key */ char* instname = gettok_instance(&curr_line); + if (!instname) + continue; nghash_insert(instances, instname, card); } @@ -175,8 +179,12 @@ void inp_probe(struct card* deck) continue; if (*curr_line == '.') continue; + if (*curr_line == '\0') + continue; char* instname = gettok_instance(&curr_line); + if (!instname) + continue; /* select elements not in need of a measure Vsource */ if (strchr("evihk", *instname)) @@ -192,6 +200,14 @@ void inp_probe(struct card* deck) strnode1 = gettok(&thisline); strnode2 = gettok(&thisline); + if (!strnode2 || *strnode2 == '\0') { + fprintf(stderr, "Warning: Cannot read 2 nodes in line %s\n", curr_line); + fprintf(stderr, " Instance not ready for .probe command\n"); + tfree(strnode1); + tfree(strnode2); + continue; + } + nodename2 = get_terminal_name(instname, "2", instances); char* newnode = tprintf("int_%s_%s", strnode2, instname); @@ -220,6 +236,12 @@ void inp_probe(struct card* deck) char* thisnode; char nodebuf[20]; thisnode = gettok(&thisline); + if (!thisnode || *thisnode == '\0') { + fprintf(stderr, "Warning: Cannot read node %d in line %s\n", i, curr_line); + fprintf(stderr, " Instance not ready for .probe command\n"); + tfree(thisnode); + continue; + } char* newnode = tprintf("int_%s_%s_%d", thisnode, instname, i); sadd(&dnewline, newnode); cadd(&dnewline, ' '); @@ -239,9 +261,6 @@ void inp_probe(struct card* deck) allsaves = wl_cons(nodesaves, allsaves); } - - - tfree(newnode); tfree(nodename); } @@ -331,6 +350,13 @@ void inp_probe(struct card* deck) thisline = nexttok(thisline); /* skip instance name */ strnode1 = gettok(&thisline); strnode2 = gettok(&thisline); + if (!strnode2 || *strnode2 == '\0') { + fprintf(stderr, "Warning: Cannot read 2 nodes in line %s\n", tmpcard1->line); + fprintf(stderr, " Instance not ready for .probe command\n"); + tfree(strnode1); + tfree(strnode2); + continue; + } newline = tprintf("Ediff%d_%s vd_%s 0 %s %s 1", ee, instname1, instname1, strnode1, strnode2); char* nodesaves = tprintf("vd_%s", instname1); @@ -347,7 +373,12 @@ void inp_probe(struct card* deck) else { char* tmpstr2, *nodename1, *nodename2; struct card* tmpcard2; + tmpstr2 = tmpstr; instname1 = gettok_char(&tmpstr, ':', FALSE, FALSE); + if (!instname1) { + fprintf(stderr, "Warning: Cannot read instance name in %s, ignored\n"); + continue; + } tmpcard1 = nghash_find(instances, instname1); if (!tmpcard1) { fprintf(stderr, "Warning: Could not find the instance line for %s,\n .probe %s will be ignored\n", instname1, wltmp->wl_word); @@ -367,6 +398,11 @@ void inp_probe(struct card* deck) tmpstr2++; /* beyond ',' */ instname2 = gettok_char(&tmpstr2, ':', FALSE, FALSE); + if (!instname2) { + fprintf(stderr, "Warning: Cannot read instance name in %s, ignored\n", tmpstr); + tfree(nodename1); + continue; + } tmpstr2++; /* beyond ':' */ tmpcard2 = nghash_find(instances, instname2); if (!tmpcard2) {