mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-07-19 19:34:29 +08:00
Fix undefined behavior in Block access
(cherry picked from commit a1cdcdb038cda474aefb900171222254599e9dd8)
This commit is contained in:
parent
b26ada1e03
commit
90dce8dfa3
@ -334,6 +334,17 @@ class BlockImpl_dense<XprType,BlockRows,BlockCols, InnerPanel,true>
|
|||||||
enum {
|
enum {
|
||||||
XprTypeIsRowMajor = (int(traits<XprType>::Flags)&RowMajorBit) != 0
|
XprTypeIsRowMajor = (int(traits<XprType>::Flags)&RowMajorBit) != 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** \internal Returns base+offset (unless base is null, in which case returns null).
|
||||||
|
* Adding an offset to nullptr is undefined behavior, so we must avoid it.
|
||||||
|
*/
|
||||||
|
template <typename Scalar>
|
||||||
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR EIGEN_ALWAYS_INLINE
|
||||||
|
static Scalar* add_to_nullable_pointer(Scalar* base, Index offset)
|
||||||
|
{
|
||||||
|
return base != NULL ? base+offset : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef MapBase<BlockType> Base;
|
typedef MapBase<BlockType> Base;
|
||||||
@ -344,8 +355,9 @@ class BlockImpl_dense<XprType,BlockRows,BlockCols, InnerPanel,true>
|
|||||||
*/
|
*/
|
||||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||||
BlockImpl_dense(XprType& xpr, Index i)
|
BlockImpl_dense(XprType& xpr, Index i)
|
||||||
: Base(xpr.data() + i * ( ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && (!XprTypeIsRowMajor))
|
: Base(add_to_nullable_pointer(xpr.data(),
|
||||||
|| ((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && ( XprTypeIsRowMajor)) ? xpr.innerStride() : xpr.outerStride()),
|
i * ( ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && (!XprTypeIsRowMajor))
|
||||||
|
|| ((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && ( XprTypeIsRowMajor)) ? xpr.innerStride() : xpr.outerStride())),
|
||||||
BlockRows==1 ? 1 : xpr.rows(),
|
BlockRows==1 ? 1 : xpr.rows(),
|
||||||
BlockCols==1 ? 1 : xpr.cols()),
|
BlockCols==1 ? 1 : xpr.cols()),
|
||||||
m_xpr(xpr),
|
m_xpr(xpr),
|
||||||
@ -359,7 +371,8 @@ class BlockImpl_dense<XprType,BlockRows,BlockCols, InnerPanel,true>
|
|||||||
*/
|
*/
|
||||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||||
BlockImpl_dense(XprType& xpr, Index startRow, Index startCol)
|
BlockImpl_dense(XprType& xpr, Index startRow, Index startCol)
|
||||||
: Base(xpr.data()+xpr.innerStride()*(XprTypeIsRowMajor?startCol:startRow) + xpr.outerStride()*(XprTypeIsRowMajor?startRow:startCol)),
|
: Base(add_to_nullable_pointer(xpr.data(),
|
||||||
|
xpr.innerStride()*(XprTypeIsRowMajor?startCol:startRow) + xpr.outerStride()*(XprTypeIsRowMajor?startRow:startCol))),
|
||||||
m_xpr(xpr), m_startRow(startRow), m_startCol(startCol)
|
m_xpr(xpr), m_startRow(startRow), m_startCol(startCol)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
@ -371,7 +384,9 @@ class BlockImpl_dense<XprType,BlockRows,BlockCols, InnerPanel,true>
|
|||||||
BlockImpl_dense(XprType& xpr,
|
BlockImpl_dense(XprType& xpr,
|
||||||
Index startRow, Index startCol,
|
Index startRow, Index startCol,
|
||||||
Index blockRows, Index blockCols)
|
Index blockRows, Index blockCols)
|
||||||
: Base(xpr.data()+xpr.innerStride()*(XprTypeIsRowMajor?startCol:startRow) + xpr.outerStride()*(XprTypeIsRowMajor?startRow:startCol), blockRows, blockCols),
|
: Base(add_to_nullable_pointer(xpr.data(),
|
||||||
|
xpr.innerStride()*(XprTypeIsRowMajor?startCol:startRow) + xpr.outerStride()*(XprTypeIsRowMajor?startRow:startCol)),
|
||||||
|
blockRows, blockCols),
|
||||||
m_xpr(xpr), m_startRow(startRow), m_startCol(startCol)
|
m_xpr(xpr), m_startRow(startRow), m_startCol(startCol)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
|
@ -196,6 +196,7 @@ public:
|
|||||||
pointer_based_stl_iterator() EIGEN_NO_THROW : m_ptr(0) {}
|
pointer_based_stl_iterator() EIGEN_NO_THROW : m_ptr(0) {}
|
||||||
pointer_based_stl_iterator(XprType& xpr, Index index) EIGEN_NO_THROW : m_incr(xpr.innerStride())
|
pointer_based_stl_iterator(XprType& xpr, Index index) EIGEN_NO_THROW : m_incr(xpr.innerStride())
|
||||||
{
|
{
|
||||||
|
eigen_assert(xpr.data() != NULL || index == 0 || m_incr.value() == 0);
|
||||||
m_ptr = xpr.data() + index * m_incr.value();
|
m_ptr = xpr.data() + index * m_incr.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user