Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Tor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
The Tor Project
Core
Tor
Commits
7999d0bf
Commit
7999d0bf
authored
7 years ago
by
Chelsea Holland Komlo
Committed by
Nick Mathewson
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
update tor_allocate and add tests
parent
7d126b9d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/rust/tor_allocate/tor_allocate.rs
+14
-8
14 additions, 8 deletions
src/rust/tor_allocate/tor_allocate.rs
with
14 additions
and
8 deletions
src/rust/tor_allocate/tor_allocate.rs
+
14
−
8
View file @
7999d0bf
use
libc
::{
c_char
,
c_void
};
use
std
::{
ptr
,
slice
};
use
std
::{
ptr
,
slice
,
mem
};
#[cfg(not(test))]
extern
"C"
{
...
...
@@ -19,29 +19,35 @@ extern "C" fn tor_malloc_ ( size: usize) -> *mut c_void {
///
/// # Inputs
///
/// * `src`, a reference to a String
that will be copied
.
/// * `src`, a reference to a String.
///
/// # Returns
///
/// A `
String
` that should be freed by tor_free in C
/// A `
*mut c_char
` that should be freed by tor_free in C
///
pub
fn
allocate_and_copy_string
(
src
:
&
String
)
->
*
mut
c_char
{
let
bytes
=
src
.as_bytes
();
let
bytes
:
&
[
u8
]
=
src
.as_bytes
();
let
size
=
bytes
.len
();
let
size_with_null_byte
=
size
+
1
;
let
size
=
mem
::
size_of_val
::
<
[
u8
]
>
(
bytes
);
let
size_one_byte
=
mem
::
size_of
::
<
u8
>
();
// handle integer overflow when adding one to the calculated length
let
size_with_null_byte
=
match
size
.checked_add
(
size_one_byte
)
{
Some
(
n
)
=>
n
,
None
=>
return
ptr
::
null_mut
(),
};
let
dest
=
unsafe
{
tor_malloc_
(
size_with_null_byte
)
as
*
mut
u8
};
if
dest
.is_null
()
{
return
dest
as
*
mut
c_char
;
return
ptr
::
null_mut
()
;
}
unsafe
{
ptr
::
copy_nonoverlapping
(
bytes
.as_ptr
(),
dest
,
size
)
};
// set the last byte as null, using the ability to index into a slice
// rather than doing pointer arithmatic
let
slice
=
unsafe
{
slice
::
from_raw_parts_mut
(
dest
,
size_with_null_byte
)
};
let
slice
=
unsafe
{
slice
::
from_raw_parts_mut
(
dest
,
size_with_null_byte
)};
slice
[
size
]
=
0
;
// add a null terminator
dest
as
*
mut
c_char
...
...
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