From ba47bb699dba5d27ad97abc8df55dfc369916f4a Mon Sep 17 00:00:00 2001
From: DL1YCF <dl1ycf@darc.de>
Date: Sat, 10 Oct 2020 13:31:06 +0200
Subject: [PATCH] Avoid segfaults when processing invalid props file

---
 band.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/band.c b/band.c
index a8f0809..de1712e 100644
--- a/band.c
+++ b/band.c
@@ -503,6 +503,7 @@ void bandSaveState() {
 
 void bandRestoreState() {
     char* value;
+    int v;
     int b;
     int stack;
     char name[128];
@@ -513,13 +514,26 @@ void bandRestoreState() {
         value=getProperty(name);
         if(value) strcpy(bands[b].title,value);
 
-        sprintf(name,"band.%d.entries",b);
-        value=getProperty(name);
-        if(value) bands[b].bandstack->entries=atoi(value);
+	// The number of entries is a compile-time constant,
+        // which changes when compiling piHPSDR with different
+	// options (e.g. with and without SOAPYSDR)
+	// Therefore this number cannot be "restored" from a props file
+
+        //sprintf(name,"band.%d.entries",b);
+        //lue=getProperty(name);
+        //if(value) bands[b].bandstack->entries=atoi(value);
 
         sprintf(name,"band.%d.current",b);
         value=getProperty(name);
-        if(value) bands[b].bandstack->current_entry=atoi(value);
+        if(value) {
+	  //
+	  // Since the number of bandstack entries is a compile-time constant,
+	  // we cannot allow "current" to exceed the number of available slots
+	  //
+	  v=atoi(value);
+	  if (v >= bands[b].bandstack->entries) v=0;
+	  bands[b].bandstack->current_entry=v;
+	}
 
         sprintf(name,"band.%d.preamp",b);
         value=getProperty(name);
-- 
2.45.2