__strncpy_chk() and __wcsncpy_chk() implementation

Change-Id: I64fd81b92096ceb41c19b5f56a06bd0cb43b3d5b
This commit is contained in:
ciciplusplus
2026-01-24 21:30:19 +01:00
parent 156c7f2890
commit 42cc34a76b
3 changed files with 26 additions and 2 deletions

View File

@@ -186,7 +186,11 @@ impl<T: Copy + Default + Eq + Ord + SafeRead + Debug> GenericChar<T> {
dest: MutPtr<T>,
src: ConstPtr<T>,
size: GuestUSize,
dest_size: GuestUSize,
) -> MutPtr<T> {
if dest_size < size {
panic!("Buffer overflow");
}
let mut end = false;
for i in 0..size {
if !end {

View File

@@ -189,7 +189,16 @@ pub(super) fn strncpy(
src: ConstPtr<u8>,
size: GuestUSize,
) -> MutPtr<u8> {
GenericChar::<u8>::strncpy(env, dest, src, size)
GenericChar::<u8>::strncpy(env, dest, src, size, GuestUSize::MAX)
}
fn __strncpy_chk(
env: &mut Environment,
dest: MutPtr<u8>,
src: ConstPtr<u8>,
size: GuestUSize,
dest_size: GuestUSize,
) -> MutPtr<u8> {
GenericChar::<u8>::strncpy(env, dest, src, size, dest_size)
}
fn strsep(env: &mut Environment, stringp: MutPtr<MutPtr<u8>>, delim: ConstPtr<u8>) -> MutPtr<u8> {
let orig = env.mem.read(stringp);
@@ -319,6 +328,7 @@ pub const FUNCTIONS: FunctionExports = &[
export_c_func!(strcspn(_, _)),
export_c_func!(__strcat_chk(_, _, _)),
export_c_func!(strncpy(_, _, _)),
export_c_func!(__strncpy_chk(_, _, _, _)),
export_c_func!(strsep(_, _)),
export_c_func!(strdup(_)),
export_c_func!(strcmp(_, _)),

View File

@@ -124,7 +124,16 @@ fn wcsncpy(
src: ConstPtr<wchar_t>,
size: GuestUSize,
) -> MutPtr<wchar_t> {
GenericChar::<wchar_t>::strncpy(env, dest, src, size)
GenericChar::<wchar_t>::strncpy(env, dest, src, size, GuestUSize::MAX)
}
fn __wcsncpy_chk(
env: &mut Environment,
dest: MutPtr<wchar_t>,
src: ConstPtr<wchar_t>,
size: GuestUSize,
dest_size: GuestUSize,
) -> MutPtr<wchar_t> {
GenericChar::<wchar_t>::strncpy(env, dest, src, size, dest_size)
}
fn wcsdup(env: &mut Environment, src: ConstPtr<wchar_t>) -> MutPtr<wchar_t> {
GenericChar::<wchar_t>::strdup(env, src)
@@ -186,6 +195,7 @@ pub const FUNCTIONS: FunctionExports = &[
export_c_func!(wcscat(_, _)),
export_c_func!(wcscspn(_, _)),
export_c_func!(wcsncpy(_, _, _)),
export_c_func!(__wcsncpy_chk(_, _, _, _)),
export_c_func!(wcsdup(_)),
export_c_func!(wcscmp(_, _)),
export_c_func!(wcsncmp(_, _, _)),