From 8658d2b49e0e0113ff9d5b97fa166edd071759f6 Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Tue, 14 Apr 2020 09:59:29 +0200
Subject: [PATCH] Avoid segfault when reading props file from another program
 version.

---
 band.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/band.c b/band.c
index a8f0809..5ca08cf 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,25 @@ 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)
+
+        //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