From e41e7774f6cd0aaf924ab2b9806873281bc3c8db Mon Sep 17 00:00:00 2001
From: Rob Swindell <rob@synchro.net>
Date: Thu, 12 Jan 2023 19:37:36 -0800
Subject: [PATCH] Fix possible crash when no extractable file types are
 configured in SCFG

If libarchive couldn't extract a QWK or REP packet, we'd fallback to searching
for a match among the configured extractable file types and if no extension/type
match was found, default to the first configured extractable file type
(even if there wasn't one) which would result in a NULL pointer dereference
and most likely a crash. Instead, if no matching configured extractable
file type is found, just log a warning message and don't continue with the
extraction attempt. With SBBS v3.19+, it's totally valid/legit to have no
extractable file types configured in SCFG and things "just work" (using
libarchive).
---
 src/sbbs3/pack_qwk.cpp | 6 ++++--
 src/sbbs3/un_rep.cpp   | 6 ++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/sbbs3/pack_qwk.cpp b/src/sbbs3/pack_qwk.cpp
index 8bf391660d..b002addd6a 100644
--- a/src/sbbs3/pack_qwk.cpp
+++ b/src/sbbs3/pack_qwk.cpp
@@ -87,8 +87,10 @@ bool sbbs_t::pack_qwk(char *packet, ulong *msgcnt, bool prepack)
 				if(!stricmp(cfg.fextr[k]->ext,useron.tmpext)
 					&& chk_ar(cfg.fextr[k]->ar,&useron,&client))
 					break;
-			if(k>=cfg.total_fextrs)
-				k=0;
+			if(k>=cfg.total_fextrs) {
+				lprintf(LOG_WARNING, "No extractable file type matching user's QWK packet type: %s", useron.tmpext);
+				return false;
+			}
 			p=cmdstr(cfg.fextr[k]->cmd,str,ALLFILES,NULL);
 			if((i=external(p,ex))==0)
 				preqwk=1; 
diff --git a/src/sbbs3/un_rep.cpp b/src/sbbs3/un_rep.cpp
index 9eac98f80a..7c211c5efa 100644
--- a/src/sbbs3/un_rep.cpp
+++ b/src/sbbs3/un_rep.cpp
@@ -85,8 +85,10 @@ bool sbbs_t::unpack_rep(char* repfile)
 		for(k=0;k<cfg.total_fextrs;k++)
 			if(!stricmp(cfg.fextr[k]->ext,useron.tmpext) && chk_ar(cfg.fextr[k]->ar,&useron,&client))
 				break;
-		if(k>=cfg.total_fextrs)
-			k=0;
+		if(k>=cfg.total_fextrs) {
+			lprintf(LOG_WARNING, "No extractable file type matching user's REP packet type: %s", useron.tmpext);
+			return false;
+		}
 		ex=EX_STDOUT;
 		if(online!=ON_REMOTE)
 			ex|=EX_OFFLINE;
-- 
GitLab