Skip to content

Commit af76ac8

Browse files
sudarsan-22danieldegrasse
authored andcommitted
video: common: prevent divide-by-zero video_closest_frmival_stepwise()
Adds an assertion and runtime log to ensure 'step' is not zero before using it in division,preventing undefined behavior without modifying the public API. Avoids a signature change (void → int) to preserve API stability for Zephyr 4.2.A more complete solution can be proposed for 4.3. CID: 444378 Signed-off-by: sudarsan N <sudarsansamy2002@gmail.com>
1 parent 2c097a4 commit af76ac8

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

drivers/video/video_common.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ void video_closest_frmival_stepwise(const struct video_frmival_stepwise *stepwis
136136
step *= stepwise->min.denominator * stepwise->max.denominator * desired->denominator;
137137
goal *= stepwise->min.denominator * stepwise->max.denominator * stepwise->step.denominator;
138138

139+
__ASSERT_NO_MSG(step != 0U);
140+
/* Prevent division by zero */
141+
if (step == 0U) {
142+
return;
143+
}
139144
/* Saturate the desired value to the min/max supported */
140145
goal = CLAMP(goal, min, max);
141146

0 commit comments

Comments
 (0)