Skip to content

Commit f2a1f2b

Browse files
committed
compiler_rt: provide strncpy impl for ssp
1 parent 7526b25 commit f2a1f2b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/compiler_rt/ssp.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const std = @import("std");
1616
const common = @import("./common.zig");
1717
const builtin = @import("builtin");
1818

19-
extern fn strncpy(dest: [*:0]u8, src: [*:0]const u8, n: usize) callconv(.C) [*:0]u8;
2019
extern fn memset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
2120
extern fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8;
2221
extern fn memmove(dest: ?[*]u8, src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8;
@@ -66,8 +65,16 @@ fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callconv(.C) [
6665
}
6766

6867
fn __strncpy_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68+
@setRuntimeSafety(false);
6969
if (dest_n < n) __chk_fail();
70-
return strncpy(dest, src, n);
70+
var i: usize = 0;
71+
while (i < n and src[i] != 0) : (i += 1) {
72+
dest[i] = src[i];
73+
}
74+
while (i < n) : (i += 1) {
75+
dest[i] = 0;
76+
}
77+
return dest;
7178
}
7279

7380
fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callconv(.C) [*:0]u8 {

0 commit comments

Comments
 (0)