Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

is_path1_modified_after_path2 panics if either paths don't exist. #48

Open
SalBakraa opened this issue Apr 4, 2023 · 0 comments
Open

Comments

@SalBakraa
Copy link

SalBakraa commented Apr 4, 2023

If you decided to move nobuild.c after bootstrapping, the nobuild executable crashes. That sounds good, however the problem arises if you want to use this function to implement make like behaviour, where only modified files are recompiled.

I think the panic should be limited to the GO_REBUILD_URSELF macro, while the is_path1_modified_after_path2 function should be safe.

Here is my patch for it.

diff --git a/nobuild.h b/nobuild.h
index 1f41c42..6edf869 100644
--- a/nobuild.h
+++ b/nobuild.h
@@ -242,6 +242,17 @@ void chain_echo(Chain chain);
         assert(argc >= 1);                                             \
         const char *binary_path = argv[0];                             \
                                                                        \
+        if (!PATH_EXISTS(source_path)) {                               \
+            ERRO("Missing source file '%s', can't Rebuild.\n",         \
+                    source_path);                                      \
+            break;                                                     \
+        }                                                              \
+                                                                       \
+        if (!PATH_EXISTS(binary_path)) {                               \
+            ERRO("Missing binary file '%s', How???\n", binary_path);   \
+            break;                                                     \
+        }                                                              \
+                                                                       \
         if (is_path1_modified_after_path2(source_path, binary_path)) { \
             RENAME(binary_path, CONCAT(binary_path, ".old"));          \
             REBUILD_URSELF(binary_path, source_path);                  \
@@ -1066,6 +1077,16 @@ void path_rm(Cstr path)
 
 int is_path1_modified_after_path2(const char *path1, const char *path2)
 {
+    // Warn the user that the path is missing
+    if (!PATH_EXISTS(path1)) {
+        WARN("file %s does not exist", path2);
+        return 0;
+    }
+
+    if (!PATH_EXISTS(path2)) {
+        return 1;
+    }
+
 #ifdef _WIN32
     FILETIME path1_time, path2_time;
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant