From 2017955d089c74a675d6e752c38b731ebd6fc38d Mon Sep 17 00:00:00 2001
From: Markus Blatt <markus@dr-blatt.de>
Date: Thu, 8 Dec 2022 13:56:51 +0100
Subject: [PATCH] Fixes implicit build mode of BCRSMatrix with zero rows.

Corner case that sometimes happens and produced a segmentation fault.
---
 dune/istl/bcrsmatrix.hh | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/dune/istl/bcrsmatrix.hh b/dune/istl/bcrsmatrix.hh
index 9384fcdf6..987acf612 100644
--- a/dune/istl/bcrsmatrix.hh
+++ b/dune/istl/bcrsmatrix.hh
@@ -1465,10 +1465,18 @@ namespace Dune {
       overflow.clear();
 
       //determine average number of entries and memory usage
-      std::ptrdiff_t diff = (r[n-1].getindexptr() + r[n-1].getsize() - j_.get());
-      nnz_ = diff;
-      stats.avg = (double) (nnz_) / (double) n;
-      stats.mem_ratio = (double) (nnz_) / (double) allocationSize_;
+      if ( n == 0)
+      {
+        stats.avg = 0;
+        stats.mem_ratio = 1;
+      }
+      else
+      {
+        std::ptrdiff_t diff = (r[n-1].getindexptr() + r[n-1].getsize() - j_.get());
+        nnz_ = diff;
+        stats.avg = (double) (nnz_) / (double) n;
+        stats.mem_ratio = (double) (nnz_) / (double) allocationSize_;
+      }
 
       //matrix is now built
       ready = built;
-- 
GitLab