Skip to content

Commit

Permalink
fixed ub access to first element of empty vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
fweik committed Jun 10, 2020
1 parent 1c09f39 commit 28a73ea
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 42 deletions.
2 changes: 1 addition & 1 deletion include/boost/mpi/collectives/all_reduce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace detail {
// implementation in this case.
// it's not clear how/if we can avoid the copy.
std::vector<T> tmp_in( out_values, out_values + n);
reduce(comm, &(tmp_in[0]), n, out_values, op, 0);
reduce(comm, detail::c_data(tmp_in), n, out_values, op, 0);
} else {
reduce(comm, in_values, n, out_values, op, 0);
}
Expand Down
12 changes: 6 additions & 6 deletions include/boost/mpi/collectives/all_to_all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ namespace detail {

// Transmit the actual data
BOOST_MPI_CHECK_RESULT(MPI_Alltoallv,
(&outgoing[0], &send_sizes[0],
&send_disps[0], MPI_PACKED,
&incoming[0], &recv_sizes[0],
&recv_disps[0], MPI_PACKED,
(detail::c_data(outgoing), detail::c_data(send_sizes),
detail::c_data(send_disps), MPI_PACKED,
detail::c_data(incoming), detail::c_data(recv_sizes),
detail::c_data(recv_disps), MPI_PACKED,
comm));

// Deserialize data from the iarchive
Expand Down Expand Up @@ -126,7 +126,7 @@ all_to_all(const communicator& comm, const std::vector<T>& in_values,
{
BOOST_ASSERT((int)in_values.size() == comm.size());
out_values.resize(comm.size());
::boost::mpi::all_to_all(comm, &in_values[0], &out_values[0]);
::boost::mpi::all_to_all(comm, detail::c_data(in_values), detail::c_data(out_values));
}

template<typename T>
Expand All @@ -143,7 +143,7 @@ all_to_all(const communicator& comm, const std::vector<T>& in_values, int n,
{
BOOST_ASSERT((int)in_values.size() == comm.size() * n);
out_values.resize(comm.size() * n);
::boost::mpi::all_to_all(comm, &in_values[0], n, &out_values[0]);
::boost::mpi::all_to_all(comm, detail::c_data(in_values), n, detail::c_data(out_values));
}

} } // end namespace boost::mpi
Expand Down
8 changes: 4 additions & 4 deletions include/boost/mpi/collectives/gatherv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ gatherv(const communicator& comm, const T* in_values, int in_size,
{
if (comm.rank() == root)
detail::gatherv_impl(comm, in_values, in_size,
out_values, &sizes[0], &displs[0],
out_values, detail::c_data(sizes), detail::c_data(displs),
root, is_mpi_datatype<T>());
else
detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
Expand All @@ -99,7 +99,7 @@ gatherv(const communicator& comm, const std::vector<T>& in_values,
T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
int root)
{
::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, displs, root);
::boost::mpi::gatherv(comm, detail::c_data(in_values), in_values.size(), out_values, sizes, displs, root);
}

template<typename T>
Expand All @@ -113,7 +113,7 @@ template<typename T>
void gatherv(const communicator& comm, const std::vector<T>& in_values, int root)
{
BOOST_ASSERT(comm.rank() != root);
detail::gatherv_impl(comm, &in_values[0], in_values.size(), root, is_mpi_datatype<T>());
detail::gatherv_impl(comm, detail::c_data(in_values), in_values.size(), root, is_mpi_datatype<T>());
}

///////////////////////
Expand All @@ -139,7 +139,7 @@ void
gatherv(const communicator& comm, const std::vector<T>& in_values,
T* out_values, const std::vector<int>& sizes, int root)
{
::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, root);
::boost::mpi::gatherv(comm, detail::c_data(in_values), in_values.size(), out_values, sizes, root);
}

} } // end namespace boost::mpi
Expand Down
4 changes: 2 additions & 2 deletions include/boost/mpi/collectives/reduce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void
reduce(const communicator & comm, std::vector<T> const & in_values, Op op,
int root)
{
reduce(comm, &in_values.front(), in_values.size(), op, root);
reduce(comm, detail::c_data(in_values), in_values.size(), op, root);
}

