From 4e41dcf2b4a8529528a6c884ae7db9ad43ed342c Mon Sep 17 00:00:00 2001
From: Katharina Heck <katharina.heck@iws.uni-stuttgart.de>
Date: Tue, 12 Jun 2018 13:12:36 +0200
Subject: [PATCH] [fix] check if compJIdx < numComponents-1 to prevent memory
 leaks

---
 dumux/discretization/box/maxwellstefanslaw.hh                | 5 +++--
 dumux/discretization/cellcentered/tpfa/maxwellstefanslaw.hh  | 3 ++-
 dumux/discretization/staggered/freeflow/maxwellstefanslaw.hh | 3 ++-
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/dumux/discretization/box/maxwellstefanslaw.hh b/dumux/discretization/box/maxwellstefanslaw.hh
index 46340d701b..c2cfc168f3 100644
--- a/dumux/discretization/box/maxwellstefanslaw.hh
+++ b/dumux/discretization/box/maxwellstefanslaw.hh
@@ -180,7 +180,7 @@ private:
             reducedDiffusionMatrix[compIIdx][compIIdx] += xi/tin;
 
             // now set the rest of the entries (off-diagonal and additional entries for diagonal)
-            for (int compJIdx = 0; compJIdx < numComponents-1; compJIdx++)
+            for (int compJIdx = 0; compJIdx < numComponents; compJIdx++)
             {
                 //we don't want to calculate e.g. water in water diffusion
                 if (compIIdx == compJIdx)
@@ -201,7 +201,8 @@ private:
                 const auto tij = problem.spatialParams().harmonicMean(tijInside, tijOutside, scvf.unitOuterNormal());
 
                 reducedDiffusionMatrix[compIIdx][compIIdx] += xj/tij;
-                reducedDiffusionMatrix[compIIdx][compJIdx] +=xi*(1/tin - 1/tij);
+                if (compJIdx < numComponents-1)
+                    reducedDiffusionMatrix[compIIdx][compJIdx] +=xi*(1/tin - 1/tij);
             }
         }
         return reducedDiffusionMatrix;
diff --git a/dumux/discretization/cellcentered/tpfa/maxwellstefanslaw.hh b/dumux/discretization/cellcentered/tpfa/maxwellstefanslaw.hh
index ad226ea7ba..dd9bbc918e 100644
--- a/dumux/discretization/cellcentered/tpfa/maxwellstefanslaw.hh
+++ b/dumux/discretization/cellcentered/tpfa/maxwellstefanslaw.hh
@@ -234,7 +234,8 @@ private:
                 Scalar tij = getDiffusionCoefficient(phaseIdx, compIIdx, compJIdx, problem, element, volVars, scv);
                 tij = EffDiffModel::effectiveDiffusivity(volVars.porosity(), volVars.saturation(phaseIdx), tij);
                 reducedDiffusionMatrix[compIIdx][compIIdx] += xj/tij;
-                reducedDiffusionMatrix[compIIdx][compJIdx] += xi*(1/tin - 1/tij);
+                if (compJIdx < numComponents-1)
+                    reducedDiffusionMatrix[compIIdx][compJIdx] += xi*(1/tin - 1/tij);
             }
         }
         return reducedDiffusionMatrix;
diff --git a/dumux/discretization/staggered/freeflow/maxwellstefanslaw.hh b/dumux/discretization/staggered/freeflow/maxwellstefanslaw.hh
index 89bfb5ec92..d8ea50c0d9 100644
--- a/dumux/discretization/staggered/freeflow/maxwellstefanslaw.hh
+++ b/dumux/discretization/staggered/freeflow/maxwellstefanslaw.hh
@@ -231,7 +231,8 @@ private:
                 const auto xj = volVars.moleFraction(compJIdx);
                 const Scalar tij = volVars.effectiveDiffusivity(compIIdx, compJIdx);
                 reducedDiffusionMatrix[compIIdx][compIIdx] += xj/tij;
-                reducedDiffusionMatrix[compIIdx][compJIdx] += xi*(1/tin - 1/tij);
+                if (compJIdx < numComponents-1)
+                    reducedDiffusionMatrix[compIIdx][compJIdx] += xi*(1/tin - 1/tij);
             }
         }
         return reducedDiffusionMatrix;
-- 
GitLab