]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
quantize: prevent input/output file collision (#18451)
authorAnri Lombard <redacted>
Wed, 31 Dec 2025 15:29:03 +0000 (17:29 +0200)
committerGitHub <redacted>
Wed, 31 Dec 2025 15:29:03 +0000 (23:29 +0800)
Check if input and output files are the same before quantizing to prevent
file corruption when mmap reads from a file being written to.

Fixes #12753

tools/quantize/quantize.cpp

index 470dc3d916b90a31404583dc26a8b755b9c3474a..881f4b3dd9e250f475b4f581e61c53e7004716df 100644 (file)
@@ -12,6 +12,7 @@
 #include <cmath>
 #include <cctype>
 #include <algorithm>
+#include <filesystem>
 
 struct quant_option {
     std::string name;
@@ -643,6 +644,11 @@ int main(int argc, char ** argv) {
         return 1;
     }
 
+    if (std::error_code ec; std::filesystem::equivalent(fname_inp, fname_out, ec)) {
+        fprintf(stderr, "%s: error: input and output files are the same: '%s'\n", __func__, fname_inp.c_str());
+        return 1;
+    }
+
     print_build_info();
 
     fprintf(stderr, "%s: quantizing '%s' to '%s' as %s", __func__, fname_inp.c_str(), fname_out.c_str(), ftype_str.c_str());