From cc6007171bc30aaa2508d88bef58f40235ef1e63 Mon Sep 17 00:00:00 2001
From: Alexey Melnichuk <mimir@newmail.ru>
Date: Wed, 8 Jan 2014 16:18:31 +0300
Subject: Fix. Prevents double close the same handle (#24)

lfs_unlock_dir can be called multiple times for the same object.
For example if lock:free is called manually.
Then lfs_unlock_dir will be called always
again, as soon as the LOCK_METATABLE is collected by GC.
This can lead to strange file errors later on, like closing
another file, which now has been assigned the same handle...
---
 src/lfs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

(limited to 'src')

diff --git a/src/lfs.c b/src/lfs.c
index b7c4fcd..ccbba5e 100644
--- a/src/lfs.c
+++ b/src/lfs.c
@@ -281,7 +281,10 @@ static int lfs_lock_dir(lua_State *L) {
 }
 static int lfs_unlock_dir(lua_State *L) {
   lfs_Lock *lock = luaL_checkudata(L, 1, LOCK_METATABLE);
-  CloseHandle(lock->fd);
+  if(lock->fd != INVALID_HANDLE_VALUE) {    
+    CloseHandle(lock->fd);
+    lock->fd=INVALID_HANDLE_VALUE;
+  }
   return 0;
 }
 #else
-- 
cgit v1.2.3-55-g6feb