Skip to content
Snippets Groups Projects
Commit 1ebd8a3d authored by Mateusz Mikuła's avatar Mateusz Mikuła Committed by Mateusz Mikuła
Browse files

Handle zero length for strerror_r

parent 87ba8f8a
No related branches found
No related tags found
1 merge request!209Handle zero len for strerror_r
......@@ -235,8 +235,10 @@ pub unsafe extern "C" fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: siz
let len = strlen(msg);
if len >= buflen {
memcpy(buf as *mut c_void, msg as *const c_void, buflen - 1);
*buf.add(buflen - 1) = 0;
if buflen != 0 {
memcpy(buf as *mut c_void, msg as *const c_void, buflen - 1);
*buf.add(buflen - 1) = 0;
}
return ERANGE as c_int;
}
memcpy(buf as *mut c_void, msg as *const c_void, len + 1);
......
......@@ -19,4 +19,8 @@ int main(void) {
char buf2[3];
int ret2 = strerror_r(err, buf2, 3);
printf("errno: %d = %s, return: %d\n", err, buf2, ret2);
char buf3[256];
int ret3 = strerror_r(err, buf3, 0);
printf("errno: %d = %s, return: %d\n", err, buf3, ret3);
}
errno: 2 = No such file or directory
errno: 2 = No such file or directory, return: 0
errno: 2 = No, return: 34
errno: 2 = , return: 34
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment