Преглед на файлове

StringName: fix returning dangling data from char constructor.

Fixes a copy paste mistake in the `StringName(const char *,bool)` constructor,
to match the same form as the other two constrcutors.
This fixes a case where this constructor can return a dangling pointer and cause use-after-free.
Lyuma преди 2 години
родител
ревизия
82c52eab6c
променени са 1 файла, в които са добавени 26 реда и са изтрити 33 реда
  1. 26 33
      core/string/string_name.cpp

+ 26 - 33
core/string/string_name.cpp

@@ -226,19 +226,16 @@ StringName::StringName(const char *p_name, bool p_static) {
 		_data = _data->next;
 	}
 
-	if (_data) {
-		if (_data->refcount.ref()) {
-			// exists
-			if (p_static) {
-				_data->static_count.increment();
-			}
+	if (_data && _data->refcount.ref()) {
+		// exists
+		if (p_static) {
+			_data->static_count.increment();
+		}
 #ifdef DEBUG_ENABLED
-			if (unlikely(debug_stringname)) {
-				_data->debug_references++;
-			}
-#endif
+		if (unlikely(debug_stringname)) {
+			_data->debug_references++;
 		}
-
+#endif
 		return;
 	}
 
@@ -288,19 +285,17 @@ StringName::StringName(const StaticCString &p_static_string, bool p_static) {
 		_data = _data->next;
 	}
 
-	if (_data) {
-		if (_data->refcount.ref()) {
-			// exists
-			if (p_static) {
-				_data->static_count.increment();
-			}
+	if (_data && _data->refcount.ref()) {
+		// exists
+		if (p_static) {
+			_data->static_count.increment();
+		}
 #ifdef DEBUG_ENABLED
-			if (unlikely(debug_stringname)) {
-				_data->debug_references++;
-			}
-#endif
-			return;
+		if (unlikely(debug_stringname)) {
+			_data->debug_references++;
 		}
+#endif
+		return;
 	}
 
 	_data = memnew(_Data);
@@ -348,19 +343,17 @@ StringName::StringName(const String &p_name, bool p_static) {
 		_data = _data->next;
 	}
 
-	if (_data) {
-		if (_data->refcount.ref()) {
-			// exists
-			if (p_static) {
-				_data->static_count.increment();
-			}
+	if (_data && _data->refcount.ref()) {
+		// exists
+		if (p_static) {
+			_data->static_count.increment();
+		}
 #ifdef DEBUG_ENABLED
-			if (unlikely(debug_stringname)) {
-				_data->debug_references++;
-			}
-#endif
-			return;
+		if (unlikely(debug_stringname)) {
+			_data->debug_references++;
 		}
+#endif
+		return;
 	}
 
 	_data = memnew(_Data);