From c12408b53fce13bc0138acfa3ad00aeabc214ba5 Mon Sep 17 00:00:00 2001
From: Albrecht Schlosser <albrechts.fltk@online.de>
Date: Tue, 2 Jun 2020 14:53:36 +0200
Subject: [PATCH] Avoid crash in Fl::next_window(win)

As documented, Fl::next_window(win) must only be called with a valid
*shown* window. The old code would crash if the argument was NULL
or the window was not shown.

The new code avoids the crash, issues an error message, and returns
NULL to the caller.
---
 src/Fl.cxx | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/Fl.cxx b/src/Fl.cxx
index 6742b0bc9..f6fef73e3 100644
--- a/src/Fl.cxx
+++ b/src/Fl.cxx
@@ -659,7 +659,12 @@ Fl_Window* Fl::first_window() {
   \param[in] window must be shown and not NULL
 */
 Fl_Window* Fl::next_window(const Fl_Window* window) {
-  Fl_X* i = Fl_X::i(window)->next;
+  Fl_X* i = window ? Fl_X::i(window) : 0;
+  if (!i) {
+    Fl::error("Fl::next_window() failed: window (%p) not shown.", window);
+    return 0;
+  }
+  i = i->next;
   return i ? i->w : 0;
 }
 
-- 
GitLab