Skip to content

Commit

Permalink
Fix crashes with invalid utf-8.
Browse files Browse the repository at this point in the history
According to the discussion in emacs bug#74922 it's possible that
emacs passes invalid strings to dynamic libraries.
  • Loading branch information
kurnevsky committed Dec 17, 2024
1 parent 126241a commit 297bdd7
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ emacs-macros = { path = "emacs-macros", version = "0.17.0" }
rustc_version = "0.2.3"

[features]
default = []
default = [ "utf-8-validation" ]
utf-8-validation = []
lossy-integer-conversion = []
nonzero-integer-conversion = []
Expand Down
4 changes: 2 additions & 2 deletions guide/src/type-conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ features = ["nonzero-integer-conversion"]

## Strings

By default, no utf-8 validation is done when converting Lisp strings into Rust strings, because the string data returned by Emacs is guaranteed to be valid utf-8 sequence. If you think you've otherwise encountered an Emacs bug, utf-8 validation can be enabled through a feature:
By default, utf-8 validation is enabled when converting Lisp strings into Rust strings, because the string data returned by Emacs is not guaranteed to be valid utf-8 sequence. If you require additional performance and don't care about possible undefined behavior and crashes, utf-8 validation can be disabled through a feature `utf-8-validation`:

```toml
[dependencies.emacs]
features = ["utf-8-validation"]
default-features = false
```

## Vectors
Expand Down
2 changes: 1 addition & 1 deletion src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl FromLisp<'_> for String {
#[cfg(feature = "utf-8-validation")]
fn from_lisp(value: Value<'_>) -> Result<Self> {
let bytes = value.env.string_bytes(value)?;
Ok(String::from_utf8(bytes).unwrap())
String::from_utf8(bytes).map_err(|e| e.into())
}
}

Expand Down

0 comments on commit 297bdd7

Please sign in to comment.