Skip to content

fix: clamp integers safely#91

Merged
MitulShah1 merged 3 commits intoMitulShah1:mainfrom
millia385:feature/clamp-integers
Feb 28, 2026
Merged

fix: clamp integers safely#91
MitulShah1 merged 3 commits intoMitulShah1:mainfrom
millia385:feature/clamp-integers

Conversation

@millia385
Copy link
Contributor

Hello! 🐧
This PR closes #78.


Changes

ferrotunnel-core/src/tunnel/client.rs

  • I removed unused clippy directive at line 166. There was no timestamp to clamp.
  • I removed a clippy directive and clamped the u128 integer to max value of u64 to make the cast safe.

ferrotunnel-core/src/tunnel/server.rs

  • I removed a clippy directive and clamped the u128 integer to max value of u64 to make the cast safe.

Testing

make check - no issues
cargo test - no issues

All of the binaries built correctly.


There should be no changes to the functionality of the code, besides improved safety.

Please review, and have a nice day ❤️

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses #78 by removing unnecessary Clippy suppressions and making timestamp-to-u64 conversions explicitly safe via clamping, improving robustness in the tunnel heartbeat paths.

Changes:

  • Removed an unused #[allow(clippy::cast_possible_truncation)] in the client handshake code path.
  • Updated client heartbeat timestamp generation to clamp u128 millis into u64::MAX before casting.
  • Updated server heartbeat-ack timestamp generation to clamp u128 millis into u64::MAX before casting.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
ferrotunnel-core/src/tunnel/client.rs Removes an unused Clippy allow and clamps heartbeat timestamps before casting to u64.
ferrotunnel-core/src/tunnel/server.rs Clamps heartbeat-ack timestamps before casting to u64, removing the local Clippy suppression.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +260 to +265
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
.as_millis()
.min(u128::from(u64::MAX))
as u64;
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

The timestamp clamping logic is duplicated here and in tunnel/server.rs. Consider extracting a small helper (e.g., timestamp_millis_u64() or clamp_u128_to_u64()) so the conversion policy lives in one place and can be unit-tested independently.

Copilot uses AI. Check for mistakes.
Comment on lines +339 to +344
timestamp: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64,
.as_millis()
.min(u128::from(u64::MAX))
as u64,
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

This timestamp clamping logic now exists in both client and server paths. Consider extracting it into a shared helper (in core/common) to avoid divergence and make the conversion policy easy to test/change in one place.

Copilot uses AI. Check for mistakes.
@MitulShah1
Copy link
Owner

@millia385 thanks again :) can you make two minor changes? and its ready to merge

Copy link
Owner

@MitulShah1 MitulShah1 left a comment

Choose a reason for hiding this comment

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

needed to fix minor changes

@millia385
Copy link
Contributor Author

Is this okay? @MitulShah1

@MitulShah1
Copy link
Owner

Is this okay? @MitulShah1

Hey Thanks, looks good now.

@MitulShah1 MitulShah1 merged commit 677f422 into MitulShah1:main Feb 28, 2026
13 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.

Handle integer truncation safely in tunnel client/server

3 participants