template<typename T, typename Op>
Expand All @@ -344,7 +344,7 @@ reduce(const communicator & comm, std::vector<T> const & in_values,
std::vector<T> & out_values, Op op, int root)
{
if (root == comm.rank()) out_values.resize(in_values.size());
reduce(comm, &in_values.front(), in_values.size(), &out_values.front(), op,
reduce(comm, detail::c_data(in_values), in_values.size(), detail::c_data(out_values), op,
root);
}

Expand Down
2 changes: 1 addition & 1 deletion include/boost/mpi/collectives/scatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void
scatter(const communicator& comm, const std::vector<T>& in_values,
T* out_values, int n, int root)
{
::boost::mpi::scatter(comm, &in_values[0], out_values, n, root);
::boost::mpi::scatter(comm, detail::c_data(in_values), out_values, n, root);
}

template<typename T>
Expand Down
4 changes: 2 additions & 2 deletions include/boost/mpi/collectives/scatterv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void
scatterv(const communicator& comm, const std::vector<T>& in_values,
const std::vector<int>& sizes, T* out_values, int root)
{
::boost::mpi::scatterv(comm, &in_values[0], sizes, out_values, root);
::boost::mpi::scatterv(comm, detail::c_data(in_values), sizes, out_values, root);
}

template<typename T>
Expand All @@ -159,7 +159,7 @@ void
scatterv(const communicator& comm, const std::vector<T>& in_values,
T* out_values, int out_size, int root)
{
::boost::mpi::scatterv(comm, &in_values[0], out_values, out_size, root);
::boost::mpi::scatterv(comm, detail::c_data(in_values), out_values, out_size, root);
}

} } // end namespace boost::mpi
Expand Down
4 changes: 2 additions & 2 deletions include/boost/mpi/detail/binary_buffer_iprimitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class BOOST_MPI_DECL binary_buffer_iprimitive

void* address ()
{
return &buffer_.front();
return detail::c_data(buffer_);
}

void const* address () const
{
return &buffer_.front();
return detail::c_data(buffer_);
}

const std::size_t& size() const
Expand Down
2 changes: 1 addition & 1 deletion include/boost/mpi/detail/binary_buffer_oprimitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BOOST_MPI_DECL binary_buffer_oprimitive

void const * address() const
{
return &buffer_.front();
return detail::c_data(buffer_);
}

const std::size_t& size() const
Expand Down
2 changes: 1 addition & 1 deletion include/boost/mpi/detail/mpi_datatype_primitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class mpi_datatype_primitive
template <class T>
static T* get_data(std::vector<T>& v)
{
return v.empty() ? 0 : &(v[0]);
return detail::c_data(v);
}

std::vector<MPI_Aint> addresses;
Expand Down
4 changes: 2 additions & 2 deletions include/boost/mpi/detail/packed_iprimitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class BOOST_MPI_DECL packed_iprimitive

void* address ()
{
return &buffer_[0];
return detail::c_data(buffer_);
}

void const* address () const
{
return &buffer_[0];
return detail::c_data(buffer_);
}

const std::size_t& size() const
Expand Down
4 changes: 2 additions & 2 deletions include/boost/mpi/detail/packed_oprimitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BOOST_MPI_DECL packed_oprimitive

void const * address() const
{
return &buffer_[0];
return detail::c_data(buffer_);
}

const std::size_t& size() const
Expand Down Expand Up @@ -114,7 +114,7 @@ class BOOST_MPI_DECL packed_oprimitive

static buffer_type::value_type* get_data(buffer_type& b)
{
return b.empty() ? 0 : &(b[0]);
return detail::c_data(b);
}

buffer_type& buffer_;
Expand Down
4 changes: 2 additions & 2 deletions include/boost/mpi/detail/request_handlers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ class request::legacy_dynamic_primitive_array_handler
// Resize our buffer and get ready to receive its data
this->extra::m_values.resize(this->extra::m_count);
BOOST_MPI_CHECK_RESULT(MPI_Irecv,
(&(this->extra::m_values[0]), this->extra::m_values.size(), get_mpi_datatype<T>(),
(detail::c_data(this->extra::m_values), this->extra::m_values.size(), get_mpi_datatype<T>(),
stat.source(), stat.tag(),
MPI_Comm(m_comm), m_requests + 1));
}
Expand All @@ -478,7 +478,7 @@ class request::legacy_dynamic_primitive_array_handler
// Resize our buffer and get ready to receive its data
this->extra::m_values.resize(this->extra::m_count);
BOOST_MPI_CHECK_RESULT(MPI_Irecv,
(&(this->extra::m_values[0]), this->extra::m_values.size(), get_mpi_datatype<T>(),
(detail::c_data(this->extra::m_values), this->extra::m_values.size(), get_mpi_datatype<T>(),
stat.source(), stat.tag(),
MPI_Comm(m_comm), m_requests + 1));
} else
Expand Down
4 changes: 2 additions & 2 deletions include/boost/mpi/graph_communicator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ graph_communicator::setup_graph(const communicator& comm, const Graph& graph,
BOOST_MPI_CHECK_RESULT(MPI_Graph_create,
((MPI_Comm)comm,
nvertices,
&indices[0],
edges.empty()? (int*)0 : &edges[0],
detail::c_data(indices),
detail::c_data(edges),
reorder,
&newcomm));
this->comm_ptr.reset(new MPI_Comm(newcomm), comm_free());
Expand Down
9 changes: 5 additions & 4 deletions include/boost/mpi/group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define BOOST_MPI_GROUP_HPP

#include <boost/mpi/exception.hpp>
#include <boost/mpi/detail/antiques.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>
#include <vector>
Expand Down Expand Up @@ -272,9 +273,9 @@ group::translate_ranks(InputIterator first, InputIterator last,
BOOST_MPI_CHECK_RESULT(MPI_Group_translate_ranks,
((MPI_Group)*this,
in_array.size(),
&in_array[0],
detail::c_data(in_array),
(MPI_Group)to_group,
&out_array[0]));
detail::c_data(out_array)));

