Skip to content

Commit

Permalink
Remove Dirty in favor of custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Nov 5, 2024
1 parent 5f51612 commit 4626d27
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 198 deletions.
21 changes: 15 additions & 6 deletions rust/src/nasl/builtin/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use thiserror::Error;

use super::super::prelude::NaslError;
use super::cryptographic::CryptographicError;
use super::regex::RegexError;
use super::{misc::MiscError, network::socket::SocketError, ssh::SshError, string::StringError};

#[derive(Debug, Clone, PartialEq, Eq, Error)]
Expand All @@ -16,23 +17,28 @@ pub enum BuiltinError {
Socket(SocketError),
#[error("{0}")]
Cryptographic(CryptographicError),
#[error("{0}")]
Regex(RegexError),
#[cfg(feature = "nasl-builtin-raw-ip")]
#[error("{0}")]
PacketForgery(super::raw_ip::PacketForgeryError),
}

macro_rules! builtin_error_variant (
($ty: ty, $variant: ident) => {
impl From<$ty> for BuiltinError {
fn from(value: $ty) -> Self {
($err: path, $variant: ident) => {
impl From<$err> for BuiltinError {
fn from(value: $err) -> Self {
BuiltinError::$variant(value)
}
}

impl From<$ty> for NaslError {
fn from(value: $ty) -> Self {
impl From<$err> for NaslError {
fn from(value: $err) -> Self {
NaslError::Builtin(BuiltinError::$variant(value))
}
}

impl TryFrom<NaslError> for $ty {
impl TryFrom<NaslError> for $err {
type Error = ();

fn try_from(value: NaslError) -> Result<Self, Self::Error> {
Expand All @@ -50,3 +56,6 @@ builtin_error_variant!(MiscError, Misc);
builtin_error_variant!(SocketError, Socket);
builtin_error_variant!(CryptographicError, Cryptographic);
builtin_error_variant!(SshError, Ssh);
builtin_error_variant!(RegexError, Regex);
#[cfg(feature = "nasl-builtin-raw-ip")]
builtin_error_variant!(super::raw_ip::PacketForgeryError, PacketForgery);
18 changes: 11 additions & 7 deletions rust/src/nasl/builtin/misc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ use flate2::{
};

#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("{0}")]
// It would be nicer to derive this using #[from] from
// thiserror, but io::Error does not impl `PartialEq`,
// `Eq` or `Clone`, so we wrap `io::ErrorKind` instead, which
// does not impl `Error` which is why this `From` impl exists.
pub struct MiscError(io::ErrorKind);
pub enum MiscError {
#[error("IO Error: {0}")]
IO(io::ErrorKind),
#[error("Encountered time before 1970. {0}")]
TimeBefore1970(String),
}

impl From<io::Error> for MiscError {
fn from(value: io::Error) -> Self {
Self(value.kind())
Self::IO(value.kind())
}
}

Expand Down Expand Up @@ -106,11 +110,11 @@ fn isnull(val: NaslValue) -> bool {

/// Returns the seconds counted from 1st January 1970 as an integer.
#[nasl_function]
fn unixtime() -> Result<u64, NaslError> {
fn unixtime() -> Result<u64, MiscError> {
std::time::SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|t| t.as_secs())
.map_err(|_| NaslError::Dirty("System time set to time before 1st January 1960".into()))
.map_err(|e| MiscError::TimeBefore1970(e.to_string()))
}

/// Compress given data with gzip, when headformat is set to 'gzip' it uses gzipheader.
Expand Down Expand Up @@ -239,13 +243,13 @@ fn defined_func(ctx: &Context, register: &Register, fn_name: Option<Maybe<&str>>
///
/// For example: “1067352015.030757” means 1067352015 seconds and 30757 microseconds.
#[nasl_function]
fn gettimeofday() -> Result<String, NaslError> {
fn gettimeofday() -> Result<String, MiscError> {
match time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH) {
Ok(time) => {
let time = time.as_micros();
Ok(format!("{}.{:06}", time / 1000000, time % 1000000))
}
Err(e) => Err(NaslError::Dirty(format!("{e}"))),
Err(e) => Err(MiscError::TimeBefore1970(e.to_string())),
}
}

Expand Down
1 change: 1 addition & 0 deletions rust/src/nasl/builtin/raw_ip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod raw_ip_utils;
use crate::nasl::utils::{IntoFunctionSet, NaslVars, StoredFunctionSet};
use frame_forgery::FrameForgery;
use packet_forgery::PacketForgery;
pub use packet_forgery::PacketForgeryError;

pub struct RawIp;

Expand Down
Loading

0 comments on commit 4626d27

Please sign in to comment.