qt: Ignore gcc 12.1 optimization bug with Qt

The Qt implicit casts from QByteArray to QString all use
size = -1, meaning to the end of the string.

This causes gcc 12.1 with -O2 to produce a very dubious stringop-overread
warning, by computing both sides of a branch even when it shouldn't:

/usr/include/qt5/QtCore/qstring.h:706:69: error: ‘size_t strlen(const char*)’ reading 1 or more bytes from a region of size 0 [-Werror=stringop-overread]
  706 |         return fromUtf8_helper(str, (str && size == -1) ? int(strlen(str)) : size);
      |                                                               ~~~~~~^~~~~

There's also a similar error with QByteArray.constData(), even though
isEmpty() should return True when the QByteArray is NULL.
(Adding isNull() prevents the warning but is redundant.)

Use DIAG_OFF and DIAG_ON to ignore the warning on GCC 12.1 and higher.
Fix #18090.
This commit is contained in:
John Thacker 2022-05-20 20:31:17 -04:00 committed by João Valverde
parent 87d0c6f492
commit e1a6913a2b
4 changed files with 40 additions and 0 deletions

View File

@ -461,8 +461,18 @@ void AboutDialog::updateWiresharkText()
/* Save the info for the clipboard copy */
clipboardInfo = "";
clipboardInfo += vcs_version_info_str + "\n\n";
/* XXX: GCC 12.1 has a bogus stringop-overread warning using the Qt
* conversions from QByteArray to QString at -O2 and higher due to
* computing a branch that will never be taken.
*/
#if WS_IS_AT_LEAST_GNUC_VERSION(12,1)
DIAG_OFF(stringop-overread)
#endif
clipboardInfo += gstring_free_to_qbytearray(get_compiled_version_info(gather_wireshark_qt_compiled_info)) + "\n";
clipboardInfo += gstring_free_to_qbytearray(get_runtime_version_info(gather_wireshark_runtime_info));
#if WS_IS_AT_LEAST_GNUC_VERSION(12,1)
DIAG_ON(stringop-overread)
#endif
}
void AboutDialog::on_copyToClipboard_clicked()

View File

@ -891,7 +891,17 @@ FollowStreamDialog::showBuffer(char *buffer, size_t nchars, gboolean is_from_ser
int len = current_pos + base64_raw_len < nchars ? base64_raw_len : (int) nchars - current_pos;
QByteArray base64_data(&buffer[current_pos], len);
/* XXX: GCC 12.1 has a bogus stringop-overread warning using the Qt
* conversions from QByteArray to QString at -O2 and higher due to
* computing a branch that will never be taken.
*/
#if WS_IS_AT_LEAST_GNUC_VERSION(12,1)
DIAG_OFF(stringop-overread)
#endif
yaml_text += " " + base64_data.toBase64() + "\n";
#if WS_IS_AT_LEAST_GNUC_VERSION(12,1)
DIAG_ON(stringop-overread)
#endif
current_pos += len;
(*global_pos) += len;

View File

@ -801,7 +801,17 @@ void ShowPacketBytesDialog::updatePacketBytes(void)
while (pos < len) {
QByteArray base64_data = field_bytes_.mid(pos, base64_raw_len);
pos += base64_data.length();
/* XXX: GCC 12.1 has a bogus stringop-overread warning using the Qt
* conversions from QByteArray to QString at -O2 and higher due to
* computing a branch that will never be taken.
*/
#if WS_IS_AT_LEAST_GNUC_VERSION(12,1)
DIAG_OFF(stringop-overread)
#endif
text.append(" " + base64_data.toBase64() + "\n");
#if WS_IS_AT_LEAST_GNUC_VERSION(12,1)
DIAG_ON(stringop-overread)
#endif
}
ui->tePacketBytes->setLineWrapMode(QTextEdit::NoWrap);

View File

@ -950,7 +950,17 @@ void WiresharkMainWindow::startCapture(QStringList interfaces _U_) {
/* Not the first selected interface; is its capture filter
the same as the one the other interfaces we've looked
at have? */
/* XXX: GCC 12.1 has a bogus warning at -O2 and higher
* even though the isEmpty() check guarantees that
* filter_ba.constData() is never NULL or empty.
*/
#if WS_IS_AT_LEAST_GNUC_VERSION(12,1)
DIAG_OFF(stringop-overread)
#endif
if (strcmp(interface_opts->cfilter, filter_ba.constData()) != 0) {
#if WS_IS_AT_LEAST_GNUC_VERSION(12,1)
DIAG_ON(stringop-overread)
#endif
/* No, so not all selected interfaces have the same capture
filter. */
filter_ba.clear();