Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux/X11: Fix memory leak from created screen images #94473

Merged
merged 1 commit into from
Jul 18, 2024

Conversation

nvlled
Copy link
Contributor

@nvlled nvlled commented Jul 17, 2024

Allocated XImages are improperly free'd with XFree. The X11 documentation says that XImage should use
XDestroyImage to free both the image structure and the data pointed to by the image structure.

@nvlled nvlled requested a review from a team as a code owner July 17, 2024 12:07
@AThousandShips AThousandShips added bug platform:linuxbsd cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release labels Jul 17, 2024
@AThousandShips AThousandShips added this to the 4.3 milestone Jul 17, 2024
@@ -1641,7 +1641,7 @@ Ref<Image> DisplayServerX11::screen_get_image(int p_screen) const {
ERR_FAIL_V_MSG(Ref<Image>(), vformat("XImage with RGB mask %x %x %x and depth %d is not supported.", (uint64_t)image->red_mask, (uint64_t)image->green_mask, (uint64_t)image->blue_mask, (int64_t)image->bits_per_pixel));
}
img = Image::create_from_data(width, height, false, Image::FORMAT_RGBA8, img_data);
XFree(image);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another XFree(image) a few lines above.

Copy link
Contributor Author

@nvlled nvlled Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That part actually looks weird, why is it using image right after it was freed? I'm not sure what the original intent was but I think it's safe to just remove that line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's freeing before returning (ERR_FAIL_V_MSG returns).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but is it alright that ERR_FAIL_V_MSG itself refers to image, like image->red_mask?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's another bug indeed. I would suggest saving the error message as a String before freeing image, and then pass it to ERR_FAIL_V_MSG.

Copy link
Member

@akien-mga akien-mga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One case was missed, but otherwise the fix is correct.

Allocated XImages are improperly free'd with XFree.
The X11 documentation says that XImage should use
XDestroyImage to free both the image structure and
the data pointed to by the image structure.

Also fix a potential use-after-free bug.
@akien-mga akien-mga force-pushed the fix-screen-image-memory-leak branch from 253ee7c to 3636d9d Compare July 18, 2024 07:46
@akien-mga akien-mga changed the title Linux: Fix memory leak from created screen images Linux/X11: Fix memory leak from created screen images Jul 18, 2024
@akien-mga akien-mga merged commit 2b2fd56 into godotengine:master Jul 18, 2024
18 checks passed
@akien-mga
Copy link
Member

akien-mga commented Jul 18, 2024

Thanks! And congrats for your first merged Godot contribution 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release platform:linuxbsd topic:porting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants