macOS: Fix an abort due to an assertion when a program tries to set an invalid window title

Fixes #7271
This commit is contained in:
Kovid Goyal 2024-03-27 19:02:43 +05:30
parent e5a7554c30
commit 8335a5212e
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
4 changed files with 9 additions and 2 deletions

View file

@ -72,6 +72,8 @@ Detailed list of changes
- Splits layout: Fix :ac:`move_window_forward` not working (:iss:`7264`)
- macOS: Fix an abort due to an assertion when a program tries to set an invalid window title (:iss:`7271`)
0.33.1 [2024-03-21]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -1963,9 +1963,11 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window)
window->ns.object = nil;
}
void _glfwPlatformSetWindowTitle(_GLFWwindow* window UNUSED, const char* title)
void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
{
if (!title) return;
NSString* string = @(title);
if (!string) return; // the runtime failed to convert title to an NSString
[window->ns.object setTitle:string];
// HACK: Set the miniwindow title explicitly as setTitle: doesn't update it
// if the window lacks NSWindowStyleMaskTitled

View file

@ -748,12 +748,14 @@ cocoa_create_global_menu(void) {
void
cocoa_update_menu_bar_title(PyObject *pytitle) {
if (!pytitle) return;
NSString *title = nil;
if (OPT(macos_menubar_title_max_length) > 0 && PyUnicode_GetLength(pytitle) > OPT(macos_menubar_title_max_length)) {
static char fmt[64];
snprintf(fmt, sizeof(fmt), "%%%ld.%ldU%%s", OPT(macos_menubar_title_max_length), OPT(macos_menubar_title_max_length));
RAII_PyObject(st, PyUnicode_FromFormat(fmt, pytitle, "…"));
if (st) title = @(PyUnicode_AsUTF8(st));
else PyErr_Print();
} else {
title = @(PyUnicode_AsUTF8(pytitle));
}

View file

@ -98,7 +98,8 @@ update_menu_bar_title(PyObject *title UNUSED) {
static char buf[2048];
strip_csi_(PyUnicode_AsUTF8(title), buf, arraysz(buf));
RAII_PyObject(stitle, PyUnicode_FromString(buf));
cocoa_update_menu_bar_title(stitle);
if (stitle) cocoa_update_menu_bar_title(stitle);
else PyErr_Print();
#endif
}