Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions forester/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,10 @@ pub struct StartArgs {
#[arg(
long,
env = "WORK_ITEM_BATCH_SIZE",
value_parser = clap::value_parser!(usize).range(1..),
value_parser = clap::value_parser!(u64).range(1..),
help = "Number of queue items to process per batch cycle. Smaller values reduce blockhash expiry risk, larger values reduce per-batch overhead."
)]
pub work_item_batch_size: Option<usize>,
pub work_item_batch_size: Option<u64>,

#[arg(
long,
Expand Down
2 changes: 1 addition & 1 deletion forester/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ impl ForesterConfig {
.transpose()?,
min_queue_items: args.min_queue_items,
enable_v1_multi_nullify: args.enable_v1_multi_nullify,
work_item_batch_size: args.work_item_batch_size.unwrap_or(50).max(1),
work_item_batch_size: args.work_item_batch_size.unwrap_or(50) as usize,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Target constraints (is project pinned to 64-bit only?) =="
fd -HI --glob 'Cargo.toml' --glob 'config.toml' --glob '*.toml' .cargo .
rg -n --iglob '*Cargo.toml' --iglob '*.toml' 'build\.target|target\s*=|x86_64|aarch64|i686|armv7|wasm32'

echo
echo "== StartArgs manual construction sites (can bypass clap validation) =="
rg -nP --type=rust -C2 '\bStartArgs\s*\{'

echo
echo "== All work_item_batch_size usize casts =="
rg -nP --type=rust -C2 'work_item_batch_size.*as\s+usize|as\s+usize.*work_item_batch_size'

Repository: Lightprotocol/light-protocol

Length of output: 3510


🏁 Script executed:

# Read the actual code around line 434 in forester/src/config.rs
head -n 450 forester/src/config.rs | tail -n 30

Repository: Lightprotocol/light-protocol

Length of output: 1189


🏁 Script executed:

# Find where StartArgs is defined and check work_item_batch_size type
rg -n "struct StartArgs" forester/src --type rust -A 30

Repository: Lightprotocol/light-protocol

Length of output: 1606


🏁 Script executed:

# Check forester/Cargo.toml for any platform restrictions
cat forester/Cargo.toml

Repository: Lightprotocol/light-protocol

Length of output: 2913


🏁 Script executed:

# Search for where work_item_batch_size comes from in args
rg -n "work_item_batch_size" forester/src --type rust -B 2 -A 2

Repository: Lightprotocol/light-protocol

Length of output: 4501


🏁 Script executed:

# Search for all u64->usize casts in forester
rg -n 'as usize' forester/src --type rust | head -20

Repository: Lightprotocol/light-protocol

Length of output: 2003


🏁 Script executed:

# Check if there's any CLAUDE.md or documentation about cast practices
find . -name 'CLAUDE.md' -o -name 'claude.md' | xargs cat 2>/dev/null

Repository: Lightprotocol/light-protocol

Length of output: 50386


🏁 Script executed:

# Look at the context of work_item_batch_size to understand practical value ranges
rg -n 'work_item_batch_size' forester/src --type rust -B 1 -A 1 | grep -E '(work_item_batch_size|help|doc|=)'

Repository: Lightprotocol/light-protocol

Length of output: 1198


The as usize cast is unsafe as a pattern, but presents zero risk in this codebase.

The conversion on line 434 is indeed a lossy cast that ignores truncation on 32-bit platforms. However, this project targets 64-bit exclusively: the solana-sdk and related Solana dependencies compile only for x86-64 and aarch64, making any 32-bit compilation impossible. Additionally, work_item_batch_size carries a safe value (default: 50, used for batching), which would never realistically exceed usize::MAX even on 32-bit platforms.

That said, using as usize for type conversions that could theoretically fail is not idiomatic Rust. The proposed fix using usize::try_from() with proper error handling is a good practice improvement and prevents future issues if the codebase evolves to support other platforms.

Proposed fix
+        let work_item_batch_size_u64 = args.work_item_batch_size.unwrap_or(50);
+        let work_item_batch_size = usize::try_from(work_item_batch_size_u64).map_err(|_| {
+            ConfigError::InvalidArguments {
+                field: "work_item_batch_size",
+                invalid_values: vec![format!(
+                    "value {} exceeds platform usize::MAX ({})",
+                    work_item_batch_size_u64,
+                    usize::MAX
+                )],
+            }
+        })?;
+
         Ok(Self {
@@
-            work_item_batch_size: args.work_item_batch_size.unwrap_or(50) as usize,
+            work_item_batch_size,
         })
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@forester/src/config.rs` at line 434, Replace the lossy "as usize" cast for
work_item_batch_size with a fallible conversion using usize::try_from to avoid
silent truncation: take the same source value
(args.work_item_batch_size.unwrap_or(50)), call usize::try_from(...) and handle
the Result by propagating a clear error (e.g., return Err(...) from the config
builder) or by using expect with a descriptive message; update the
work_item_batch_size field initialization to use that converted usize instead of
the "as usize" cast so failures are explicit.

})
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cargo clippy --workspace --all-features --all-targets -- -D warnings
# Check that READMEs are up-to-date with cargo-rdme
echo "Checking READMEs are up-to-date..."
if ! command -v cargo-rdme &> /dev/null; then
cargo install cargo-rdme
cargo install --locked cargo-rdme
fi
for toml in $(find program-libs sdk-libs -name '.cargo-rdme.toml' -type f); do
crate_dir=$(dirname "$toml")
Expand Down
Loading