From 9391ac7898445e7e6213a2a2486d9b073de2cfe0 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Wed, 15 Apr 2020 02:12:39 +0000
Subject: [PATCH] smb_open_sub() will now create the msgbase if hasn't been
 created yet.

I noticed that more than one caller of smb_open_sub() would not call
smb_create() if the sub had not been previously "created" and in some of the
instances where they did call smb_create(), if create failed, the message base
was errorneously left open. So just do the create here to elmiinate the chance
of error (forgetting) and redundancy of logic.
---
 src/sbbs3/scfgsave.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/sbbs3/scfgsave.c b/src/sbbs3/scfgsave.c
index c0998d206c..1c975397c5 100644
--- a/src/sbbs3/scfgsave.c
+++ b/src/sbbs3/scfgsave.c
@@ -1127,6 +1127,8 @@ int DLLCALL smb_storage_mode(scfg_t* cfg, smb_t* smb)
 	return SMB_SELFPACK;
 }
 
+/* Open Synchronet Message Base and create, if necessary (e.g. first time opened) */
+/* If return value is not SMB_SUCCESS, sub-board is not left open */
 int DLLCALL smb_open_sub(scfg_t* cfg, smb_t* smb, unsigned int subnum)
 {
 	int retval;
@@ -1148,8 +1150,13 @@ int DLLCALL smb_open_sub(scfg_t* cfg, smb_t* smb, unsigned int subnum)
 		smb->status.attr		= cfg->sub[subnum]->misc&SUB_HYPER ? SMB_HYPERALLOC :0;
 	}
 	smb->retry_time = cfg->smb_retry_time;
-	if((retval = smb_open(smb)) == SMB_SUCCESS)
-		smb->subnum = subnum;
+	if((retval = smb_open(smb)) == SMB_SUCCESS) {
+		if(filelength(fileno(smb->shd_fp)) < sizeof(smbhdr_t) + sizeof(smb->status)) {
+			if((retval = smb_create(smb)) != SMB_SUCCESS)
+				smb_close(smb);
+		}
+		if(retval == SMB_SUCCESS)
+			smb->subnum = subnum;
+	}
 	return retval;
 }
-
-- 
GitLab