From 7d611252ac7e589fe8ce4f820ee099f01f8e18aa Mon Sep 17 00:00:00 2001 From: Jim Monte Date: Wed, 1 May 2019 12:43:51 +0200 Subject: [PATCH] [PATCH #38] Fixed crash when composing a complex vector from a mix of real and complex values. Also eliminated unnecessary test for element being real when composed vector is real. --- src/frontend/com_compose.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/frontend/com_compose.c b/src/frontend/com_compose.c index 5bdd6c48c..d76ef3ba7 100644 --- a/src/frontend/com_compose.c +++ b/src/frontend/com_compose.c @@ -199,14 +199,30 @@ com_compose(wordlist *wl) */ for (v = vecs, i = 0; v; v = v->v_link2) { if (dim == 1) { - if (realflag && isreal(v)) { + /* 3 possibilities + * 1) Composed vector is real (and current value is real) + * 2) Composed vector is complex + * a) and current value is real + * b) and current value is complex + * It is not possible for the composed vector to be real and + * the current value to be complex because it would have + * caused the composed vector to be complex. */ + if (realflag) { /* composed vector is real */ data[i] = v->v_realdata[0]; - } else if (isreal(v)) { - realpart(cdata[i]) = realpart(v->v_compdata[0]); - imagpart(cdata[i]) = 0.0; - } else { - cdata[i] = v->v_compdata[0]; } + else { /* complex composed vector */ + ngcomplex_t *cdata_cur = cdata + i; + if (isreal(v)) { + /* Current value is real, so build complex value from it + * and no imaginary part */ + realpart(*cdata_cur) = *v->v_realdata; + imagpart(*cdata_cur) = 0.0; + } + else { + *cdata_cur = *v->v_compdata; + } + } + i++; continue; }