Skip to content

add explicit casts for clang#16

Merged
pknowles merged 1 commit into
mainfrom
pknowles/casts_for_clang
Feb 8, 2026
Merged

add explicit casts for clang#16
pknowles merged 1 commit into
mainfrom
pknowles/casts_for_clang

Conversation

@pknowles
Copy link
Copy Markdown
Contributor

@pknowles pknowles commented Feb 8, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Improved type handling and alignment calculations in Linux memory-mapped file operations. This reduces rare alignment-related failures during file resizing and memory synchronization, improves arithmetic consistency across platforms, and enhances overall stability and reliability of file-backed memory usage.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 8, 2026

Warning

Rate limit exceeded

@pknowles has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 18 minutes and 37 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Walkthrough

Refined type casts in Linux memory-mapped file code: ftruncate size is cast to off_t; page-size values are cast to size_t for alignment masks in MemoryMap::sync; and resizing arithmetic in ResizableMappedMemory uses size_t casts.

Changes

Cohort / File(s) Summary
Type safety & alignment fixes
include/decodeless/detail/mappedfile_linux.hpp
Cast ftruncate size argument to off_t; cast (pageSize() - 1) and page-size usages to size_t for bitmask/alignment and resize calculations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 I nibble at casts, tidy and spry,

off_t for truncate, page masks comply.
Size_t aligns where bits once slipped,
Mappings snug now, no misfit crypt.
A little hop — safe syscalls fly! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'add explicit casts for clang' accurately describes the main change—adding explicit type casts to improve clang compiler compatibility.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch pknowles/casts_for_clang

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In `@include/decodeless/detail/mappedfile_linux.hpp`:
- Around line 297-298: Change the C-style cast to a C++ cast by replacing
size_t(pageSize()) with static_cast<size_t>(pageSize()), and replace the
division/multiplication alignment used to compute newMappedSize with a
power-of-two bitmask rounding approach (round up to the next multiple of ps
using a mask based on ps-1 since pageSize() is power-of-two) so the alignment
idiom matches the one used elsewhere in this file.
- Line 136: The expression that computes alignedOffset uses a functional-style
cast; change it to use an explicit C++ cast by replacing size_t(pageSize() - 1)
with static_cast<size_t>(pageSize() - 1) in the alignedOffset calculation (the
variables/functions involved: alignedOffset, offset, pageSize()); optionally
consider aligning pageSize()'s return type to size_t on Linux to remove the need
for casts across mappedfile_linux.hpp.
- Line 77: Replace the C-style functional cast off_t(size) in the ftruncate call
with an explicit static_cast: use static_cast<off_t>(size) when calling
ftruncate(m_fd, ...), and add a pre-check that the unsigned/integral variable
size does not exceed std::numeric_limits<off_t>::max() (handle the error path if
it does) to avoid signed overflow/UB; update the code around the ftruncate
invocation in mappedfile_linux.hpp where m_fd and size are used to perform this
bounds check before casting and truncating.

size_t size() const { return static_cast<size_t>(stat().st_size); }
void truncate(size_t size) {
if (ftruncate(m_fd, size) == -1)
if (ftruncate(m_fd, off_t(size)) == -1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick | 🔵 Trivial

Prefer static_cast<off_t>(size) over functional-style cast.

off_t(size) is a C-style (functional notation) cast. In C++ codebases, static_cast<off_t>(size) is preferred as it's more explicit and grep-friendly, and it won't silently degrade to a reinterpret_cast if types change in the future.

Also, if size exceeds std::numeric_limits<off_t>::max(), this is signed integer overflow (UB). Consider a bounds check before truncating.

Suggested fix
-        if (ftruncate(m_fd, off_t(size)) == -1)
+        if (ftruncate(m_fd, static_cast<off_t>(size)) == -1)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (ftruncate(m_fd, off_t(size)) == -1)
if (ftruncate(m_fd, static_cast<off_t>(size)) == -1)
🤖 Prompt for AI Agents
In `@include/decodeless/detail/mappedfile_linux.hpp` at line 77, Replace the
C-style functional cast off_t(size) in the ftruncate call with an explicit
static_cast: use static_cast<off_t>(size) when calling ftruncate(m_fd, ...), and
add a pre-check that the unsigned/integral variable size does not exceed
std::numeric_limits<off_t>::max() (handle the error path if it does) to avoid
signed overflow/UB; update the code around the ftruncate invocation in
mappedfile_linux.hpp where m_fd and size are used to perform this bounds check
before casting and truncating.

Comment thread include/decodeless/detail/mappedfile_linux.hpp Outdated
Comment thread include/decodeless/detail/mappedfile_linux.hpp Outdated
@pknowles pknowles force-pushed the pknowles/casts_for_clang branch from 77b7099 to f0e8e1e Compare February 8, 2026 00:55
@pknowles pknowles force-pushed the pknowles/casts_for_clang branch from f0e8e1e to d3425b2 Compare February 8, 2026 01:01
@pknowles pknowles merged commit d3425b2 into main Feb 8, 2026
12 checks passed
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.

1 participant