util: Address various issues in AllocateFileRange #33228

pull luke-jr wants to merge 5 commits into bitcoin:master from luke-jr:fix_preallocate changing 1 files +23 −8
  1. luke-jr commented at 3:35 PM on August 20, 2025: member

    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)

  2. Bugfix: AllocateFileRange: Disable buggy macOS-specific implementation
    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
    cab5e8d861
  3. Bugfix: AllocateFileRange: Only posix_fallocate the intended range 4a9f8c78f6
  4. Bugfix: AllocateFileRange: Don't SetEndOfFile if SetFilePointerEx fails 8e9c114fae
  5. AllocateFileRange: Avoid clobbering data in fallback implementation
    While this data should never be live, in practice it can be right now, so better play it safe
    476861713e
  6. Bugfix: AllocateFileRange: Avoid truncating files on Windows 2c113ebc2d
  7. DrahtBot commented at 3:35 PM on August 20, 2025: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

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

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    No conflicts as of last run.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  8. fanquake renamed this:
    Bugfix: AllocateFileRange: Address various issues
    util: Address various issues in AllocateFileRange
    on Nov 10, 2025
  9. in src/util/fs_helpers.cpp:216 in 4a9f8c78f6 outdated
     212 | @@ -213,8 +213,7 @@ void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length)
     213 |  #else
     214 |  #if defined(HAVE_POSIX_FALLOCATE)
     215 |      // Version using posix_fallocate
     216 | -    off_t nEndPos = (off_t)offset + length;
     217 | -    if (0 == posix_fallocate(fileno(file), 0, nEndPos)) return;
     218 | +    if (0 == posix_fallocate(fileno(file), offset, length)) return;
    


    sedited commented at 12:53 PM on December 15, 2025:

    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?

  10. in src/util/fs_helpers.cpp:230 in 476861713e outdated
     226 | +    clearerr(file);
     227 |      while (length > 0) {
     228 |          unsigned int now = 65536;
     229 |          if (length < now)
     230 |              now = length;
     231 | +        const size_t rlen = fread(buf, 1, now, file);
    


    sedited commented at 1:14 PM on December 15, 2025:

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

  11. fanquake commented at 1:18 PM on March 3, 2026: member

    @luke-jr are you planning on responding to comments here?

  12. fanquake commented at 1:28 PM on June 1, 2026: member

    @luke-jr can you respond to the review?


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-06-04 09:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me