libobs/util: Simplify implementation of os_get_path_extension

To avoid a false use-after-free warning from GCC's -Wuse-after-free
option, revise the implementation without using memory allocation.
This commit is contained in:
Norihiro Kamae
2023-03-18 17:52:01 +09:00
committed by Jim
parent e4496c7afa
commit 4ea0b69974
+10 -21
View File
@@ -654,28 +654,17 @@ int os_mkdirs(const char *dir)
const char *os_get_path_extension(const char *path)
{
struct dstr temp;
size_t pos = 0;
char *period;
char *slash;
for (size_t pos = strlen(path); pos > 0; pos--) {
switch (path[pos - 1]) {
case '.':
return path + pos - 1;
case '/':
case '\\':
return NULL;
}
}
if (!path[0])
return NULL;
dstr_init_copy(&temp, path);
dstr_replace(&temp, "\\", "/");
slash = strrchr(temp.array, '/');
period = strrchr(temp.array, '.');
if (period)
pos = (size_t)(period - temp.array);
dstr_free(&temp);
if (!period || slash > period)
return NULL;
return path + pos;
return NULL;
}
static inline bool valid_string(const char *str)