From 8e65614d3134d980d7e57951d2f524519d30956f Mon Sep 17 00:00:00 2001 From: rlar Date: Sun, 8 Feb 2015 21:57:07 +0100 Subject: [PATCH] spfactor.c, cktnames.c, don't crash when simulating "nothing" reported by Siddhant Saraf in > [ngspice:discussion] "out of memory" error : simulating a 2bit AND gate" This comes in two flavours, 1) an empty circuit 2) a xspice circuit with exclusively xspice event nodes --- src/maths/sparse/spfactor.c | 11 +++++++++++ src/spicelib/analysis/cktnames.c | 2 +- tests/regression/misc/Makefile.am | 2 +- tests/regression/misc/empty-1.cir | 18 ++++++++++++++++++ tests/regression/misc/empty-1.out | 26 ++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 tests/regression/misc/empty-1.cir create mode 100644 tests/regression/misc/empty-1.out diff --git a/src/maths/sparse/spfactor.c b/src/maths/sparse/spfactor.c index bd72a9788..dcfad94ac 100644 --- a/src/maths/sparse/spfactor.c +++ b/src/maths/sparse/spfactor.c @@ -340,6 +340,11 @@ spFactor(MatrixPtr Matrix) Size = Matrix->Size; + if (Size == 0) { + Matrix->Factored = YES; + return (Matrix->Error = spOKAY); + } + if (Matrix->Diag[1]->Real == 0.0) return ZeroPivot( Matrix, 1 ); Matrix->Diag[1]->Real = 1.0 / Matrix->Diag[1]->Real; @@ -443,6 +448,12 @@ FactorComplexMatrix( MatrixPtr Matrix ) assert(Matrix->Complex); Size = Matrix->Size; + + if (Size == 0) { + Matrix->Factored = YES; + return (Matrix->Error = spOKAY); + } + pElement = Matrix->Diag[1]; if (ELEMENT_MAG(pElement) == 0.0) return ZeroPivot( Matrix, 1 ); /* Cmplx expr: *pPivot = 1.0 / *pPivot. */ diff --git a/src/spicelib/analysis/cktnames.c b/src/spicelib/analysis/cktnames.c index 9fc145310..d8718ffac 100644 --- a/src/spicelib/analysis/cktnames.c +++ b/src/spicelib/analysis/cktnames.c @@ -22,7 +22,7 @@ CKTnames(CKTcircuit *ckt, int *numNames, IFuid **nameList) int i; *numNames = ckt->CKTmaxEqNum-1; *nameList = TMALLOC(IFuid, *numNames); - if ((*nameList) == NULL) return(E_NOMEM); + if (*numNames && (*nameList) == NULL) return(E_NOMEM); i=0; for (here = ckt->CKTnodes->next; here; here = here->next) { (*nameList) [i++] = here->name; diff --git a/tests/regression/misc/Makefile.am b/tests/regression/misc/Makefile.am index 9f639b9e1..b12dab9f6 100644 --- a/tests/regression/misc/Makefile.am +++ b/tests/regression/misc/Makefile.am @@ -1,7 +1,7 @@ ## Process this file with automake to produce Makefile.in -TESTS = bugs-1.cir dollar-1.cir +TESTS = bugs-1.cir dollar-1.cir empty-1.cir TESTS_ENVIRONMENT = ngspice_vpath=$(srcdir) $(SHELL) $(top_srcdir)/tests/bin/check.sh $(top_builddir)/src/ngspice diff --git a/tests/regression/misc/empty-1.cir b/tests/regression/misc/empty-1.cir new file mode 100644 index 000000000..9236f777c --- /dev/null +++ b/tests/regression/misc/empty-1.cir @@ -0,0 +1,18 @@ +check that we can survive emptiness + +* (exec-spice "ngspice -b %s") + +* Nothingness, No circuit elements at all. +* We have only the implicit node "0" +* Checks whether we can live with a +* zero times zero circuit matrix + +.control +op +tran 0.1m 1m +ac lin 3 1kHz 2kHz +echo "TEST: done" +quit 0 +.endc + +.end diff --git a/tests/regression/misc/empty-1.out b/tests/regression/misc/empty-1.out new file mode 100644 index 000000000..b0d09f5b2 --- /dev/null +++ b/tests/regression/misc/empty-1.out @@ -0,0 +1,26 @@ + +Circuit: check that we can survive emptiness + +Doing analysis at TEMP = 27.000000 and TNOM = 27.000000 + + + +No. of Data Rows : 1 +Doing analysis at TEMP = 27.000000 and TNOM = 27.000000 + + +Initial Transient Solution +-------------------------- + +Node Voltage +---- ------- + + + +No. of Data Rows : 59 +Doing analysis at TEMP = 27.000000 and TNOM = 27.000000 + + + +No. of Data Rows : 3 +TEST: done