OSDN Git Service

Fix crash in exfat_rename().
authorrelan <relan@users.noreply.github.com>
Sat, 15 Dec 2012 08:51:09 +0000 (08:51 +0000)
committerrelan <relan@users.noreply.github.com>
Mon, 24 Aug 2015 05:26:15 +0000 (08:26 +0300)
Crash happened when renaming a file within a single directory and a new
name differs only in case.

libexfat/node.c

index 5b43a3a..307f2ec 100644 (file)
@@ -910,27 +910,33 @@ int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
        }
        if (existing != NULL)
        {
-               if (existing->flags & EXFAT_ATTRIB_DIR)
+               /* remove target if it's not the same node as source */
+               if (existing != node)
                {
-                       if (node->flags & EXFAT_ATTRIB_DIR)
-                               rc = exfat_rmdir(ef, existing);
+                       if (existing->flags & EXFAT_ATTRIB_DIR)
+                       {
+                               if (node->flags & EXFAT_ATTRIB_DIR)
+                                       rc = exfat_rmdir(ef, existing);
+                               else
+                                       rc = -ENOTDIR;
+                       }
                        else
-                               rc = -ENOTDIR;
+                       {
+                               if (!(node->flags & EXFAT_ATTRIB_DIR))
+                                       rc = exfat_unlink(ef, existing);
+                               else
+                                       rc = -EISDIR;
+                       }
+                       exfat_put_node(ef, existing);
+                       if (rc != 0)
+                       {
+                               exfat_put_node(ef, dir);
+                               exfat_put_node(ef, node);
+                               return rc;
+                       }
                }
                else
-               {
-                       if (!(node->flags & EXFAT_ATTRIB_DIR))
-                               rc = exfat_unlink(ef, existing);
-                       else
-                               rc = -EISDIR;
-               }
-               exfat_put_node(ef, existing);
-               if (rc != 0)
-               {
-                       exfat_put_node(ef, dir);
-                       exfat_put_node(ef, node);
-                       return rc;
-               }
+                       exfat_put_node(ef, existing);
        }
 
        rc = find_slot(ef, dir, &cluster, &offset,