Browse Source

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.
Norihiro Kamae 2 years ago
parent
commit
4ea0b69974
1 changed files with 10 additions and 21 deletions
  1. 10 21
      libobs/util/platform.c

+ 10 - 21
libobs/util/platform.c

@@ -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;
-
-	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;
+	for (size_t pos = strlen(path); pos > 0; pos--) {
+		switch (path[pos - 1]) {
+		case '.':
+			return path + pos - 1;
+		case '/':
+		case '\\':
+			return NULL;
+		}
+	}
 
-	return path + pos;
+	return NULL;
 }
 
 static inline bool valid_string(const char *str)