From: Daniel Arndt Date: Sun, 15 Oct 2017 12:20:54 +0000 (+0200) Subject: Make sure to not call memset with a nullptr X-Git-Tag: v9.0.0-rc1~928^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F5248%2Fhead;p=dealii.git Make sure to not call memset with a nullptr --- diff --git a/include/deal.II/lac/la_parallel_vector.templates.h b/include/deal.II/lac/la_parallel_vector.templates.h index 3128f13299..7511e5f02a 100644 --- a/include/deal.II/lac/la_parallel_vector.templates.h +++ b/include/deal.II/lac/la_parallel_vector.templates.h @@ -237,9 +237,14 @@ namespace LinearAlgebra reinit (v, true); thread_loop_partitioner = v.thread_loop_partitioner; - dealii::internal::VectorOperations::Vector_copy copier(v.val, val); - internal::VectorOperations::parallel_for(copier, 0, partitioner->local_size(), - thread_loop_partitioner); + + const size_type this_size = local_size(); + if (this_size>0) + { + dealii::internal::VectorOperations::Vector_copy copier(v.val, val); + internal::VectorOperations::parallel_for(copier, 0, partitioner->local_size(), + thread_loop_partitioner); + } } @@ -378,9 +383,14 @@ namespace LinearAlgebra must_update_ghost_values |= vector_is_ghosted; thread_loop_partitioner = c.thread_loop_partitioner; - dealii::internal::VectorOperations::Vector_copy copier(c.val, val); - internal::VectorOperations::parallel_for(copier, 0, partitioner->local_size(), - thread_loop_partitioner); + + const size_type this_size = local_size(); + if (this_size>0) + { + dealii::internal::VectorOperations::Vector_copy copier(c.val, val); + internal::VectorOperations::parallel_for(copier, 0, this_size, + thread_loop_partitioner); + } if (must_update_ghost_values) update_ghost_values(); @@ -930,10 +940,14 @@ namespace LinearAlgebra Vector & Vector::operator = (const Number s) { - internal::VectorOperations::Vector_set setter(s, val); + const size_type this_size = local_size(); + if (this_size>0) + { + internal::VectorOperations::Vector_set setter(s, val); - internal::VectorOperations::parallel_for(setter, 0, partitioner->local_size(), - thread_loop_partitioner); + internal::VectorOperations::parallel_for(setter, 0, this_size, + thread_loop_partitioner); + } // if we call Vector::operator=0, we want to zero out all the entries // plus ghosts. diff --git a/include/deal.II/lac/read_write_vector.templates.h b/include/deal.II/lac/read_write_vector.templates.h index 7ee68d7021..75cfc9ec55 100644 --- a/include/deal.II/lac/read_write_vector.templates.h +++ b/include/deal.II/lac/read_write_vector.templates.h @@ -220,8 +220,12 @@ namespace LinearAlgebra Assert(s==static_cast(0), ExcMessage("Only 0 can be assigned to a vector.")); (void)s; - dealii::internal::VectorOperations::Vector_set setter(Number(), val); - dealii::internal::VectorOperations::parallel_for(setter, 0, n_elements(), thread_loop_partitioner); + const size_type this_size = n_elements(); + if (this_size>0) + { + dealii::internal::VectorOperations::Vector_set setter(Number(), val); + dealii::internal::VectorOperations::parallel_for(setter, 0, this_size, thread_loop_partitioner); + } return *this; } diff --git a/include/deal.II/lac/vector.templates.h b/include/deal.II/lac/vector.templates.h index 816296e029..385d8945bc 100644 --- a/include/deal.II/lac/vector.templates.h +++ b/include/deal.II/lac/vector.templates.h @@ -205,8 +205,11 @@ Vector::operator= (const Vector &v) if (vec_size != v.vec_size) reinit (v, true); - dealii::internal::VectorOperations::Vector_copy copier(v.val, val); - internal::VectorOperations::parallel_for(copier,0,vec_size,thread_loop_partitioner); + if (vec_size>0) + { + dealii::internal::VectorOperations::Vector_copy copier(v.val, val); + internal::VectorOperations::parallel_for(copier,0,vec_size,thread_loop_partitioner); + } return *this; } @@ -358,9 +361,11 @@ Vector::operator= (const Number s) if (s != Number()) Assert (vec_size!=0, ExcEmptyObject()); - internal::VectorOperations::Vector_set setter(s, val); - - internal::VectorOperations::parallel_for(setter,0,vec_size,thread_loop_partitioner); + if (vec_size>0) + { + internal::VectorOperations::Vector_set setter(s, val); + internal::VectorOperations::parallel_for(setter,0,vec_size,thread_loop_partitioner); + } return *this; } diff --git a/include/deal.II/lac/vector_operations_internal.h b/include/deal.II/lac/vector_operations_internal.h index 3211f8d72e..517ef3f76e 100644 --- a/include/deal.II/lac/vector_operations_internal.h +++ b/include/deal.II/lac/vector_operations_internal.h @@ -203,18 +203,22 @@ namespace internal : value(value), dst(dst) - {} + { + Assert(dst != nullptr, ExcInternalError()); + } void operator() (const size_type begin, const size_type end) const { + Assert (end>=begin, ExcInternalError()); + if (value == Number()) std::memset (dst+begin,0,(end-begin)*sizeof(Number)); else std::fill (dst+begin, dst+end, value); } - Number value; - Number *dst; + const Number value; + Number *const dst; }; template @@ -224,10 +228,15 @@ namespace internal : src(src), dst(dst) - {} + { + Assert(src!=nullptr, ExcInternalError()); + Assert(dst!=nullptr, ExcInternalError()); + } void operator() (const size_type begin, const size_type end) const { + Assert (end>=begin, ExcInternalError()); + if (std::is_same::value) std::memcpy(dst+begin, src+begin, (end-begin)*sizeof(Number)); else @@ -238,8 +247,8 @@ namespace internal } } - const OtherNumber *src; - Number *dst; + const OtherNumber *const src; + Number *const dst; }; template