From c660896202536e29d84c774ba790f7783667ecb1 Mon Sep 17 00:00:00 2001 From: datadiode Date: Tue, 10 Jan 2023 03:07:47 +0100 Subject: [PATCH] Don't let QWindowsXpNativeFileDialog::populateOpenFileName() operate on uninitialized memory when faced with an empty filter list --- .../windows/qwindowsdialoghelpers.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp b/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp index 8f0cd3857e9..65baf360f4a 100644 --- a/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp +++ b/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp @@ -1786,16 +1786,17 @@ void QWindowsXpNativeFileDialog::populateOpenFileName(OPENFILENAME *ofn, HWND ow int totalStringLength = 0; const QList specs = filterSpecs(m_options->nameFilters(), m_options->options() & QFileDialogOptions::HideNameFilterDetails, &totalStringLength); - const int size = specs.size(); - auto *ptr = new wchar_t[totalStringLength + 2 * size + 1]; - ofn->lpstrFilter = ptr; - for (const FilterSpec &spec : specs) { - ptr += spec.description.toWCharArray(ptr); - *ptr++ = 0; - ptr += spec.filter.toWCharArray(ptr); - *ptr++ = 0; + if (const int size = specs.size()) { + auto *ptr = new wchar_t[totalStringLength + 2 * size + 1]; + ofn->lpstrFilter = ptr; + for (const FilterSpec &spec : specs) { + ptr += spec.description.toWCharArray(ptr); + *ptr++ = 0; + ptr += spec.filter.toWCharArray(ptr); + *ptr++ = 0; + } + *ptr = 0; } - *ptr = 0; const int nameFilterIndex = indexOfNameFilter(m_options->nameFilters(), m_data.selectedNameFilter()); if (nameFilterIndex >= 0) ofn->nFilterIndex = nameFilterIndex + 1; // 1..n based.