Skip to content

Commit 08df2fd

Browse files
Alain Volmatkartben
authored andcommitted
video: common: add __ASSERT_NO_MSG in video api function entry
Protect video API functions via __ASSERT_NO_MSG call to ensure that required pointers are valid when entering functions. Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
1 parent 7b42946 commit 08df2fd

File tree

3 files changed

+103
-14
lines changed

3 files changed

+103
-14
lines changed

drivers/video/video_common.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ void video_buffer_release(struct video_buffer *vbuf)
8282
struct mem_block *block = NULL;
8383
int i;
8484

85+
__ASSERT_NO_MSG(vbuf != NULL);
86+
8587
/* vbuf to block */
8688
for (i = 0; i < ARRAY_SIZE(video_block); i++) {
8789
if (video_block[i].data == vbuf->buffer) {
@@ -99,6 +101,10 @@ void video_buffer_release(struct video_buffer *vbuf)
99101
int video_format_caps_index(const struct video_format_cap *fmts, const struct video_format *fmt,
100102
size_t *idx)
101103
{
104+
__ASSERT_NO_MSG(fmts != NULL);
105+
__ASSERT_NO_MSG(fmt != NULL);
106+
__ASSERT_NO_MSG(idx != NULL);
107+
102108
for (int i = 0; fmts[i].pixelformat != 0; i++) {
103109
if (fmts[i].pixelformat == fmt->pixelformat &&
104110
IN_RANGE(fmt->width, fmts[i].width_min, fmts[i].width_max) &&
@@ -114,6 +120,10 @@ void video_closest_frmival_stepwise(const struct video_frmival_stepwise *stepwis
114120
const struct video_frmival *desired,
115121
struct video_frmival *match)
116122
{
123+
__ASSERT_NO_MSG(stepwise != NULL);
124+
__ASSERT_NO_MSG(desired != NULL);
125+
__ASSERT_NO_MSG(match != NULL);
126+
117127
uint64_t min = stepwise->min.numerator;
118128
uint64_t max = stepwise->max.numerator;
119129
uint64_t step = stepwise->step.numerator;
@@ -136,6 +146,9 @@ void video_closest_frmival_stepwise(const struct video_frmival_stepwise *stepwis
136146

137147
void video_closest_frmival(const struct device *dev, struct video_frmival_enum *match)
138148
{
149+
__ASSERT_NO_MSG(dev != NULL);
150+
__ASSERT_NO_MSG(match != NULL);
151+
139152
struct video_frmival desired = match->discrete;
140153
struct video_frmival_enum fie = {.format = match->format};
141154
uint64_t best_diff_nsec = INT32_MAX;
@@ -207,6 +220,8 @@ int video_read_cci_reg(const struct i2c_dt_spec *i2c, uint32_t reg_addr, uint32_
207220
uint8_t *data_ptr;
208221
int ret;
209222

223+
__ASSERT_NO_MSG(i2c != NULL);
224+
__ASSERT_NO_MSG(reg_data != NULL);
210225
__ASSERT(addr_size > 0, "The address must have a address size flag");
211226
__ASSERT(data_size > 0, "The address must have a data size flag");
212227

@@ -246,6 +261,9 @@ static int video_write_reg_retry(const struct i2c_dt_spec *i2c, uint8_t *buf_w,
246261
{
247262
int ret;
248263

264+
__ASSERT_NO_MSG(i2c != NULL);
265+
__ASSERT_NO_MSG(buf_w != NULL);
266+
249267
for (int i = 0;; i++) {
250268
ret = i2c_write_dt(i2c, buf_w, size);
251269
if (ret == 0) {
@@ -272,6 +290,7 @@ int video_write_cci_reg(const struct i2c_dt_spec *i2c, uint32_t reg_addr, uint32
272290
uint8_t *data_ptr;
273291
int ret;
274292

293+
__ASSERT_NO_MSG(i2c != NULL);
275294
__ASSERT(addr_size > 0, "The address must have a address size flag");
276295
__ASSERT(data_size > 0, "The address must have a data size flag");
277296

@@ -326,6 +345,8 @@ int video_write_cci_multiregs(const struct i2c_dt_spec *i2c, const struct video_
326345
{
327346
int ret;
328347

348+
__ASSERT_NO_MSG(regs != NULL);
349+
329350
for (int i = 0; i < num_regs; i++) {
330351
ret = video_write_cci_reg(i2c, regs[i].addr, regs[i].data);
331352
if (ret < 0) {
@@ -341,6 +362,8 @@ int video_write_cci_multiregs8(const struct i2c_dt_spec *i2c, const struct video
341362
{
342363
int ret;
343364

365+
__ASSERT_NO_MSG(regs != NULL);
366+
344367
for (int i = 0; i < num_regs; i++) {
345368
ret = video_write_cci_reg(i2c, regs[i].addr | VIDEO_REG_ADDR8_DATA8, regs[i].data);
346369
if (ret < 0) {
@@ -356,6 +379,8 @@ int video_write_cci_multiregs16(const struct i2c_dt_spec *i2c, const struct vide
356379
{
357380
int ret;
358381

382+
__ASSERT_NO_MSG(regs != NULL);
383+
359384
for (int i = 0; i < num_regs; i++) {
360385
ret = video_write_cci_reg(i2c, regs[i].addr | VIDEO_REG_ADDR16_DATA8, regs[i].data);
361386
if (ret < 0) {

drivers/video/video_ctrls.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,12 @@ int video_init_ctrl(struct video_ctrl *ctrl, const struct device *dev, uint32_t
120120
uint32_t flags;
121121
enum video_ctrl_type type;
122122
struct video_ctrl *vc;
123-
struct video_device *vdev = video_find_vdev(dev);
123+
struct video_device *vdev;
124124

125+
__ASSERT_NO_MSG(dev != NULL);
126+
__ASSERT_NO_MSG(ctrl != NULL);
127+
128+
vdev = video_find_vdev(dev);
125129
if (!vdev) {
126130
return -EINVAL;
127131
}
@@ -261,6 +265,9 @@ int video_get_ctrl(const struct device *dev, struct video_control *control)
261265
{
262266
struct video_ctrl *ctrl = NULL;
263267

268+
__ASSERT_NO_MSG(dev != NULL);
269+
__ASSERT_NO_MSG(control != NULL);
270+
264271
int ret = video_find_ctrl(dev, control->id, &ctrl);
265272

266273
if (ret) {
@@ -299,11 +306,15 @@ int video_get_ctrl(const struct device *dev, struct video_control *control)
299306
int video_set_ctrl(const struct device *dev, struct video_control *control)
300307
{
301308
struct video_ctrl *ctrl = NULL;
302-
int ret = video_find_ctrl(dev, control->id, &ctrl);
309+
int ret;
303310
uint8_t i = 0;
304311
int32_t val = 0;
305312
int64_t val64 = 0;
306313

314+
__ASSERT_NO_MSG(dev != NULL);
315+
__ASSERT_NO_MSG(control != NULL);
316+
317+
ret = video_find_ctrl(dev, control->id, &ctrl);
307318
if (ret) {
308319
return ret;
309320
}
@@ -516,6 +527,9 @@ int video_query_ctrl(const struct device *dev, struct video_ctrl_query *cq)
516527
struct video_device *vdev;
517528
struct video_ctrl *ctrl = NULL;
518529

530+
__ASSERT_NO_MSG(dev != NULL);
531+
__ASSERT_NO_MSG(cq != NULL);
532+
519533
if (cq->id & VIDEO_CTRL_FLAG_NEXT_CTRL) {
520534
vdev = video_find_vdev(dev);
521535
cq->id &= ~VIDEO_CTRL_FLAG_NEXT_CTRL;
@@ -552,7 +566,8 @@ void video_print_ctrl(const struct device *const dev, const struct video_ctrl_qu
552566
const char *type = NULL;
553567
char typebuf[8];
554568

555-
__ASSERT(dev && cq, "Invalid arguments");
569+
__ASSERT_NO_MSG(dev != NULL);
570+
__ASSERT_NO_MSG(cq != NULL);
556571

557572
/* Get type of the control */
558573
switch (cq->type) {

include/zephyr/drivers/video.h

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,12 @@ __subsystem struct video_driver_api {
360360
*/
361361
static inline int video_set_format(const struct device *dev, struct video_format *fmt)
362362
{
363-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
363+
const struct video_driver_api *api;
364+
365+
__ASSERT_NO_MSG(dev != NULL);
366+
__ASSERT_NO_MSG(fmt != NULL);
364367

368+
api = (const struct video_driver_api *)dev->api;
365369
if (api->set_format == NULL) {
366370
return -ENOSYS;
367371
}
@@ -381,8 +385,12 @@ static inline int video_set_format(const struct device *dev, struct video_format
381385
*/
382386
static inline int video_get_format(const struct device *dev, struct video_format *fmt)
383387
{
384-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
388+
const struct video_driver_api *api;
385389

390+
__ASSERT_NO_MSG(dev != NULL);
391+
__ASSERT_NO_MSG(fmt != NULL);
392+
393+
api = (const struct video_driver_api *)dev->api;
386394
if (api->get_format == NULL) {
387395
return -ENOSYS;
388396
}
@@ -408,8 +416,12 @@ static inline int video_get_format(const struct device *dev, struct video_format
408416
*/
409417
static inline int video_set_frmival(const struct device *dev, struct video_frmival *frmival)
410418
{
411-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
419+
const struct video_driver_api *api;
420+
421+
__ASSERT_NO_MSG(dev != NULL);
422+
__ASSERT_NO_MSG(frmival != NULL);
412423

424+
api = (const struct video_driver_api *)dev->api;
413425
if (api->set_frmival == NULL) {
414426
return -ENOSYS;
415427
}
@@ -432,8 +444,12 @@ static inline int video_set_frmival(const struct device *dev, struct video_frmiv
432444
*/
433445
static inline int video_get_frmival(const struct device *dev, struct video_frmival *frmival)
434446
{
435-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
447+
const struct video_driver_api *api;
448+
449+
__ASSERT_NO_MSG(dev != NULL);
450+
__ASSERT_NO_MSG(frmival != NULL);
436451

452+
api = (const struct video_driver_api *)dev->api;
437453
if (api->get_frmival == NULL) {
438454
return -ENOSYS;
439455
}
@@ -460,8 +476,13 @@ static inline int video_get_frmival(const struct device *dev, struct video_frmiv
460476
*/
461477
static inline int video_enum_frmival(const struct device *dev, struct video_frmival_enum *fie)
462478
{
463-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
479+
const struct video_driver_api *api;
464480

481+
__ASSERT_NO_MSG(dev != NULL);
482+
__ASSERT_NO_MSG(fie != NULL);
483+
__ASSERT_NO_MSG(fie->format != NULL);
484+
485+
api = (const struct video_driver_api *)dev->api;
465486
if (api->enum_frmival == NULL) {
466487
return -ENOSYS;
467488
}
@@ -486,6 +507,11 @@ static inline int video_enqueue(const struct device *dev, struct video_buffer *b
486507
{
487508
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
488509

510+
__ASSERT_NO_MSG(dev != NULL);
511+
__ASSERT_NO_MSG(buf != NULL);
512+
__ASSERT_NO_MSG(buf->buffer != NULL);
513+
514+
api = (const struct video_driver_api *)dev->api;
489515
if (api->enqueue == NULL) {
490516
return -ENOSYS;
491517
}
@@ -510,8 +536,12 @@ static inline int video_enqueue(const struct device *dev, struct video_buffer *b
510536
static inline int video_dequeue(const struct device *dev, struct video_buffer **buf,
511537
k_timeout_t timeout)
512538
{
513-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
539+
const struct video_driver_api *api;
514540

541+
__ASSERT_NO_MSG(dev != NULL);
542+
__ASSERT_NO_MSG(buf != NULL);
543+
544+
api = (const struct video_driver_api *)dev->api;
515545
if (api->dequeue == NULL) {
516546
return -ENOSYS;
517547
}
@@ -534,8 +564,11 @@ static inline int video_dequeue(const struct device *dev, struct video_buffer **
534564
*/
535565
static inline int video_flush(const struct device *dev, bool cancel)
536566
{
537-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
567+
const struct video_driver_api *api;
568+
569+
__ASSERT_NO_MSG(dev != NULL);
538570

571+
api = (const struct video_driver_api *)dev->api;
539572
if (api->flush == NULL) {
540573
return -ENOSYS;
541574
}
@@ -560,8 +593,11 @@ static inline int video_flush(const struct device *dev, bool cancel)
560593
*/
561594
static inline int video_stream_start(const struct device *dev, enum video_buf_type type)
562595
{
563-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
596+
const struct video_driver_api *api;
597+
598+
__ASSERT_NO_MSG(dev != NULL);
564599

600+
api = (const struct video_driver_api *)dev->api;
565601
if (api->set_stream == NULL) {
566602
return -ENOSYS;
567603
}
@@ -583,9 +619,12 @@ static inline int video_stream_start(const struct device *dev, enum video_buf_ty
583619
*/
584620
static inline int video_stream_stop(const struct device *dev, enum video_buf_type type)
585621
{
586-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
622+
const struct video_driver_api *api;
587623
int ret;
588624

625+
__ASSERT_NO_MSG(dev != NULL);
626+
627+
api = (const struct video_driver_api *)dev->api;
589628
if (api->set_stream == NULL) {
590629
return -ENOSYS;
591630
}
@@ -606,8 +645,12 @@ static inline int video_stream_stop(const struct device *dev, enum video_buf_typ
606645
*/
607646
static inline int video_get_caps(const struct device *dev, struct video_caps *caps)
608647
{
609-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
648+
const struct video_driver_api *api;
649+
650+
__ASSERT_NO_MSG(dev != NULL);
651+
__ASSERT_NO_MSG(caps != NULL);
610652

653+
api = (const struct video_driver_api *)dev->api;
611654
if (api->get_caps == NULL) {
612655
return -ENOSYS;
613656
}
@@ -696,8 +739,12 @@ void video_print_ctrl(const struct device *const dev, const struct video_ctrl_qu
696739
*/
697740
static inline int video_set_signal(const struct device *dev, struct k_poll_signal *sig)
698741
{
699-
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
742+
const struct video_driver_api *api;
743+
744+
__ASSERT_NO_MSG(dev != NULL);
745+
__ASSERT_NO_MSG(sig != NULL);
700746

747+
api = (const struct video_driver_api *)dev->api;
701748
if (api->set_signal == NULL) {
702749
return -ENOSYS;
703750
}
@@ -755,6 +802,8 @@ int video_format_caps_index(const struct video_format_cap *fmts, const struct vi
755802
*/
756803
static inline uint64_t video_frmival_nsec(const struct video_frmival *frmival)
757804
{
805+
__ASSERT_NO_MSG(frmival != NULL);
806+
758807
return (uint64_t)NSEC_PER_SEC * frmival->numerator / frmival->denominator;
759808
}
760809

0 commit comments

Comments
 (0)