Avoid close negative fd
Fix memleak in sdb_query
Fix another memleak in sdb_query