Skip to content

Commit 3e14c72

Browse files
andredbebarino
authored andcommitted
clk: s2mps11: initialise clk_hw_onecell_data::num before accessing ::hws[] in probe()
With UBSAN enabled, we're getting the following trace: UBSAN: array-index-out-of-bounds in .../drivers/clk/clk-s2mps11.c:186:3 index 0 is out of range for type 'struct clk_hw *[] __counted_by(num)' (aka 'struct clk_hw *[]') This is because commit f316cdf ("clk: Annotate struct clk_hw_onecell_data with __counted_by") annotated the hws member of that struct with __counted_by, which informs the bounds sanitizer about the number of elements in hws, so that it can warn when hws is accessed out of bounds. As noted in that change, the __counted_by member must be initialised with the number of elements before the first array access happens, otherwise there will be a warning from each access prior to the initialisation because the number of elements is zero. This occurs in s2mps11_clk_probe() due to ::num being assigned after ::hws access. Move the assignment to satisfy the requirement of assign-before-access. Cc: stable@vger.kernel.org Fixes: f316cdf ("clk: Annotate struct clk_hw_onecell_data with __counted_by") Signed-off-by: André Draszik <andre.draszik@linaro.org> Link: https://lore.kernel.org/r/20250326-s2mps11-ubsan-v1-1-fcc6fce5c8a9@linaro.org Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent 2bc3ada commit 3e14c72

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

drivers/clk/clk-s2mps11.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ static int s2mps11_clk_probe(struct platform_device *pdev)
137137
if (!clk_data)
138138
return -ENOMEM;
139139

140+
clk_data->num = S2MPS11_CLKS_NUM;
141+
140142
switch (hwid) {
141143
case S2MPS11X:
142144
s2mps11_reg = S2MPS11_REG_RTC_CTRL;
@@ -186,7 +188,6 @@ static int s2mps11_clk_probe(struct platform_device *pdev)
186188
clk_data->hws[i] = &s2mps11_clks[i].hw;
187189
}
188190

189-
clk_data->num = S2MPS11_CLKS_NUM;
190191
of_clk_add_hw_provider(s2mps11_clks->clk_np, of_clk_hw_onecell_get,
191192
clk_data);
192193

0 commit comments

Comments
 (0)