-
Notifications
You must be signed in to change notification settings - Fork 527
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
Deserialize invalid UTF-8 into byte bufs as WTF-8 #877
Open
lucacasonato
wants to merge
1
commit into
serde-rs:master
Choose a base branch
from
lucacasonato:wtf8_encoding
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1713,7 +1713,8 @@ fn test_byte_buf_de() { | |
} | ||
|
||
#[test] | ||
fn test_byte_buf_de_lone_surrogate() { | ||
fn test_byte_buf_de_invalid_surrogates() { | ||
// lone leading surrogate | ||
let bytes = ByteBuf::from(vec![237, 160, 188]); | ||
let v: ByteBuf = from_str(r#""\ud83c""#).unwrap(); | ||
assert_eq!(v, bytes); | ||
|
@@ -1726,23 +1727,49 @@ fn test_byte_buf_de_lone_surrogate() { | |
let v: ByteBuf = from_str(r#""\ud83c ""#).unwrap(); | ||
assert_eq!(v, bytes); | ||
|
||
let bytes = ByteBuf::from(vec![237, 176, 129]); | ||
let v: ByteBuf = from_str(r#""\udc01""#).unwrap(); | ||
assert_eq!(v, bytes); | ||
|
||
let res = from_str::<ByteBuf>(r#""\ud83c\!""#); | ||
assert!(res.is_err()); | ||
|
||
let res = from_str::<ByteBuf>(r#""\ud83c\u""#); | ||
assert!(res.is_err()); | ||
|
||
let res = from_str::<ByteBuf>(r#""\ud83c\ud83c""#); | ||
assert!(res.is_err()); | ||
// lone trailing surrogate | ||
let bytes = ByteBuf::from(vec![237, 176, 129]); | ||
let v: ByteBuf = from_str(r#""\udc01""#).unwrap(); | ||
assert_eq!(v, bytes); | ||
|
||
// leading surrogate followed by other leading surrogate | ||
let bytes = ByteBuf::from(vec![237, 160, 188, 237, 160, 188]); | ||
let v: ByteBuf = from_str(r#""\ud83c\ud83c""#).unwrap(); | ||
assert_eq!(v, bytes); | ||
|
||
// leading surrogate followed by "a" (U+0061) in \u encoding | ||
let bytes = ByteBuf::from(vec![237, 160, 188, 97]); | ||
let v: ByteBuf = from_str(r#""\ud83c\u0061""#).unwrap(); | ||
assert_eq!(v, bytes); | ||
|
||
// leading surrogate followed by U+0080 | ||
let bytes = ByteBuf::from(vec![237, 160, 188, 194, 128]); | ||
let v: ByteBuf = from_str(r#""\ud83c\u0080""#).unwrap(); | ||
assert_eq!(v, bytes); | ||
|
||
// leading surrogate followed by U+FFFF | ||
let bytes = ByteBuf::from(vec![237, 160, 188, 239, 191, 191]); | ||
let v: ByteBuf = from_str(r#""\ud83c\uffff""#).unwrap(); | ||
assert_eq!(v, bytes); | ||
} | ||
|
||
#[test] | ||
fn test_byte_buf_de_surrogate_pair() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was no test for parsing valid surrogate pairs into byte bufs that I could find, so I added one. |
||
// leading surrogate followed by trailing surrogate | ||
let bytes = ByteBuf::from(vec![240, 159, 128, 128]); | ||
let v: ByteBuf = from_str(r#""\ud83c\udc00""#).unwrap(); | ||
assert_eq!(v, bytes); | ||
} | ||
|
||
#[cfg(feature = "raw_value")] | ||
#[test] | ||
fn test_raw_de_lone_surrogate() { | ||
fn test_raw_de_invalid_surrogates() { | ||
use serde_json::value::RawValue; | ||
|
||
assert!(from_str::<Box<RawValue>>(r#""\ud83c""#).is_ok()); | ||
|
@@ -1752,6 +1779,17 @@ fn test_raw_de_lone_surrogate() { | |
assert!(from_str::<Box<RawValue>>(r#""\udc01\!""#).is_err()); | ||
assert!(from_str::<Box<RawValue>>(r#""\udc01\u""#).is_err()); | ||
assert!(from_str::<Box<RawValue>>(r#""\ud83c\ud83c""#).is_ok()); | ||
assert!(from_str::<Box<RawValue>>(r#""\ud83c\u0061""#).is_ok()); | ||
assert!(from_str::<Box<RawValue>>(r#""\ud83c\u0080""#).is_ok()); | ||
assert!(from_str::<Box<RawValue>>(r#""\ud83c\uffff""#).is_ok()); | ||
} | ||
|
||
#[cfg(feature = "raw_value")] | ||
#[test] | ||
fn test_raw_de_surrogate_pair() { | ||
use serde_json::value::RawValue; | ||
|
||
assert!(from_str::<Box<RawValue>>(r#""\ud83c\udc00""#).is_ok()); | ||
} | ||
|
||
#[test] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth adding another error code? Would doing so even be semver compatible? (unrelated to this PR)