Skip to content

Commit 9a5f09a

Browse files
Boris Brezillonrichardweinberger
authored andcommitted
UBI: add an helper to check lnum validity
ubi_leb_valid() is here to replace the lnum < 0 || lnum >= vol->reserved_pebs checks. Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com> Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent 2d78aee commit 9a5f09a

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

drivers/mtd/ubi/cdev.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
454454

455455
/* Validate the request */
456456
err = -EINVAL;
457-
if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
457+
if (!ubi_leb_valid(vol, req.lnum) ||
458458
req.bytes < 0 || req.bytes > vol->usable_leb_size)
459459
break;
460460

@@ -485,7 +485,7 @@ static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
485485
break;
486486
}
487487

488-
if (lnum < 0 || lnum >= vol->reserved_pebs) {
488+
if (!ubi_leb_valid(vol, lnum)) {
489489
err = -EINVAL;
490490
break;
491491
}

drivers/mtd/ubi/kapi.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
538538
if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
539539
return -EROFS;
540540

541-
if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
541+
if (!ubi_leb_valid(vol, lnum) || offset < 0 || len < 0 ||
542542
offset + len > vol->usable_leb_size ||
543543
offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
544544
return -EINVAL;
@@ -583,7 +583,7 @@ int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
583583
if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
584584
return -EROFS;
585585

586-
if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
586+
if (!ubi_leb_valid(vol, lnum) || len < 0 ||
587587
len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
588588
return -EINVAL;
589589

@@ -620,7 +620,7 @@ int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
620620
if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
621621
return -EROFS;
622622

623-
if (lnum < 0 || lnum >= vol->reserved_pebs)
623+
if (!ubi_leb_valid(vol, lnum))
624624
return -EINVAL;
625625

626626
if (vol->upd_marker)
@@ -680,7 +680,7 @@ int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
680680
if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
681681
return -EROFS;
682682

683-
if (lnum < 0 || lnum >= vol->reserved_pebs)
683+
if (!ubi_leb_valid(vol, lnum))
684684
return -EINVAL;
685685

686686
if (vol->upd_marker)
@@ -716,7 +716,7 @@ int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
716716
if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
717717
return -EROFS;
718718

719-
if (lnum < 0 || lnum >= vol->reserved_pebs)
719+
if (!ubi_leb_valid(vol, lnum))
720720
return -EINVAL;
721721

722722
if (vol->upd_marker)
@@ -751,7 +751,7 @@ int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
751751

752752
dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
753753

754-
if (lnum < 0 || lnum >= vol->reserved_pebs)
754+
if (!ubi_leb_valid(vol, lnum))
755755
return -EINVAL;
756756

757757
if (vol->upd_marker)

drivers/mtd/ubi/ubi.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,11 @@ void ubi_update_reserved(struct ubi_device *ubi);
843843
void ubi_calculate_reserved(struct ubi_device *ubi);
844844
int ubi_check_pattern(const void *buf, uint8_t patt, int size);
845845

846+
static inline bool ubi_leb_valid(struct ubi_volume *vol, int lnum)
847+
{
848+
return lnum >= 0 && lnum < vol->reserved_pebs;
849+
}
850+
846851
/* eba.c */
847852
int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
848853
int lnum);

0 commit comments

Comments
 (0)