mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-15 16:35:13 +00:00
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:
@@ -21,6 +21,17 @@ impl crate::charging::ChargingEngine {
|
|||||||
.map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?;
|
.map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?;
|
||||||
|
|
||||||
let key = format!("credit:{}", ip);
|
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
|
let new_balance: u64 = redis::AsyncCommands::incr(&mut conn, &key, bytes_to_add).await
|
||||||
.map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?;
|
.map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user