From 3bcb6938f714494fd3affc8e47e3f06abba08a5e Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 20 May 2021 14:10:45 -0700 Subject: [PATCH] Factor out base 36 generation to separate function This is so it can be reused without having to copy-paste. generateBase36() is guaranateed to completely initialize and null-terminate the buffer that is passed in. --- desktop_version/src/FileSystemUtils.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index f7bd8402..91dcc99d 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -244,13 +244,12 @@ static bool FILESYSTEM_exists(const char *fname) return PHYSFS_exists(fname); } -static void generateVirtualMountPath(char* path, const size_t path_size) +static void generateBase36(char* string, const size_t string_size) { - char random[6 + 1] = {'\0'}; size_t i; - for (i = 0; i < SDL_arraysize(random) - 1; ++i) + for (i = 0; i < string_size - 1; ++i) { - /* Generate a-z0-9 (base 36) */ + /* a-z0-9 */ char randchar = fRandom() * 36; if (randchar <= 26) { @@ -261,13 +260,20 @@ static void generateVirtualMountPath(char* path, const size_t path_size) randchar -= 26; randchar += '0'; } - random[i] = randchar; + string[i] = randchar; } + string[string_size - 1] = '\0'; +} + +static void generateVirtualMountPath(char* path, const size_t path_size) +{ + char random_str[6 + 1]; + generateBase36(random_str, sizeof(random_str)); SDL_snprintf( path, path_size, ".vvv-mnt-virtual-%s/custom-assets/", - random + random_str ); }