Skip to content

Commit 07ee2f0

Browse files
authored
Merge pull request #218 from elfenpiff/iox2-213-increase-test-container-coverage
[#213] increase test container coverage
2 parents 7c098cd + e12486f commit 07ee2f0

File tree

8 files changed

+762
-462
lines changed

8 files changed

+762
-462
lines changed

iceoryx2-bb/container/src/byte_string.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ use iceoryx2_bb_log::{fail, fatal_panic};
4545
///
4646
/// * The string must be '\0' (null) terminated.
4747
///
48-
pub unsafe fn strlen(ptr: *mut core::ffi::c_char) -> usize {
48+
pub unsafe fn strnlen(ptr: *const core::ffi::c_char, len: usize) -> usize {
4949
const NULL_TERMINATION: core::ffi::c_char = 0;
50-
for i in 0..isize::MAX {
51-
if *ptr.offset(i) == NULL_TERMINATION {
52-
return i as usize;
50+
for i in 0..len {
51+
if *ptr.add(i) == NULL_TERMINATION {
52+
return i;
5353
}
5454
}
5555

56-
unreachable!()
56+
len
5757
}
5858

5959
/// Error which can occur when a [`FixedSizeByteString`] is modified.
@@ -64,7 +64,7 @@ pub enum FixedSizeByteStringModificationError {
6464

6565
impl std::fmt::Display for FixedSizeByteStringModificationError {
6666
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67-
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
67+
std::write!(f, "FixedSizeByteStringModificationError::{:?}", self)
6868
}
6969
}
7070

@@ -278,9 +278,9 @@ impl<const CAPACITY: usize> FixedSizeByteString<CAPACITY> {
278278
/// * `ptr` must be '\0' (null) terminated
279279
///
280280
pub unsafe fn from_c_str(
281-
ptr: *mut core::ffi::c_char,
281+
ptr: *const core::ffi::c_char,
282282
) -> Result<Self, FixedSizeByteStringModificationError> {
283-
let string_length = strlen(ptr);
283+
let string_length = strnlen(ptr, CAPACITY + 1);
284284
if CAPACITY < string_length {
285285
return Err(FixedSizeByteStringModificationError::InsertWouldExceedCapacity);
286286
}

iceoryx2-bb/container/src/semantic_string.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
//! ```
6262
6363
use crate::byte_string::FixedSizeByteStringModificationError;
64-
use crate::byte_string::{as_escaped_string, strlen, FixedSizeByteString};
64+
use crate::byte_string::{as_escaped_string, strnlen, FixedSizeByteString};
6565
use iceoryx2_bb_elementary::enum_gen;
6666
use iceoryx2_bb_log::fail;
6767
use std::fmt::{Debug, Display};
@@ -80,7 +80,7 @@ enum_gen! {
8080

8181
impl std::fmt::Display for SemanticStringError {
8282
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83-
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
83+
std::write!(f, "SemanticStringError::{:?}", self)
8484
}
8585
}
8686

@@ -148,7 +148,10 @@ pub trait SemanticString<const CAPACITY: usize>:
148148
/// * The contents must not contain invalid UTF-8 characters
149149
///
150150
unsafe fn from_c_str(ptr: *mut std::ffi::c_char) -> Result<Self, SemanticStringError> {
151-
Self::new(std::slice::from_raw_parts(ptr as *const u8, strlen(ptr)))
151+
Self::new(std::slice::from_raw_parts(
152+
ptr as *const u8,
153+
strnlen(ptr, CAPACITY + 1),
154+
))
152155
}
153156

154157
/// Returns the contents as a slice

0 commit comments

Comments
 (0)