From 46c603ce3c9eba660f425cc4983ac2c8fbe06b0d Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Windows 11)" <rob@synchro.net>
Date: Mon, 26 Aug 2024 17:07:32 -0700
Subject: [PATCH] Fix NULL pointer deref in random_menu()

When no files with extensions are found, though they matched the glob()
pattern, a NULL pointer deref would result (segfault).

This could happen if the only files matching the pattern had no extenions or
were directories (not files).

Fix for issue #779
---
 src/sbbs3/prntfile.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/sbbs3/prntfile.cpp b/src/sbbs3/prntfile.cpp
index fc4632dce6..4f94366d36 100644
--- a/src/sbbs3/prntfile.cpp
+++ b/src/sbbs3/prntfile.cpp
@@ -373,9 +373,12 @@ bool sbbs_t::random_menu(const char *name, int mode, JSObject* obj)
 	globfree(&g);
 	strListDedupe(&names, /* case_sensitive: */true);
 	bool result = false;
-	size_t i = sbbs_random(strListCount(names));
-	if(menu_exists(names[i], NULL, path)) {
-		result = menu(names[i], mode, obj);
+	size_t count = strListCount(names);
+	if(count > 0) {
+		size_t i = sbbs_random(count);
+		if(menu_exists(names[i], NULL, path)) {
+			result = menu(names[i], mode, obj);
+		}
 	}
 	strListFree(&names);
 	return result;
-- 
GitLab