Skip to content

Fix warnings and truncations around filesize_to_str() - #6987

Merged
MichaelChirico merged 9 commits into
masterfrom
fix_filesize_to_str
May 27, 2025
Merged

MichaelChirico merged 9 commits into
masterfrom
fix_filesize_to_str

Conversation

@aitap

@aitap aitap commented May 16, 2025

Copy link
Copy Markdown
Member
  • Seen on 32-bit Windows and Linux:
    library(data.table)
    fwrite(mtcars, "mtcars")
    data.table::fread("mtcars", verbose=TRUE) |> capture.output() |> grepv("bytes", x = _, value = TRUE)
    # [1] "  File opened, size = 0.000TB (1281 bytes)."
    # [2] "  Number of sampling jump points = 1 because (1280 bytes from row 1 to eof) / (2 * 1280 jump0size) == 0"
    # [3] "Read 32 rows x 11 columns from 0.000TB (1281 bytes) file in 00:00.000 wall clock time"

    Performing a 40-bit shift on a 32-bit size_t is undefined behaviour; things go downhill from there. Accept uint64_t instead of size_t for a file size, since it's still possible for a file to exceed the address space in size.

  • This will also fix the warnings noted in Review and update GitLab CI failure rules #6984, where a size_t was passed to the PRIu64 size specifier:
      fread.c:422:18: warning: format specifies type 'unsigned long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
      fread.c:422:49: warning: format specifies type 'unsigned long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
      fread.c:427:70: warning: format specifies type 'unsigned long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
      fread.c:432:55: warning: format specifies type 'unsigned long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
    

    This warning ends up being harmless even on 32-bit systems, but it's still undefined behaviour.

  • Make use of this on both Windows and non-Windows. Instead of blindly reading the file size modulo 4 GiB, check that the file at least has a chance to fit into the address space. On Linux with glibc, we default to 32-bit off_t and thus fail to open files that are too large, but guard against this anyway in case some other POSIX system behaves differently.
  • Since we already make use of powers of two in the size printouts, print the binary SI prefixes.

aitap added 3 commits May 16, 2025 14:10
On 32-bit platforms, we would otherwise perform a 40-bit shift of a
32-bit size_t, which is undefined behaviour. Moreover, files can be
larger than the address space of the process.
On non-Windows, print the errno when file exists but we couldn't open
it. That will happen for files larger than the address space when off_t
is 32-bit (default on Linux+glibc).

In case an operating system does let us open a large file, check the
size before casting it to size_t. (This happens at least on Windows.)
@codecov

codecov Bot commented May 16, 2025 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.69%. Comparing base (db834d4) to head (d462ecf).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6987   +/-   ##
=======================================
  Coverage   98.69%   98.69%           
=======================================
  Files          79       79           
  Lines       14678    14678           
=======================================
  Hits        14487    14487           
  Misses        191      191           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented May 16, 2025 •

Copy link
Copy Markdown

Comparison Plot

Generated via commit d462ecf

Download link for the artifact containing the test results: ↓ atime-results.zip

Task Duration
R setup and installing dependencies 4 minutes and 53 seconds
Installing different package versions 10 minutes and 24 seconds
Running and plotting the test cases 2 minutes and 10 seconds

@jangorecki

jangorecki commented May 16, 2025 •

Copy link
Copy Markdown
Member

Before merging this we could possibly add check to CI which detects compilation warnings also on Mac, now we have one for Linux.

@MichaelChirico

Copy link
Copy Markdown
Member

I think this warrants a NEWS entry since it could be user-facing

Comment thread src/fread.c Resolved
Comment thread src/fread.c Resolved
Comment thread src/fread.c Resolved
@MichaelChirico

Copy link
Copy Markdown
Member

Before merging this we could possibly add check to CI which detects compilation warnings also on Mac, now we have one for Linux.

I want to avoid piling in unrelated edits to PRs, it makes the git history a lot harder to understand. Just this week we had to try and parse out why a certain change was made in an unrelated 7+-year-old PR & guess based on PR comments:

#6982 (comment)

Since I don't think it's a blocking issue, I filed #6990.

aitap and others added 3 commits May 20, 2025 14:07
Co-authored-by: Michael Chirico <chiricom@google.com>
Co-authored-by: Michael Chirico <chiricom@google.com>
@aitap

aitap commented May 20, 2025

Copy link
Copy Markdown
Member Author

Yes, writing the NEWS made me realise how unrelated were the fixes. Sorry. Will apply the Boy Scout Rule less liberally in the future. I can split the PR if needed.

aitap and others added 2 commits May 20, 2025 17:47
- Make sure to use the right platform-specific variable in both cases
- Can't use filesize_to_str() more than once due to its static buffer
Comment thread NEWS.md Resolved
@MichaelChirico

Copy link
Copy Markdown
Member

Thanks!

@MichaelChirico
MichaelChirico merged commit 4f2c94a into master May 27, 2025
@MichaelChirico
MichaelChirico deleted the fix_filesize_to_str branch May 27, 2025 06:29
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.

3 participants