Skip to content

Commit 1ff059b

Browse files
committed
[compute] Explicitly initialize dims_ in constructors to avoid uninitialized warnings
This commit explicitly initialize dims_ in Shape constructors to avoid uninitialized warnings. - Set dims_ using std::array or std::vector in the (int, int32_t), (int, const int32_t*), and initializer-list constructors to ensure proper initialization before calling Resize(). - Added a check in Resize() using dims_.valueless_by_exception() to handle any uninitialized state. ONE-DCO-1.0-Signed-off-by: ragmani <ragmani0216@gmail.com>
1 parent dda03b0 commit 1ff059b

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

  • runtime/compute/cker/include/cker

runtime/compute/cker/include/cker/Shape.h

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ class Shape
6262
// Constructor that creates a shape of given size and fills all dimensions with "value".
6363
Shape(int shape_size, int32_t value) : _size(0)
6464
{
65-
Resize(shape_size);
65+
if (shape_size <= kMaxSmallSize)
66+
{
67+
dims_ = std::array<int32_t, kMaxSmallSize>{};
68+
}
69+
else
70+
{
71+
dims_ = std::vector<int32_t>(shape_size);
72+
}
73+
6674
for (int i = 0; i < shape_size; ++i)
6775
{
6876
SetDim(i, value);
@@ -72,12 +80,38 @@ class Shape
7280
// Constructor that creates a shape from an array of dimension data.
7381
Shape(int dimensions_count, const int32_t *dims_data) : _size(0)
7482
{
83+
// Explicitly initialize dims_ based on dimensions_count to avoid uninitialized state.
84+
if (dimensions_count <= kMaxSmallSize)
85+
{
86+
dims_ = std::array<int32_t, kMaxSmallSize>{};
87+
}
88+
else
89+
{
90+
dims_ = std::vector<int32_t>(dimensions_count);
91+
}
92+
7593
ReplaceWith(dimensions_count, dims_data);
7694
}
7795

7896
// Initializer list constructor.
7997
// Marked explicit to avoid unintended overload resolution.
80-
Shape(const std::initializer_list<int> init_list) : _size(0) { BuildFrom(init_list); }
98+
Shape(const std::initializer_list<int> init_list) : _size(0)
99+
{
100+
const auto size = static_cast<int>(std::distance(init_list.begin(), init_list.end()));
101+
102+
// Explicitly initialize dims_ based on the initializer list size to prevent
103+
// "maybe uninitialized" warnings when BuildFrom() is invoked.
104+
if (size <= kMaxSmallSize)
105+
{
106+
dims_ = std::array<int32_t, kMaxSmallSize>{};
107+
}
108+
else
109+
{
110+
dims_ = std::vector<int32_t>(size);
111+
}
112+
113+
BuildFrom(init_list);
114+
}
81115

82116
// Copy constructor
83117
Shape(const Shape &other) : _size(other._size)
@@ -169,6 +203,16 @@ class Shape
169203
// Resizes the shape to dimensions_count while preserving existing data.
170204
inline void Resize(int dimensions_count)
171205
{
206+
// If dims_ is in a valueless state (i.e. not yet initialized or lost due to an exception),
207+
// initialize dims_ explicitly based on dimensions_count to ensure it is in a valid state.
208+
if (dims_.valueless_by_exception())
209+
{
210+
if (dimensions_count <= kMaxSmallSize)
211+
dims_ = std::array<int32_t, kMaxSmallSize>{};
212+
else
213+
dims_ = std::vector<int32_t>(dimensions_count);
214+
}
215+
172216
std::vector<int32_t> oldDims;
173217
oldDims.reserve(_size);
174218
if (_size <= kMaxSmallSize)

0 commit comments

Comments
 (0)