Skip to content

Commit dd642ea

Browse files
committed
Replace mi_strlcpy() and mi_strlcat() with versions written from scratch
They used strncpy() and strncat(), which behave almost, but not quite like the ...l... functions. Since these functions are not standard, and not all OSes have comparable functions available, just add a implementations. Addresses first issue raised in #502.
1 parent 0560fc2 commit dd642ea

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/options.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,27 @@ void _mi_error_message(int err, const char* fmt, ...) {
400400
// --------------------------------------------------------
401401

402402
static void mi_strlcpy(char* dest, const char* src, size_t dest_size) {
403-
dest[0] = 0;
404-
strncpy(dest, src, dest_size - 1);
405-
dest[dest_size - 1] = 0;
403+
if (dest_size == 0)
404+
return;
405+
406+
// Copy until end of 'src' or dest is (almost) full
407+
while (*src && (dest_size > 1)) {
408+
*dest++ = *src++;
409+
--dest_size;
410+
}
411+
// Null-terminate dest
412+
*dest = 0;
406413
}
407414

408415
static void mi_strlcat(char* dest, const char* src, size_t dest_size) {
409-
strncat(dest, src, dest_size - 1);
410-
dest[dest_size - 1] = 0;
416+
// Skip existing data in 'dest'
417+
while (*dest && (dest_size > 1)) {
418+
++dest;
419+
--dest_size;
420+
}
421+
422+
// Concatenate src
423+
mi_strlcpy(dest, src, dest_size);
411424
}
412425

413426
#ifdef MI_NO_GETENV

0 commit comments

Comments
 (0)