Path: blob/main/editors/diamond/files/patch-e8f0d274471cf0a50a78aec102ffa87541887f2e.patch
16461 views
From e8f0d274471cf0a50a78aec102ffa87541887f2e Mon Sep 17 00:00:00 20011From: Adriaan de Groot <[email protected]>2Date: Sun, 20 Feb 2022 16:23:53 +01003Subject: [PATCH] Fix crash when passing filenames on command-line45Consider running `diamond file.txt`. If previously there6was an untitled tab open and nothing else, we arrive7here with 2 tabs, `cnt==2`. The first for-loop finds8an untitled tab at index `k==0` and decrements `cnt`,9then the for-loop increments `k` and the for-loop terminates10(because `1 < 1` is false). We have `cnt==1` but an **empty**11list `m_openedFiles`. This crashes with an out-of-bounds access12in the second for-loop, because `cnt` doesn't match the length13of the list anymore.1415As a fix:16- do not modify `cnt` in the first for-loop, always check17all of the current tabs,18- re-calculate the `cnt` based on the files that are actually19opened, before the second loop.20---21src/recent_tabs.cpp | 8 +++-----221 file changed, 3 insertions(+), 5 deletions(-)2324diff --git a/src/recent_tabs.cpp b/src/recent_tabs.cpp25index b3359ac..3eef680 10064426--- src/recent_tabs.cpp27+++ src/recent_tabs.cpp28@@ -31,15 +31,13 @@ void MainWindow::openTab_CreateMenus()29for (int k = 0; k < cnt; ++k) {30fullName = this->get_curFileName(k);3132- if (fullName.isEmpty()) {33- --cnt;34-35- } else {36+ if (!fullName.isEmpty()) {37m_openedFiles.append(fullName);38m_openedModified.append(false);39}40}41-42+ // How many were really opened43+ cnt = m_openedFiles.count();44//45QMenu *windowMenu = m_ui->menuWindow;46windowMenu->addSeparator();474849