From 0cb01b87c04925dffae11269b0e94e16a5d2b2be Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Sat, 9 Dec 2023 15:17:54 +0100 Subject: [PATCH] macOS a11y: rebuild table model if out-of-bounds cell is requested MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While it is the itemview's responsibility to keep the accessibility bridge updated about changes in the view's structure and size, we have experienced a number of assertions getting triggered when that wasn't done correctly. Instead of an assert (or hard crash in release builds), recreate the table representation in the accessibility bridge when a cell that is out-of-bounds for the current representation is requested. Emit a warning message to inform widget authors, and improve the debug message with information about the column count as well. Amends 52c2b82082b535123c0eecafe1ec1e4e4190df2a. Pick-to: 6.6 Change-Id: I19c20a932153268a5176d7378c485277088f10bf Reviewed-by: Michael Weghorn Reviewed-by: Tor Arne Vestbø (cherry picked from commit c81e31461fd5a5bd2fe959f26b2e6d134b9a71e9) Reviewed-by: Qt Cherry-pick Bot --- .../cocoa/qcocoaaccessibilityelement.mm | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoaaccessibilityelement.mm b/src/plugins/platforms/cocoa/qcocoaaccessibilityelement.mm index 0f76d564cdf..fdd089e72d9 100644 --- a/src/plugins/platforms/cocoa/qcocoaaccessibilityelement.mm +++ b/src/plugins/platforms/cocoa/qcocoaaccessibilityelement.mm @@ -135,19 +135,37 @@ static void convertLineOffset(QAccessibleTextInterface *text, int *line, int *of if (tableInterface) { auto *tableElement = [QMacAccessibilityElement elementWithInterface:table]; Q_ASSERT(tableElement); + if (!tableElement->rows + || int(tableElement->rows.count) <= m_rowIndex + || int(tableElement->rows.count) != tableInterface->rowCount()) { + qCWarning(lcAccessibilityTable) + << "Cell requested for row" << m_rowIndex << "is out of" + << "bounds for table with" << (tableElement->rows ? + tableElement->rows.count : tableInterface->rowCount()) + << "rows! Resizing table model."; + [tableElement updateTableModel]; + } + Q_ASSERT(tableElement->rows); + Q_ASSERT(int(tableElement->rows.count) > m_rowIndex); + + auto *rowElement = tableElement->rows[m_rowIndex]; + if (!rowElement->columns || int(rowElement->columns.count) != tableInterface->columnCount()) { + if (rowElement->columns) { + qCWarning(lcAccessibilityTable) + << "Table representation column count is out of sync:" + << rowElement->columns.count << "!=" << tableInterface->columnCount(); + } + rowElement->columns = [rowElement populateTableRow:rowElement->columns + count:tableInterface->columnCount()]; + } qCDebug(lcAccessibilityTable) << "Creating cell representation for" << m_rowIndex << m_columnIndex << "in table with" - << tableElement->rows.count << "rows"; + << tableElement->rows.count << "rows and" + << rowElement->columns.count << "columns"; - Q_ASSERT(int(tableElement->rows.count) > m_rowIndex); - auto *rowElement = tableElement->rows[m_rowIndex]; - if (!rowElement->columns) { - rowElement->columns = [rowElement populateTableRow:rowElement->columns - count:tableInterface->columnCount()]; - } rowElement->columns[m_columnIndex] = self; } } @@ -223,6 +241,10 @@ static void convertLineOffset(QAccessibleTextInterface *text, int *line, int *of - (NSMutableArray *)populateTableArray:(NSMutableArray *)array role:(NSAccessibilityRole)role count:(int)count { if (QAccessibleInterface *iface = self.qtInterface) { + if (array && int(array.count) != count) { + [array release]; + array = nil; + } if (!array) { array = [NSMutableArray arrayWithCapacity:count]; [array retain]; @@ -253,6 +275,11 @@ static void convertLineOffset(QAccessibleTextInterface *text, int *line, int *of - (NSMutableArray *)populateTableRow:(NSMutableArray *)array count:(int)count { Q_ASSERT(synthesizedRole == NSAccessibilityRowRole); + if (array && int(array.count) != count) { + [array release]; + array = nil; + } + if (!array) { array = [NSMutableArray arrayWithCapacity:count]; [array retain];