bftw_cache_reserve() can lead to bftw_cache_pop(), which could close the
directory we're trying to unwrap! If that happened, we would try
dup_cloexec(-1), which would fail with EBADF, so there was no observable
bug. But it's better to avoid the whole situation.
return bftw_close(state, file);
}
- if (bftw_cache_reserve(state) != 0) {
- return -1;
+ // Make room for dup()
+ bftw_cache_pin(cache, file);
+ int ret = bftw_cache_reserve(state);
+ bftw_cache_unpin(cache, file);
+ if (ret != 0) {
+ return ret;
}
int fd = dup_cloexec(file->fd);