From 9a9e04d0474b7977fd9c663e8d31bfb8868a8b37 Mon Sep 17 00:00:00 2001
From: jgromes <jan.gromes@gmail.com>
Date: Sun, 26 May 2024 09:25:13 +0200
Subject: [PATCH] Add check for self-assignment

---
 src/protocols/AX25/AX25.cpp                   | 20 ++++++++++---------
 src/protocols/ExternalRadio/ExternalRadio.cpp |  8 +++++---
 src/protocols/Print/ITA2String.cpp            | 14 +++++++------
 3 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/src/protocols/AX25/AX25.cpp b/src/protocols/AX25/AX25.cpp
index 480ea343..c5913e94 100644
--- a/src/protocols/AX25/AX25.cpp
+++ b/src/protocols/AX25/AX25.cpp
@@ -214,16 +214,18 @@ AX25Client::AX25Client(const AX25Client& ax25)
 }
 
 AX25Client& AX25Client::operator=(const AX25Client& ax25) {
-  this->phyLayer = ax25.phyLayer;
-  this->sourceSSID = ax25.sourceSSID;
-  this->preambleLen = ax25.preambleLen;
-  strncpy(sourceCallsign, ax25.sourceCallsign, RADIOLIB_AX25_MAX_CALLSIGN_LEN + 1);
-  #if !RADIOLIB_EXCLUDE_AFSK
-  if(ax25.bellModem) {
-    this->audio = ax25.audio;
-    this->bellModem = new BellClient(ax25.audio);
+  if(&ax25 != this) {
+    this->phyLayer = ax25.phyLayer;
+    this->sourceSSID = ax25.sourceSSID;
+    this->preambleLen = ax25.preambleLen;
+    strncpy(sourceCallsign, ax25.sourceCallsign, RADIOLIB_AX25_MAX_CALLSIGN_LEN + 1);
+    #if !RADIOLIB_EXCLUDE_AFSK
+    if(ax25.bellModem) {
+      this->audio = ax25.audio;
+      this->bellModem = new BellClient(ax25.audio);
+    }
+    #endif
   }
-  #endif
   return(*this);
 }
 
diff --git a/src/protocols/ExternalRadio/ExternalRadio.cpp b/src/protocols/ExternalRadio/ExternalRadio.cpp
index d8c3a5fa..212f8a15 100644
--- a/src/protocols/ExternalRadio/ExternalRadio.cpp
+++ b/src/protocols/ExternalRadio/ExternalRadio.cpp
@@ -22,9 +22,11 @@ ExternalRadio::ExternalRadio(const ExternalRadio& ext) : PhysicalLayer(1, 0) {
 }
 
 ExternalRadio& ExternalRadio::operator=(const ExternalRadio& ext) {
-  this->prevFrf = ext.prevFrf;
-  if(ext.mod) {
-    this->mod = new Module(ext.mod->hal, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, ext.mod->getGpio());
+  if(&ext != this) {
+    this->prevFrf = ext.prevFrf;
+    if(ext.mod) {
+      this->mod = new Module(ext.mod->hal, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, ext.mod->getGpio());
+    }
   }
   return(*this);
 }
diff --git a/src/protocols/Print/ITA2String.cpp b/src/protocols/Print/ITA2String.cpp
index 6c67faa5..368edebf 100644
--- a/src/protocols/Print/ITA2String.cpp
+++ b/src/protocols/Print/ITA2String.cpp
@@ -30,12 +30,14 @@ ITA2String::ITA2String(const ITA2String& ita2) {
 }
 
 ITA2String& ITA2String::operator=(const ITA2String& ita2) {
-  this->asciiLen = ita2.asciiLen;
-  this->ita2Len = ita2.ita2Len;
-  #if !RADIOLIB_STATIC_ONLY
-  this->strAscii = new char[asciiLen + 1];
-  #endif
-  strcpy(this->strAscii, ita2.strAscii);
+  if(&ita2 != this) {
+    this->asciiLen = ita2.asciiLen;
+    this->ita2Len = ita2.ita2Len;
+    #if !RADIOLIB_STATIC_ONLY
+    this->strAscii = new char[asciiLen + 1];
+    #endif
+    strcpy(this->strAscii, ita2.strAscii);
+  }
   return(*this);
 }