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 <nandansayan@outlook.com>
next
Sayan Nandan 4 years ago
parent 0ea02f846a
commit 71e020db59
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

@ -371,10 +371,13 @@ pub enum ConfigType<T> {
/// 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<dyn Error>),
SyntaxError(Box<dyn Error>),
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<ConfigType<ParsedConfig>, 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),

Loading…
Cancel
Save