From 90f0420382e15124ec84545dd17bc235431eb079 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 16 Jul 2014 11:35:32 +0000 Subject: [PATCH] math: fix uninitialised variable in arraynd. --- src/lol/math/arraynd.h | 35 ++++++++++++++++++----------------- test/unit/arraynd.cpp | 10 +++++++++- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/lol/math/arraynd.h b/src/lol/math/arraynd.h index 2477e9b9..e012d0a9 100644 --- a/src/lol/math/arraynd.h +++ b/src/lol/math/arraynd.h @@ -107,25 +107,20 @@ public: typedef array super; typedef typename super::element_t element_t; - inline arraynd() : - super(), - m_sizes() + inline arraynd() { } - inline arraynd(vec_t sizes, element_t e = element_t()) : - super(), - m_sizes(sizes) + inline arraynd(vec_t sizes, element_t e = element_t()) + : m_sizes(sizes) { - SetSize(m_sizes, e); + FixSizes(e); } - inline arraynd(arraynd_initializer initializer) : - super(), - m_sizes() + inline arraynd(arraynd_initializer initializer) { initializer.FillSizes(&m_sizes[0]); - SetSize(m_sizes); + FixSizes(); initializer.FillValues(&super::operator[](0), &m_sizes[0], 1); } @@ -210,12 +205,8 @@ public: * FIXME: data gets scrambled; should we care? */ inline void SetSize(vec_t sizes, element_t e = element_t()) { - size_t total_size = 1; - - for (auto size : sizes) - total_size *= size; - - this->Resize(total_size, e); + m_sizes = sizes; + FixSizes(e); } inline vec_t GetSize() const @@ -230,6 +221,16 @@ public: inline int Bytes() const { return super::Bytes(); } private: + inline void FixSizes(element_t e = element_t()) + { + size_t total_size = 1; + + for (auto size : m_sizes) + total_size *= size; + + this->Resize(total_size, e); + } + vec_t m_sizes; }; diff --git a/test/unit/arraynd.cpp b/test/unit/arraynd.cpp index a84133e1..ba67a2f7 100644 --- a/test/unit/arraynd.cpp +++ b/test/unit/arraynd.cpp @@ -27,16 +27,24 @@ LOLUNIT_FIXTURE(ArrayNDTest) LOLUNIT_TEST(Array2D) { arraynd<2, int> a; - a.SetSize(vec_t(1, 2)); + a.SetSize(vec_t(2, 2)); + /* Non-const accessors */ a[0][0] = 1; a[0][1] = 2; + a[1][0] = 3; + a[1][1] = 4; LOLUNIT_ASSERT_EQUAL(a[0][0], 1); LOLUNIT_ASSERT_EQUAL(a[0][1], 2); + LOLUNIT_ASSERT_EQUAL(a[1][0], 3); + LOLUNIT_ASSERT_EQUAL(a[1][1], 4); + /* Const accessors */ arraynd<2, int> const &b = a; LOLUNIT_ASSERT_EQUAL(b[0][0], 1); LOLUNIT_ASSERT_EQUAL(b[0][1], 2); + LOLUNIT_ASSERT_EQUAL(b[1][0], 3); + LOLUNIT_ASSERT_EQUAL(b[1][1], 4); } LOLUNIT_TEST(ArrayNDCreate)