Browse Source

libobs/util: Fix config value line break unescaping

Given a config file with some value "foo=\\n" querying the value of foo
would return {'\\', '\n', 0}, instead of {'\\', 'n', 0}
Palana 10 years ago
parent
commit
1cd98ae008
1 changed files with 30 additions and 3 deletions
  1. 30 3
      libobs/util/config-file.c

+ 30 - 3
libobs/util/config-file.c

@@ -117,15 +117,42 @@ static bool config_parse_string(struct lexer *lex, struct strref *ref,
 	return success;
 }
 
+static void unescape(struct dstr *str)
+{
+	char *read = str->array;
+	char *write = str->array;
+
+	for (; *read; read++, write++) {
+		char cur = *read;
+		if (cur == '\\') {
+			char next = read[1];
+			if (next == '\\') {
+				read++;
+			} else if (next == 'r') {
+				cur = '\r';
+				read++;
+			} else if (next =='n') {
+				cur = '\n';
+				read++;
+			}
+		}
+
+		if (read != write)
+			*write = cur;
+	}
+
+	if (read != write)
+		*write = '\0';
+}
+
 static void config_add_item(struct darray *items, struct strref *name,
 		struct strref *value)
 {
 	struct config_item item;
 	struct dstr item_value;
 	dstr_init_copy_strref(&item_value, value);
-	dstr_replace(&item_value, "\\n", "\n");
-	dstr_replace(&item_value, "\\r", "\r");
-	dstr_replace(&item_value, "\\\\", "\\");
+
+	unescape(&item_value);
 
 	item.name  = bstrdup_n(name->array,  name->len);
 	item.value = item_value.array;