From 71e020db59cdae208f07064a3ad3017916a6b8ed Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Wed, 28 Oct 2020 11:01:30 +0530 Subject: [PATCH] Ensure that duration values are not zero If the duration for a periodic operation is set to zero in the config file then it is likely that the thread would crash. We want to prevent this at all costs, which is why we're adding this check. Signed-off-by: Sayan Nandan --- server/src/config/mod.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/server/src/config/mod.rs b/server/src/config/mod.rs index c11617dd..68e8d911 100644 --- a/server/src/config/mod.rs +++ b/server/src/config/mod.rs @@ -371,10 +371,13 @@ pub enum ConfigType { /// Type of configuration error: /// - The config file was not found (`OSError`) -/// - THe config file was invalid (`SyntaxError`) +/// - The config file was invalid (`SyntaxError`) +/// - The config file has an invalid value, which is syntatically correct +/// but logically incorrect (`CfgError`) pub enum ConfigError { OSError(Box), SyntaxError(Box), + CfgError(&'static str), } impl fmt::Display for ConfigError { @@ -382,6 +385,7 @@ impl fmt::Display for ConfigError { match self { ConfigError::OSError(e) => write!(f, "error: {}\n", e), ConfigError::SyntaxError(e) => write!(f, "syntax error in configuration file: {}\n", e), + ConfigError::CfgError(e) => write!(f, "Configuration error: {}", e), } } } @@ -401,6 +405,20 @@ pub fn get_config_file_or_return_cfg() -> Result, Confi if cfg.bgsave.is_disabled() { log::warn!("BGSAVE is disabled: If this system crashes unexpectedly, it may lead to the loss of data"); } + if let SnapshotConfig::Enabled(e) = &cfg.snapshot { + if e.every == 0 { + return Err(ConfigError::CfgError( + "The snapshot duration has to be greater than 0!", + )); + } + } + if let BGSave::Enabled(dur) = &cfg.bgsave { + if *dur == 0 { + return Err(ConfigError::CfgError( + "The BGSAVE duration has to be greater than 0!", + )); + } + } return Ok(ConfigType::Custom(cfg)); } Err(e) => return Err(e),