From 3538cbafacaedbcc9a1d6ad1fe23f8589b4781d1 Mon Sep 17 00:00:00 2001
From: Tuomas Karna <tuomas.karna@uclouvain.be>
Date: Thu, 21 Jul 2011 11:35:54 +0000
Subject: [PATCH] fullMatrix::copy and setAll updated to avoid bugs. copy may
 reallocate the matrix so it is prohibited for proxies.

---
 Numeric/fullMatrix.cpp |  2 +-
 Numeric/fullMatrix.h   | 19 +++++--------------
 2 files changed, 6 insertions(+), 15 deletions(-)

diff --git a/Numeric/fullMatrix.cpp b/Numeric/fullMatrix.cpp
index cd289f5f68..d35cf99734 100644
--- a/Numeric/fullMatrix.cpp
+++ b/Numeric/fullMatrix.cpp
@@ -297,7 +297,7 @@ bool fullMatrix<double>::invert(fullMatrix<double> &result) const
 {
   int M = size1(), N = size2(), lda = size1(), info;
   int *ipiv = new int[std::min(M, N)];
-  result = *this;
+  result.setAll(*this);
   F77NAME(dgetrf)(&M, &N, result._data, &lda, ipiv, &info);
   if(info == 0){
     int lwork = M * 4;
diff --git a/Numeric/fullMatrix.h b/Numeric/fullMatrix.h
index ba89fb6086..8d555d84d3 100644
--- a/Numeric/fullMatrix.h
+++ b/Numeric/fullMatrix.h
@@ -251,20 +251,7 @@ class fullMatrix
   }
   fullMatrix<scalar> & operator = (const fullMatrix<scalar> &other)
   {
-    if(this != &other){
-      if (_r != other._r || _c != other._c) {
-        _r = other._r;
-        _c = other._c;
-        if (_data && _own_data) delete[] _data;
-        if ((_r == 0) || (_c == 0))
-          _data=0;
-        else{
-          _data = new scalar[_r * _c];
-          _own_data=true;
-        }
-      }
-      for(int i = 0; i < _r * _c; ++i) _data[i] = other._data[i];
-    }
+    copy(other);
     return *this;
   }
   void operator += (const fullMatrix<scalar> &other)
@@ -300,6 +287,8 @@ class fullMatrix
   }
   void copy(const fullMatrix<scalar> &a)
   {
+    if (_data && !_own_data)
+      Msg::Fatal("fullMatrix::copy operation is prohibited for proxies, use setAll instead");
     if (_r != a._r || _c != a._c) {
       if(_data && _own_data)
         delete [] _data;
@@ -360,6 +349,8 @@ class fullMatrix
   }
   inline void setAll(const fullMatrix<scalar> &m)
   {
+    if (_r != m._r || _c != m._c )
+      Msg::Fatal("fullMatrix size does not match");
     for(int i = 0; i < _r * _c; i++) _data[i] = m._data[i];
   }
   void scale(const double s)
-- 
GitLab