From 08439ac543be0eaecb9fa4fa822da467f3b88676 Mon Sep 17 00:00:00 2001 From: apocelipes Date: Sun, 10 Nov 2024 21:14:33 +0800 Subject: [PATCH] IO: fix an off-by-one bug --- src/common/io/io_unix.c | 2 +- src/common/io/io_windows.c | 2 +- src/util/stringUtils.h | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/common/io/io_unix.c b/src/common/io/io_unix.c index d66354481b..b47531e40d 100644 --- a/src/common/io/io_unix.c +++ b/src/common/io/io_unix.c @@ -29,7 +29,7 @@ static void createSubfolders(const char* fileName) while((token = strchr(fileName, '/')) != NULL) { uint32_t length = (uint32_t)(token - fileName + 1); - pathTail = ffStrCopyN(pathTail, fileName, length); + pathTail = ffStrCopyN(pathTail, fileName, length + 1); mkdir(path, S_IRWXU | S_IRGRP | S_IROTH); fileName = token + 1; } diff --git a/src/common/io/io_windows.c b/src/common/io/io_windows.c index 90f12b76a6..4a73922fd0 100644 --- a/src/common/io/io_windows.c +++ b/src/common/io/io_windows.c @@ -14,7 +14,7 @@ static void createSubfolders(const char* fileName) while((token = strchr(fileName, '/')) != NULL) { uint32_t length = (uint32_t)(token - fileName + 1); - pathTail = ffStrCopyN(pathTail, fileName, length); + pathTail = ffStrCopyN(pathTail, fileName, length + 1); CreateDirectoryA(path, NULL); fileName = token + 1; } diff --git a/src/util/stringUtils.h b/src/util/stringUtils.h index 6c641517be..b84bcf3d45 100644 --- a/src/util/stringUtils.h +++ b/src/util/stringUtils.h @@ -80,6 +80,7 @@ static inline bool ffCharIsDigit(char c) return '0' <= c && c <= '9'; } +// ffStrCopyN copies at most (nDst - 1) bytes from src to dst static inline char* ffStrCopyN(char* __restrict__ dst, const char* __restrict__ src, size_t nDst) { assert(dst != NULL);