Skip to content

Commit

Permalink
Merge pull request #141 from mikemadden42/feature/fix-warnings-falcon…
Browse files Browse the repository at this point in the history
…_discover_hosts

Safely handle type conversions to avoid potential truncation:
  • Loading branch information
mikemadden42 authored Aug 30, 2024
2 parents 800e9cb + c9b15ee commit 769f4f1
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions examples/falcon_discover_hosts.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clap::Parser;

use rusty_falcon::{
apis::discover_api::{get_hosts, query_hosts},
easy::client::FalconHandle,
};
use std::convert::{TryFrom, TryInto};

// We set API limits to a constant value as the `get_hosts` takes max 100 elements.
const LIMIT: i32 = 100;
Expand Down Expand Up @@ -33,9 +33,12 @@ async fn main() {
let mut offset = 0usize;

loop {
// Use try_into() for safe conversion from usize to i32
let offset_i32: i32 = offset.try_into().expect("Offset exceeded i32 range");

let response = query_hosts(
&falcon.cfg,
Some(offset as i32),
Some(offset_i32),
Some(LIMIT),
Some(args.sort.as_str()),
args.filter.as_deref(),
Expand Down Expand Up @@ -65,7 +68,15 @@ async fn main() {
details.extend(batch_details);

match response.meta.pagination {
Some(pagination) if offset < pagination.total as usize => {}
Some(pagination) => {
// Use `try_from()` to safely convert `i64` to `usize`
if let Ok(total_usize) = usize::try_from(pagination.total) {
if offset < total_usize {
continue;
}
}
break;
}
_ => break,
};
}
Expand Down

0 comments on commit 769f4f1

Please sign in to comment.