Skip to content

Commit

Permalink
Fix: nasl-interpreter: operation on data returns NaslValue::String
Browse files Browse the repository at this point in the history
Instead of NaslValue::String it should return NaslValue::Data when
working with data as operation.
  • Loading branch information
nichtsfrei committed May 15, 2024
1 parent ac9a5d5 commit c573378
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 4 additions & 5 deletions rust/nasl-interpreter/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ where
NaslValue::Data(x) => {
let right: String = b.map(|x| x.to_string()).unwrap_or_default();
let x: String = x.into_iter().map(|b| b as char).collect();
// TODO: wrong return value, should be data
Ok(NaslValue::String(format!("{x}{right}")))
Ok(NaslValue::Data(format!("{x}{right}").into()))
}
// on plus only we need to cast to string when right is a string
left => match b {
Expand All @@ -134,7 +133,7 @@ where
NaslValue::Data(x) => {
let right: String = b.map(|x| x.to_string()).unwrap_or_default();
let x: String = x.into_iter().map(|b| b as char).collect();
Ok(NaslValue::String(x.replacen(&right, "", 1)))
Ok(NaslValue::Data(x.replacen(&right, "", 1).into()))
}
left => match b {
Some(NaslValue::String(_)) => {
Expand Down Expand Up @@ -276,8 +275,8 @@ mod tests {
cast_to_string_minus: "11-\"1\";" => "1".into(),
string_plus: "\"hello \" + \"world!\";" => "hello world!".into(),
string_minus : "\"hello \" - 'o ';" => "hell".into(),
data_plus: "'hello ' + 'world!';" => "hello world!".into(),
data_minus: "'hello ' - 'o ';" => "hell".into(),
data_plus: "'hello ' + 'world!';" => "hello world!".as_bytes().into(),
data_minus: "'hello ' - 'o ';" => "hell".as_bytes().into(),
numeric_minus : "1 - 2;" => NaslValue::Number(-1),
multiplication: "1*2;" => 2.into(),
division: "512/2;" => 256.into(),
Expand Down
6 changes: 6 additions & 0 deletions rust/nasl-syntax/src/naslvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ impl Display for NaslValue {
}
}

impl From<&[u8]> for NaslValue {
fn from(value: &[u8]) -> Self {
value.to_vec().into()
}
}

impl From<Vec<u8>> for NaslValue {
fn from(s: Vec<u8>) -> Self {
Self::Data(s)
Expand Down

0 comments on commit c573378

Please sign in to comment.