changeset 7:84888a38bf59

Do some error checking.
author Sjoerd Mullender <sjoerd@acm.org>
date Wed, 05 Jul 2017 17:11:48 +0200 (2017-07-05)
parents fe52d7542b80
children f407504e6d24
files gsl.c
diffstat 1 files changed, 40 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/gsl.c	Fri Mar 31 15:17:46 2017 +0200
+++ b/gsl.c	Wed Jul 05 17:11:48 2017 +0200
@@ -51,7 +51,6 @@
 	BATiter bi;
 	BUN p,q;
 	dbl r;
-	char *msg = NULL;
 
 	if (datapoints == dbl_nil) {
 		throw(MAL, "GSLbat_chisqprob_cst", "Parameter datapoints should not be nil");
@@ -70,15 +69,22 @@
 	}
 	BATloop(b,p,q) {
 		dbl d = *(dbl*)BUNtail(bi,p);
-		if ((d == dbl_nil) || (d < 0))
+		if ((d == dbl_nil) || (d < 0)) {
+			BBPunfix(b->batCacheid);
+			BBPreclaim(bn);
 			throw(MAL, "gsl.chi2prob", "Wrong value for chi2");
+		}
 		r = gsl_cdf_chisq_Q(d, datapoints);
-		BUNappend(bn, &r, FALSE);
+		if (BUNappend(bn, &r, FALSE) != GDK_SUCCEED) {
+			BBPunfix(b->batCacheid);
+			BBPreclaim(bn);
+			throw(MAL, "gsl.chi2prob", GDK_EXCEPTION);
+		}
 	}
 	*retval = bn->batCacheid;
 	BBPkeepref(bn->batCacheid);
 	BBPunfix(b->batCacheid);
-	return msg;
+	return MAL_SUCCEED;
 }
 
 static str
@@ -88,16 +94,15 @@
 	BATiter bi;
 	BUN p,q;
 	dbl r;
-	char *msg = NULL;
 
-	if( (b = BATdescriptor(datapoints)) == NULL) {
-		throw(MAL, "chisqprob", "Cannot access descriptor");
-	}
 	if (chi2 == dbl_nil) {
 		throw(MAL, "GSLbat_chisqprob_cst", "Parameter chi2 should not be nil");
 	}
 	if (chi2 < 0)
 		throw(MAL, "gsl.chi2prob", "Wrong value for chi2");
+	if( (b = BATdescriptor(datapoints)) == NULL) {
+		throw(MAL, "chisqprob", "Cannot access descriptor");
+	}
 	bi = bat_iterator(b);
 	bn = COLnew(b->hseqbase, TYPE_dbl, BATcount(b), TRANSIENT);
 	if( bn == NULL) {
@@ -107,14 +112,21 @@
 	BATloop(b,p,q) {
 		dbl datapoints = *(dbl*)BUNtail(bi,p);
 
-		if ((datapoints == dbl_nil) || (datapoints < 0))
+		if ((datapoints == dbl_nil) || (datapoints < 0)) {
+			BBPunfix(b->batCacheid);
+			BBPreclaim(bn);
 			throw(MAL, "gsl.chi2prob", "Wrong value for datapoints");
+		}
 		r = gsl_cdf_chisq_Q(chi2, datapoints);
-		BUNappend(bn, &r, FALSE);
+		if (BUNappend(bn, &r, FALSE) != GDK_SUCCEED) {
+			BBPunfix(b->batCacheid);
+			BBPreclaim(bn);
+			throw(MAL, "gsl.chi2prob", GDK_EXCEPTION);
+		}
 	}
 	BBPkeepref( *retval = bn->batCacheid);
 	BBPunfix(b->batCacheid);
-	return msg;
+	return MAL_SUCCEED;
 }
 
 static str
@@ -122,7 +134,6 @@
 {
 	BAT *b, *c, *bn;
 	dbl r, *chi2p, *datapointsp;
-	char *msg = NULL;
 	size_t cnt = 0, i;
 
 	if( (b = BATdescriptor(chi2)) == NULL) {
@@ -139,17 +150,30 @@
 	chi2p = (dbl*)Tloc(b, 0);
 	datapointsp = (dbl*)Tloc(c, 0);
 	for(i = 0; i<cnt; i++) { 
-		if ((chi2p[i] == dbl_nil) || (chi2p[i] < 0))
+		if ((chi2p[i] == dbl_nil) || (chi2p[i] < 0)) {
+			BBPunfix(b->batCacheid);
+			BBPunfix(c->batCacheid);
+			BBPreclaim(bn);
 			throw(MAL, "gsl.chi2prob", "Wrong value for chi2");
-		if ((datapointsp[i] == dbl_nil) || (datapointsp[i] < 0))
+		}
+		if ((datapointsp[i] == dbl_nil) || (datapointsp[i] < 0)) {
+			BBPunfix(b->batCacheid);
+			BBPunfix(c->batCacheid);
+			BBPreclaim(bn);
 			throw(MAL, "gsl.chi2prob", "Wrong value for datapoints");
+		}
 		r = gsl_cdf_chisq_Q(chi2p[i], datapointsp[i]);
-		BUNappend(bn, &r, FALSE);
+		if (BUNappend(bn, &r, FALSE) != GDK_SUCCEED) {
+			BBPunfix(b->batCacheid);
+			BBPunfix(c->batCacheid);
+			BBPreclaim(bn);
+			throw(MAL, "gsl.chi2prob", GDK_EXCEPTION);
+		}
 	}
 	BBPkeepref( *retval = bn->batCacheid);
 	BBPunfix(b->batCacheid);
 	BBPunfix(c->batCacheid);
-	return msg;
+	return MAL_SUCCEED;
 }
 
 str