Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tokio
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
felixmoebius
tokio
Commits
2da15b5f
Unverified
Commit
2da15b5f
authored
4 years ago
by
Geoffry Song
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
io: remove unsafe from ReadToString (#2384)
parent
7e88b56b
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
tokio/Cargo.toml
+1
-1
1 addition, 1 deletion
tokio/Cargo.toml
tokio/src/io/util/read_to_string.rs
+22
-14
22 additions, 14 deletions
tokio/src/io/util/read_to_string.rs
tokio/tests/read_to_string.rs
+49
-0
49 additions, 0 deletions
tokio/tests/read_to_string.rs
with
72 additions
and
15 deletions
tokio/Cargo.toml
+
1
−
1
View file @
2da15b5f
...
...
@@ -121,7 +121,7 @@ default-features = false
optional
=
true
[dev-dependencies]
tokio-test
=
{
version
=
"0.2.0"
}
tokio-test
=
{
version
=
"0.2.0"
,
path
=
"../tokio-test"
}
futures
=
{
version
=
"0.3.0"
,
features
=
[
"async-await"
]
}
proptest
=
"0.9.4"
tempfile
=
"3.1.0"
...
...
This diff is collapsed.
Click to expand it.
tokio/src/io/util/read_to_string.rs
+
22
−
14
View file @
2da15b5f
...
...
@@ -4,7 +4,7 @@ use crate::io::AsyncRead;
use
std
::
future
::
Future
;
use
std
::
pin
::
Pin
;
use
std
::
task
::{
Context
,
Poll
};
use
std
::{
io
,
mem
,
str
};
use
std
::{
io
,
mem
};
cfg_io_util!
{
/// Future for the [`read_to_string`](super::AsyncReadExt::read_to_string) method.
...
...
@@ -25,7 +25,7 @@ where
let
start_len
=
buf
.len
();
ReadToString
{
reader
,
bytes
:
unsafe
{
mem
::
replace
(
buf
.as_mut_vec
(),
Vec
::
new
())
}
,
bytes
:
mem
::
replace
(
buf
,
String
::
new
())
.into_bytes
()
,
buf
,
start_len
,
}
...
...
@@ -38,19 +38,20 @@ fn read_to_string_internal<R: AsyncRead + ?Sized>(
bytes
:
&
mut
Vec
<
u8
>
,
start_len
:
usize
,
)
->
Poll
<
io
::
Result
<
usize
>>
{
let
ret
=
ready!
(
read_to_end_internal
(
reader
,
cx
,
bytes
,
start_len
));
if
str
::
from_utf8
(
&
bytes
)
.is_err
()
{
Poll
::
Ready
(
ret
.and_then
(|
_
|
{
Err
(
io
::
Error
::
new
(
let
ret
=
ready!
(
read_to_end_internal
(
reader
,
cx
,
bytes
,
start_len
))
?
;
match
String
::
from_utf8
(
mem
::
replace
(
bytes
,
Vec
::
new
()))
{
Ok
(
string
)
=>
{
debug_assert!
(
buf
.is_empty
());
*
buf
=
string
;
Poll
::
Ready
(
Ok
(
ret
))
}
Err
(
e
)
=>
{
*
bytes
=
e
.into_bytes
();
Poll
::
Ready
(
Err
(
io
::
Error
::
new
(
io
::
ErrorKind
::
InvalidData
,
"stream did not contain valid UTF-8"
,
))
}))
}
else
{
debug_assert!
(
buf
.is_empty
());
// Safety: `bytes` is a valid UTF-8 because `str::from_utf8` returned `Ok`.
mem
::
swap
(
unsafe
{
buf
.as_mut_vec
()
},
bytes
);
Poll
::
Ready
(
ret
)
)))
}
}
}
...
...
@@ -67,7 +68,14 @@ where
bytes
,
start_len
,
}
=
&
mut
*
self
;
read_to_string_internal
(
Pin
::
new
(
reader
),
cx
,
buf
,
bytes
,
*
start_len
)
let
ret
=
read_to_string_internal
(
Pin
::
new
(
reader
),
cx
,
buf
,
bytes
,
*
start_len
);
if
let
Poll
::
Ready
(
Err
(
_
))
=
ret
{
// Put back the original string.
bytes
.truncate
(
*
start_len
);
**
buf
=
String
::
from_utf8
(
mem
::
replace
(
bytes
,
Vec
::
new
()))
.expect
(
"original string no longer utf-8"
);
}
ret
}
}
...
...
This diff is collapsed.
Click to expand it.
tokio/tests/read_to_string.rs
0 → 100644
+
49
−
0
View file @
2da15b5f
use
std
::
io
;
use
tokio
::
io
::
AsyncReadExt
;
use
tokio_test
::
io
::
Builder
;
#[tokio::test]
async
fn
to_string_does_not_truncate_on_utf8_error
()
{
let
data
=
vec!
[
0xff
,
0xff
,
0xff
];
let
mut
s
=
"abc"
.to_string
();
match
AsyncReadExt
::
read_to_string
(
&
mut
data
.as_slice
(),
&
mut
s
)
.await
{
Ok
(
len
)
=>
panic!
(
"Should fail: {} bytes."
,
len
),
Err
(
err
)
if
err
.to_string
()
==
"stream did not contain valid UTF-8"
=>
{}
Err
(
err
)
=>
panic!
(
"Fail: {}."
,
err
),
}
assert_eq!
(
s
,
"abc"
);
}
#[tokio::test]
async
fn
to_string_does_not_truncate_on_io_error
()
{
let
mut
mock
=
Builder
::
new
()
.read
(
b"def"
)
.read_error
(
io
::
Error
::
new
(
io
::
ErrorKind
::
Other
,
"whoops"
))
.build
();
let
mut
s
=
"abc"
.to_string
();
match
AsyncReadExt
::
read_to_string
(
&
mut
mock
,
&
mut
s
)
.await
{
Ok
(
len
)
=>
panic!
(
"Should fail: {} bytes."
,
len
),
Err
(
err
)
if
err
.to_string
()
==
"whoops"
=>
{}
Err
(
err
)
=>
panic!
(
"Fail: {}."
,
err
),
}
assert_eq!
(
s
,
"abc"
);
}
#[tokio::test]
async
fn
to_string_appends
()
{
let
data
=
b"def"
.to_vec
();
let
mut
s
=
"abc"
.to_string
();
let
len
=
AsyncReadExt
::
read_to_string
(
&
mut
data
.as_slice
(),
&
mut
s
)
.await
.unwrap
();
assert_eq!
(
len
,
3
);
assert_eq!
(
s
,
"abcdef"
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment