Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix implode/0 to emit replacement characters on invalid input numbers #182

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/intrinsic/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,10 @@ pub(crate) fn implode(value: Value) -> Result<Value> {
let s = s
.iter()
.map(|c| match c {
Value::Number(c) => {
let c = c
.to_u32()
.ok_or(QueryExecutionError::InvalidNumberAsChar(*c))?;
let c = char::try_from(c)
.map_err(|_| QueryExecutionError::InvalidNumberAsChar(c.into()))?;
Ok(c)
}
Value::Number(c) => Ok(c
.to_u32()
.and_then(|c| char::try_from(c).ok())
.unwrap_or(char::REPLACEMENT_CHARACTER)),
_ => Err(QueryExecutionError::InvalidArgType("implode", c.clone())),
})
.collect::<Result<String>>()?;
Expand Down
2 changes: 0 additions & 2 deletions src/vm/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ pub enum QueryExecutionError {
InvalidAsBase64(#[from] base64::DecodeError),
#[error("Invalid as a UTF-8-encoded byte array")]
InvalidUTF8Bytes(#[from] std::string::FromUtf8Error),
#[error("Invalid as a unicode scalar value `{0:}`")]
InvalidNumberAsChar(Number),
#[error("utf8bytelength is applied to non-string value`{0:?}`")]
InvalidUTF8ByteLength(Value),
#[error("{0:?} was called invalidly with arg `{1:?}`")]
Expand Down
13 changes: 13 additions & 0 deletions tests/from_manual/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,19 @@ test!(
"#
);

test!(
implode2,
r#"
implode
"#,
r#"
[-1, 0, 1, 1114111, 1114112]
"#,
r#"
"\ufffd\u0000\u0001\udbff\udfff\ufffd"
"#
);

test!(
split1,
r#"
Expand Down
Loading