From ac4253d6a99cc281b91f109273c88027c8bfea3b Mon Sep 17 00:00:00 2001 From: relan Date: Sat, 9 Oct 2021 10:13:59 +0300 Subject: [PATCH] Make error handling in find_slot() more robust. check_slot() can potentially return any value besides 0, -EIO and -EINVAL. --- libexfat/node.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/libexfat/node.c b/libexfat/node.c index a500524a..08cd2f54 100644 --- a/libexfat/node.c +++ b/libexfat/node.c @@ -858,21 +858,22 @@ static int find_slot(struct exfat* ef, struct exfat_node* dir, if (contiguous++ == 0) *offset = (off_t) i * sizeof(struct exfat_entry); if (contiguous == n) + { + int rc; + /* suitable slot is found, check that it's not occupied */ - switch (check_slot(ef, dir, *offset, n)) + rc = check_slot(ef, dir, *offset, n); + if (rc == -EINVAL) { - case 0: - free(dmap); - return 0; - case -EIO: - free(dmap); - return -EIO; - case -EINVAL: /* slot at (i-n) is occupied, go back and check (i-n+1) */ i -= contiguous - 1; contiguous = 0; - break; + } else { + /* slot is free or an error occurred */ + free(dmap); + return rc; } + } } else contiguous = 0;