-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: assorted numeric overflow fixes #2462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
60a2600
9eeae6c
63de26d
bc77456
a7687ef
fb7090d
260664b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2689,7 +2689,15 @@ fn parse_cpu_limit(value: &str) -> Result<Option<i64>, Status> { | |
| )); | ||
| } | ||
|
|
||
| Ok(Some((cores * 1_000_000_000.0).round() as i64)) | ||
| let nano_cpus = (cores * 1_000_000_000.0).round(); | ||
| #[allow(clippy::cast_precision_loss)] | ||
| if !nano_cpus.is_finite() || nano_cpus < i64::MIN as f64 || nano_cpus > i64::MAX as f64 { | ||
| return Err(Status::failed_precondition(format!( | ||
| "docker cpu_limit '{value}' is too large", | ||
| ))); | ||
| } | ||
|
|
||
| Ok(Some(nano_cpus as i64)) | ||
| } | ||
|
|
||
| #[allow(clippy::cast_possible_truncation)] | ||
|
|
@@ -2735,7 +2743,15 @@ fn parse_memory_limit(value: &str) -> Result<Option<i64>, Status> { | |
| } | ||
| }; | ||
|
|
||
| Ok(Some((amount * multiplier).round() as i64)) | ||
| let bytes = (amount * multiplier).round(); | ||
| #[allow(clippy::cast_precision_loss)] | ||
| if !bytes.is_finite() || bytes < i64::MIN as f64 || bytes > i64::MAX as f64 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Warning (CWE-190): The same 2^63 boundary issue applies here: a quantity producing exactly 2^63 bytes passes validation and clamps to |
||
| return Err(Status::failed_precondition(format!( | ||
| "docker memory_limit '{value}' is too large", | ||
| ))); | ||
| } | ||
|
|
||
| Ok(Some(bytes as i64)) | ||
| } | ||
|
|
||
| fn sandbox_from_container_summary( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -987,7 +987,9 @@ pub fn build_container_spec_with_token_and_gpu_devices( | |
| openshell_core::config::DEFAULT_SSH_PORT | ||
| ), | ||
| ], | ||
| interval: config.health_check_interval_secs * 1_000_000_000, | ||
| interval: config | ||
| .health_check_interval_secs | ||
| .saturating_mul(1_000_000_000), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Warning (CWE-190): |
||
| timeout: 2_000_000_000, | ||
| retries: 10, | ||
| start_period: 5_000_000_000, | ||
|
|
@@ -1107,8 +1109,13 @@ fn parse_cpu_to_microseconds(quantity: &str) -> Option<u64> { | |
| if cores <= 0.0 || cores.is_nan() || cores.is_infinite() { | ||
| return None; | ||
| } | ||
| let micros_f = cores * 100_000.0; | ||
| #[allow(clippy::cast_precision_loss)] | ||
| if !micros_f.is_finite() || micros_f > u64::MAX as f64 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Warning (CWE-190): |
||
| return None; | ||
| } | ||
| #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] | ||
| let val = (cores * 100_000.0) as u64; | ||
| let val = micros_f as u64; | ||
| val | ||
| }; | ||
| // A quota of 0 microseconds is invalid — treat as no limit. | ||
|
|
@@ -1183,6 +1190,12 @@ mod tests { | |
| assert_eq!(parse_cpu_to_microseconds("0.5"), Some(50_000)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_cpu_huge_value_returns_none_instead_of_overflow() { | ||
| // A finite f64 whose product with 100_000 overflows to infinity. | ||
| assert_eq!(parse_cpu_to_microseconds("1e300"), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_memory_binary_suffixes() { | ||
| assert_eq!(parse_memory_to_bytes("256Mi"), Some(256 * 1024 * 1024)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Warning (CWE-190):
i64::MAX as f64rounds to 2^63, so an intermediate equal to 2^63 passes this>check and the cast silently saturates toi64::MAX. Reject this boundary (for example with>=) or use an exact checked conversion, and add a 2^63 regression test.