From bd7facc4b5f53481dc85a15ba123361b2758655b Mon Sep 17 00:00:00 2001
From: Benji Dial <benji6283@gmail.com>
Date: Sun, 24 Jan 2021 20:02:58 -0500
Subject: [PATCH] making fat driver refuse to open a file when its name is too
 long rather than kernel panicking

---
 src/kernel/fat.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/src/kernel/fat.c b/src/kernel/fat.c
index 362539c..befc1d8 100644
--- a/src/kernel/fat.c
+++ b/src/kernel/fat.c
@@ -174,7 +174,7 @@ static bool try_locate_entry(const uint8_t *fat_name) {
 }
 
 //puts first path component's fat name into fat_name_buffer,
-//returns rest of path
+//returns rest of path, or zero on error
 static const char *split_path(const char *path, uint8_t *fat_name_buffer) {
   uint8_t pi = 0, fi = 0;
   while (1) {
@@ -190,9 +190,9 @@ static const char *split_path(const char *path, uint8_t *fat_name_buffer) {
         ++pi;
       }
       else
-        PANIC("Bad path in FAT16 driver.");
+        return 0;
     else if (((fi == 8) && (path[pi - 1] != EXT_SEP_CHAR)) || (fi == 11))
-      PANIC("Bad path in FAT16 driver.");
+      return 0;
     else {
       fat_name_buffer[fi++] = (uint8_t)path[pi++];
     }
@@ -206,15 +206,13 @@ static bool try_load_from_path(const struct drive *d, const char *path) {
   cur_fdi = &infos[cur_id];
 
   uint8_t fat_name[11];
-  path = split_path(path, fat_name);
-  if (!try_locate_root_entry(fat_name))
+  if (!(path = split_path(path, fat_name)) ||
+      !try_locate_root_entry(fat_name))
     return false;
-  while (*path) {
-    path = split_path(path, fat_name);
-    if (!try_locate_entry(fat_name))
+  while (*path)
+    if (!(path = split_path(path, fat_name)) ||
+        !try_locate_entry(fat_name))
       return false;
-  }
-
   return true;
 }