Browse Source

Merge pull request #60484 from timothyqiu/x11-memleak

[3.x] Fix several `OS_X11` memory leaks
Rémi Verschelde 3 years ago
parent
commit
22d040dfb4
1 changed files with 15 additions and 9 deletions
  1. 15 9
      platform/x11/os_x11.cpp

+ 15 - 9
platform/x11/os_x11.cpp

@@ -1248,7 +1248,10 @@ Point2 OS_X11::get_screen_position(int p_screen) const {
 	XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
 
 	// Check if screen is valid
-	ERR_FAIL_INDEX_V(p_screen, count, Point2i(0, 0));
+	if (p_screen < 0 || p_screen >= count) {
+		XFree(xsi);
+		ERR_FAIL_V_MSG(Point2i(0, 0), vformat("Index %d is out of bounds (count = %d)", p_screen, count));
+	}
 
 	Point2i position = Point2i(xsi[p_screen].x_org, xsi[p_screen].y_org);
 
@@ -1273,7 +1276,10 @@ Size2 OS_X11::get_screen_size(int p_screen) const {
 	XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
 
 	// Check if screen is valid
-	ERR_FAIL_INDEX_V(p_screen, count, Size2i(0, 0));
+	if (p_screen < 0 || p_screen >= count) {
+		XFree(xsi);
+		ERR_FAIL_V_MSG(Size2i(0, 0), vformat("Index %d is out of bounds (count = %d)", p_screen, count));
+	}
 
 	Size2i size = Point2i(xsi[p_screen].width, xsi[p_screen].height);
 	XFree(xsi);
@@ -3126,7 +3132,7 @@ String OS_X11::_get_clipboard_impl(Atom p_source, Window x11_window, Atom target
 	Window selection_owner = XGetSelectionOwner(x11_display, p_source);
 	if (selection_owner == x11_window) {
 		static const char *target_type = "PRIMARY";
-		if (p_source != None && String(XGetAtomName(x11_display, p_source)) == target_type) {
+		if (p_source != None && get_atom_name(x11_display, p_source) == target_type) {
 			return OS::get_clipboard_primary();
 		} else {
 			return OS::get_clipboard();
@@ -4107,14 +4113,14 @@ OS::LatinKeyboardVariant OS_X11::get_latin_keyboard_variant() const {
 	XkbDescRec *xkbdesc = XkbAllocKeyboard();
 	ERR_FAIL_COND_V(!xkbdesc, LATIN_KEYBOARD_QWERTY);
 
-	XkbGetNames(x11_display, XkbSymbolsNameMask, xkbdesc);
-	ERR_FAIL_COND_V(!xkbdesc->names, LATIN_KEYBOARD_QWERTY);
-	ERR_FAIL_COND_V(!xkbdesc->names->symbols, LATIN_KEYBOARD_QWERTY);
+	if (XkbGetNames(x11_display, XkbSymbolsNameMask, xkbdesc) != Success) {
+		XkbFreeKeyboard(xkbdesc, 0, true);
+		ERR_FAIL_V(LATIN_KEYBOARD_QWERTY);
+	}
 
-	String layout = get_atom_name(x11_display, xkbdesc->names->symbols);
-	ERR_FAIL_COND_V(layout.empty(), LATIN_KEYBOARD_QWERTY);
+	Vector<String> info = get_atom_name(x11_display, xkbdesc->names->symbols).split("+");
+	XkbFreeKeyboard(xkbdesc, 0, true);
 
-	Vector<String> info = layout.split("+");
 	ERR_FAIL_INDEX_V(1, info.size(), LATIN_KEYBOARD_QWERTY);
 
 	if (info[1].find("colemak") != -1) {