Another Bug in svdcmp???


MatthiasH
05-25-2007, 08:57 AM
First of all, I implemented all known bugs in svdcmp.cpp, which are posted in the official bug reports.

I got some strange heap-block corruptions using svdcmp. I tracked it down to this code sniplet in svdcmp.cpp:

94: flag=true;
95: for (l=k;l>=0;l--) {
96: nm=l-1;
97: if (fabs(rv1[l])+anorm == anorm) {
98: flag=false;
99: break;
100: }
101: if (fabs(w[nm])+anorm == anorm) break;
102: }

If for any reason check in line 97 does not break when l==0, then array 'w' and 'a' later on get corrupted, because nm is -1.

I suggest the following line:
97: if (fabs(rv1[l])+anorm == anorm || nm<0) {

This avoids memory corruption. The function will possibly not converge at this point, but that is better then corrupting the heap.

Is this a known issue?

Greetings from Germany

Matthias Heinrichs

Saul Teukolsky
05-25-2007, 04:28 PM
This issue is dealt with in the thread http://www.nr.com/forum/showthread.php?t=605
In summary, this is not a bug. However, your proposed "fix" will not do any harm.

Saul Teukolsky