OSDN Git Service

Add node start cluster checks.
authorrelan <relan@users.noreply.github.com>
Tue, 19 Jul 2016 15:29:25 +0000 (18:29 +0300)
committerrelan <relan@users.noreply.github.com>
Mon, 5 Dec 2016 10:38:30 +0000 (13:38 +0300)
libexfat/node.c

index 4a3ee40..c506fad 100644 (file)
@@ -101,6 +101,13 @@ static int opendir(struct exfat* ef, const struct exfat_node* dir,
                exfat_get_name(dir, buffer, sizeof(buffer) - 1);
                exfat_bug("'%s' is not a directory", buffer);
        }
+       if (CLUSTER_INVALID(dir->start_cluster))
+       {
+               exfat_get_name(dir, buffer, sizeof(buffer) - 1);
+               exfat_error("'%s' directory starts with invalid cluster %#x", buffer,
+                               dir->start_cluster);
+               return -EIO;
+       }
        it->cluster = dir->start_cluster;
        it->offset = 0;
        it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
@@ -230,6 +237,27 @@ static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
                ret = false;
        }
 
+       /*
+          Empty file must have zero start cluster. Non-empty file must start
+          with a valid cluster. Directories cannot be empty (i.e. must always
+          have a valid start cluster), but we will check this later in opendir()
+          to give user a chance to read current directory.
+       */
+       if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
+       {
+               exfat_get_name(node, buffer, sizeof(buffer) - 1);
+               exfat_error("'%s' is empty but start cluster is %#x", buffer,
+                               node->start_cluster);
+               ret = false;
+       }
+       if (node->size > 0 && CLUSTER_INVALID(node->start_cluster))
+       {
+               exfat_get_name(node, buffer, sizeof(buffer) - 1);
+               exfat_error("'%s' points to invalid cluster %#x", buffer,
+                               node->start_cluster);
+               ret = false;
+       }
+
        return ret;
 }