Skip to content

Commit 297bdd7

Browse files
committed
Fix crashes with invalid utf-8.
According to the discussion in emacs bug#74922 it's possible that emacs passes invalid strings to dynamic libraries.
1 parent 126241a commit 297bdd7

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ emacs-macros = { path = "emacs-macros", version = "0.17.0" }
3434
rustc_version = "0.2.3"
3535

3636
[features]
37-
default = []
37+
default = [ "utf-8-validation" ]
3838
utf-8-validation = []
3939
lossy-integer-conversion = []
4040
nonzero-integer-conversion = []

guide/src/type-conversions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ features = ["nonzero-integer-conversion"]
5757

5858
## Strings
5959

60-
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:
60+
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`:
6161

6262
```toml
6363
[dependencies.emacs]
64-
features = ["utf-8-validation"]
64+
default-features = false
6565
```
6666

6767
## Vectors

src/types/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl FromLisp<'_> for String {
1313
#[cfg(feature = "utf-8-validation")]
1414
fn from_lisp(value: Value<'_>) -> Result<Self> {
1515
let bytes = value.env.string_bytes(value)?;
16-
Ok(String::from_utf8(bytes).unwrap())
16+
String::from_utf8(bytes).map_err(|e| e.into())
1717
}
1818
}
1919

0 commit comments

Comments
 (0)