feat: Add overflow protection to credit addition in charging engine

- Add current balance retrieval before credit increment operation
- Add checked_add validation to prevent u64 overflow when adding credits
- Add InvalidInput error return when credit addition would cause overflow
- Add overflow check between get and incr operations on Redis credit key
This commit is contained in:
nutcas3
2026-04-25 05:28:33 +03:00
parent 2c5d513c4b
commit e4b32a096a
@@ -21,6 +21,17 @@ impl crate::charging::ChargingEngine {
.map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?;
let key = format!("credit:{}", ip);
// Check for potential overflow before incrementing
let current_balance: Option<u64> = redis::AsyncCommands::get(&mut conn, &key).await
.map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?;
if let Some(existing_balance) = current_balance {
if existing_balance.checked_add(bytes_to_add).is_none() {
return Err(ChargingError::InvalidInput("Credit addition would overflow".to_string()));
}
}
let new_balance: u64 = redis::AsyncCommands::incr(&mut conn, &key, bytes_to_add).await
.map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?;