ensure we don't call closedir(NULL) in search_packidx()
diff --git a/lib/pack.c b/lib/pack.c
index edc9583..f5d5be0 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -356,7 +356,7 @@ search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
err = got_error(GOT_ERR_NO_OBJ);
done:
free(path_packdir);
- if (closedir(packdir) != 0 && err == 0)
+ if (packdir && closedir(packdir) != 0 && err == 0)
err = got_error_from_errno();
return err;
}