Skip to content

Commit 677f422

Browse files
fix: clamp integers safely (#91)
* fix: clamp integers safely * refactor: separate clamping logic --------- Co-authored-by: Mitul Shah <shahmitul005@gmail.com>
1 parent 4117f98 commit 677f422

4 files changed

Lines changed: 16 additions & 9 deletions

File tree

ferrotunnel-core/src/tunnel/client.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::auth::validate_token_format;
22
use crate::stream::{Multiplexer, PrioritizedFrame, VirtualStream};
33
use crate::transport::batched_sender::run_batched_sender;
44
use crate::transport::{self, TransportConfig};
5+
use crate::tunnel::common::clamp_u128_to_u64;
56
use ferrotunnel_common::{Result, TunnelError};
67
use ferrotunnel_protocol::codec::TunnelCodec;
78
use ferrotunnel_protocol::constants::{MAX_PROTOCOL_VERSION, MIN_PROTOCOL_VERSION};
@@ -163,7 +164,6 @@ impl TunnelClient {
163164
where
164165
C: FnOnce(Uuid) + Send + 'static,
165166
{
166-
#[allow(clippy::cast_possible_truncation)]
167167
framed
168168
.send(Frame::Handshake(Box::new(HandshakeFrame {
169169
min_version: MIN_PROTOCOL_VERSION,
@@ -258,11 +258,12 @@ impl TunnelClient {
258258
let decode_start = Instant::now();
259259
tokio::select! {
260260
_ = heartbeat_interval.tick() => {
261-
#[allow(clippy::cast_possible_truncation)]
262-
let ts = std::time::SystemTime::now()
261+
let ts = clamp_u128_to_u64(
262+
std::time::SystemTime::now()
263263
.duration_since(std::time::UNIX_EPOCH)
264264
.unwrap_or_default()
265-
.as_millis() as u64;
265+
.as_millis()
266+
);
266267
if let Err(e) = multiplexer.send_frame(Frame::Heartbeat { timestamp: ts }).await {
267268
error!("Failed to send heartbeat: {}", e);
268269
return Err(e);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn clamp_u128_to_u64(i: u128) -> u64 {
2+
i.min(u128::from(u64::MAX)) as u64
3+
}

ferrotunnel-core/src/tunnel/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod client;
2+
mod common;
23
pub mod server;
34
pub mod session;
45

ferrotunnel-core/src/tunnel/server.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::resource_limits::{ServerResourceLimits, SessionPermit};
33
use crate::stream::{Multiplexer, PrioritizedFrame};
44
use crate::transport::batched_sender::run_batched_sender;
55
use crate::transport::{self, BoxedStream, TransportConfig};
6+
use crate::tunnel::common::clamp_u128_to_u64;
67
use crate::tunnel::session::{Session, SessionStoreBackend, ShardedSessionStore};
78
use ferrotunnel_common::{Result, TunnelError};
89
use ferrotunnel_protocol::codec::TunnelCodec;
@@ -336,11 +337,12 @@ impl TunnelServer {
336337
Frame::Heartbeat { .. } => {
337338
multiplexer
338339
.send_frame(Frame::HeartbeatAck {
339-
#[allow(clippy::cast_possible_truncation)]
340-
timestamp: std::time::SystemTime::now()
341-
.duration_since(std::time::UNIX_EPOCH)
342-
.unwrap_or_default()
343-
.as_millis() as u64,
340+
timestamp: clamp_u128_to_u64(
341+
std::time::SystemTime::now()
342+
.duration_since(std::time::UNIX_EPOCH)
343+
.unwrap_or_default()
344+
.as_millis(),
345+
),
344346
})
345347
.await?;
346348
}

0 commit comments

Comments
 (0)