fix broken double close in BpfMapTest.cpp

Bug: 139175951
Bug: 139205160
Test: atest libbpf_android_test
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I911ce9276e5f8b2278e459b5b11be8e7ccb3632f
diff --git a/libbpf_android/include/bpf/BpfMap.h b/libbpf_android/include/bpf/BpfMap.h
index d47698e..a894b57 100644
--- a/libbpf_android/include/bpf/BpfMap.h
+++ b/libbpf_android/include/bpf/BpfMap.h
@@ -46,8 +46,19 @@
 template <class Key, class Value>
 class BpfMap {
   public:
-    BpfMap<Key, Value>() : mMapFd(-1){};
+    BpfMap<Key, Value>() {};
     explicit BpfMap<Key, Value>(int fd) : mMapFd(fd){};
+
+    // We could technically implement this constructor either with
+    //   : mMapFd(dup(fd)) {}        // fd valid in caller, we have our own local copy
+    // or
+    //   : mMapFd(fd.release()) {}   // fd no longer valid in caller, we 'stole' it
+    //
+    // However, I think we're much better off with a compile time failure, since
+    // it's better for whoever passes in a unique_fd to think twice about whether
+    // they're trying to pass in ownership or not.
+    explicit BpfMap<Key, Value>(base::unique_fd fd) = delete;
+
     BpfMap<Key, Value>(bpf_map_type map_type, uint32_t max_entries, uint32_t map_flags) {
         int map_fd = createMap(map_type, sizeof(Key), sizeof(Value), max_entries, map_flags);
         if (map_fd < 0) {