Skip to content

Conversation

@luke-jr
Copy link
Member

@luke-jr luke-jr commented Aug 20, 2025

Fixes several bugs in AllocateFileRange.

Not a real fix for #33128, but a workaround for the issue.

Potentially might fix #28552 too, but I don't have a way to reproduce that. (After digging into Apple's XNU code, it looks too buggy to try to use F_PREALLOCATE at all IMO; for reference: XNU kernel code and HFS driver code - notice the truncation risk from comparing to block-allocated size rather than file size and reachable code paths the comments assume to be dead code)

Several bugs exist:
- F_PREALLOCATE is undone at close (which we do immediately)
- HFS implementation appears buggy (and we have unknown issues on macOS with exFAT - possibly related?)
- F_PEOFPOSMODE assumes offset is EOF exactly
While this data should never be live, in practice it can be right now, so better play it safe
@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 20, 2025

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33228.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #33164 (Avoid file overwriting in fallback AllocateFileRange implementation by hebasto)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@fanquake fanquake changed the title Bugfix: AllocateFileRange: Address various issues util: Address various issues in AllocateFileRange Nov 10, 2025
// Version using posix_fallocate
off_t nEndPos = (off_t)offset + length;
if (0 == posix_fallocate(fileno(file), 0, nEndPos)) return;
if (0 == posix_fallocate(fileno(file), offset, length)) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really a bugfix? Might this not be intentional to allocate as large a chunk of the file as possible, even in the case where a non-contiguous offset is passed in?

unsigned int now = 65536;
if (length < now)
now = length;
const size_t rlen = fread(buf, 1, now, file);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not seek to the end of the file instead of reading?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crashes constantly on Mac OS Sonoma when using an ExFAT external SSD

3 participants