From 0ac518c9242397e63aed3b06c118d8128aae2195 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 19 Dec 2017 13:51:31 -0500 Subject: [PATCH] fixed bug when opening empty files or nonexistent files --- lib/misc_prims.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/misc_prims.c b/lib/misc_prims.c index 2bc6268..fdb0a92 100644 --- a/lib/misc_prims.c +++ b/lib/misc_prims.c @@ -12,14 +12,14 @@ CAMLprim value load_file(value path) { if (((fd = open(String_val(path), O_RDONLY, 0)) < 0) || (fstat(fd, &sb) < 0) || (sb.st_size == 0)) { - caml_failwith("could not open file"); + str = caml_alloc_string(0); } else { str = caml_alloc_string(sb.st_size); while ((nread = read(fd, String_val(str), sb.st_size)) > 0); if (nread < 0) caml_failwith("read() failed"); } - close(fd); + if (fd > 0) close(fd); CAMLreturn(str); } -- 2.52.0