for (std::vector<int>::size_type i = 0, n = out_array.size(); i < n; ++i)
*out++ = out_array[i];
Expand All @@ -300,7 +301,7 @@ group group::include(InputIterator first, InputIterator last)
std::vector<int> ranks(first, last);
MPI_Group result;
BOOST_MPI_CHECK_RESULT(MPI_Group_incl,
((MPI_Group)*this, ranks.size(), &ranks[0], &result));
((MPI_Group)*this, ranks.size(), detail::c_data(ranks), &result));
return group(result, /*adopt=*/true);
}

Expand All @@ -322,7 +323,7 @@ group group::exclude(InputIterator first, InputIterator last)
std::vector<int> ranks(first, last);
MPI_Group result;
BOOST_MPI_CHECK_RESULT(MPI_Group_excl,
((MPI_Group)*this, ranks.size(), &ranks[0], &result));
((MPI_Group)*this, ranks.size(), detail::c_data(ranks), &result));
return group(result, /*adopt=*/true);
}

Expand Down
18 changes: 9 additions & 9 deletions include/boost/mpi/nonblocking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ wait_any(ForwardIterator first, ForwardIterator last)
int index;
status stat;
BOOST_MPI_CHECK_RESULT(MPI_Waitany,
(n, &requests[0], &index, &stat.m_status));
(n, detail::c_data(requests), &index, &stat.m_status));

// We don't have a notion of empty requests or status objects,
// so this is an error.
Expand Down Expand Up @@ -222,8 +222,8 @@ wait_all(ForwardIterator first, ForwardIterator last, OutputIterator out)
// Let MPI wait until all of these operations completes.
std::vector<MPI_Status> stats(num_outstanding_requests);
BOOST_MPI_CHECK_RESULT(MPI_Waitall,
(num_outstanding_requests, &requests[0],
&stats[0]));
(num_outstanding_requests, detail::c_data(requests),
detail::c_data(stats)));

for (std::vector<MPI_Status>::iterator i = stats.begin();
i != stats.end(); ++i, ++out) {
Expand Down Expand Up @@ -289,7 +289,7 @@ wait_all(ForwardIterator first, ForwardIterator last)

// Let MPI wait until all of these operations completes.
BOOST_MPI_CHECK_RESULT(MPI_Waitall,
(num_outstanding_requests, &requests[0],
(num_outstanding_requests, detail::c_data(requests),
MPI_STATUSES_IGNORE));

// Signal completion
Expand Down Expand Up @@ -346,7 +346,7 @@ test_all(ForwardIterator first, ForwardIterator last, OutputIterator out)
int flag = 0;
int n = requests.size();
std::vector<MPI_Status> stats(n);
BOOST_MPI_CHECK_RESULT(MPI_Testall, (n, &requests[0], &flag, &stats[0]));
BOOST_MPI_CHECK_RESULT(MPI_Testall, (n, detail::c_data(requests), &flag, detail::c_data(stats)));
if (flag) {
for (int i = 0; i < n; ++i, ++out) {
status stat;
Expand Down Expand Up @@ -379,7 +379,7 @@ test_all(ForwardIterator first, ForwardIterator last)
int flag = 0;
int n = requests.size();
BOOST_MPI_CHECK_RESULT(MPI_Testall,
(n, &requests[0], &flag, MPI_STATUSES_IGNORE));
(n, detail::c_data(requests), &flag, MPI_STATUSES_IGNORE));
return flag != 0;
}

Expand Down Expand Up @@ -483,8 +483,8 @@ wait_some(BidirectionalIterator first, BidirectionalIterator last,
// Let MPI wait until some of these operations complete.
int num_completed;
BOOST_MPI_CHECK_RESULT(MPI_Waitsome,
(n, &requests[0], &num_completed, &indices[0],
&stats[0]));
(n, detail::c_data(requests), &num_completed, detail::c_data(indices),
detail::c_data(stats)));

// Translate the index-based result of MPI_Waitsome into a
// partitioning on the requests.
Expand Down Expand Up @@ -591,7 +591,7 @@ wait_some(BidirectionalIterator first, BidirectionalIterator last)
// Let MPI wait until some of these operations complete.
int num_completed;
BOOST_MPI_CHECK_RESULT(MPI_Waitsome,
(n, &requests[0], &num_completed, &indices[0],
(n, detail::c_data(requests), &num_completed, detail::c_data(indices),
MPI_STATUSES_IGNORE));

// Translate the index-based result of MPI_Waitsome into a
Expand Down
3 changes: 2 additions & 1 deletion src/cartesian_communicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
#include <cassert>

#include <boost/mpi/cartesian_communicator.hpp>
#include <boost/mpi/detail/antiques.hpp>

namespace boost { namespace mpi {

namespace {
template <typename T, typename A>
T* c_data(std::vector<T,A>& v) { return &(v[0]); }
T* c_data(std::vector<T,A>& v) { return c_data(v); }
}

std::ostream&
Expand Down

0 comments on commit 28a73ea

Please sign in to comment.