diff --git a/cpath/src/cpath.c b/cpath/src/cpath.c index 50531f6..e6c725d 100644 --- a/cpath/src/cpath.c +++ b/cpath/src/cpath.c @@ -32,7 +32,10 @@ void dirup(char *dst, u64 levels, const char *path) { u64 end_index = 0; u64 sep_count = 0; - u64 length = strlen(path); + u64 full_length; + u64 length; + length = full_length = strlen(path); + if (path[length - 1] == path_sep) { --length; } @@ -52,7 +55,13 @@ void dirup(char *dst, u64 levels, const char *path) { end_index = 0; } - strncpy(dst, path, end_index); + if (dst == path) { + memset(&dst[end_index], 0, full_length - end_index); + } else { + u64 dst_length = strlen(dst); + memset(dst, 0, dst_length); + strncpy(dst, path, end_index); + } } void join_root_and_leaf(const char *root, const char *leaf, char *dst) {