From 2e89c735c0b8e8b6948da9bb8f85d2b64b32e3ce Mon Sep 17 00:00:00 2001
From: Jeff Young <jeff@rokeby.ie>
Date: Thu, 13 Jul 2023 16:03:40 +0100
Subject: [PATCH] Fix buffer overrun.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/15187
---
 eeschema/sim/sim_plot_tab.cpp    | 24 ++++++++++--------------
 eeschema/sim/sim_plot_tab.h      |  2 +-
 eeschema/sim/simulator_panel.cpp |  4 ++--
 3 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/eeschema/sim/sim_plot_tab.cpp b/eeschema/sim/sim_plot_tab.cpp
index de57d208b7..05d7423d89 100644
--- a/eeschema/sim/sim_plot_tab.cpp
+++ b/eeschema/sim/sim_plot_tab.cpp
@@ -826,19 +826,15 @@ TRACE* SIM_PLOT_TAB::AddTrace( const wxString& aVectorName, int aType )
 }
 
 
-void SIM_PLOT_TAB::SetTraceData( TRACE* trace, unsigned int aPoints, const double* aX,
-                                 const double* aY )
+void SIM_PLOT_TAB::SetTraceData( TRACE* trace, std::vector<double>& aX, std::vector<double>& aY )
 {
-    std::vector<double> x( aX, aX + aPoints );
-    std::vector<double> y( aY, aY + aPoints );
-
     if( dynamic_cast<LOG_SCALE<mpScaleXLog>*>( m_axis_x ) )
     {
         // log( 0 ) is not valid.
-        if( x.size() > 0 && x[0] == 0 )
+        if( aX.size() > 0 && aX[0] == 0 )
         {
-            x.erase( x.begin() );
-            y.erase( y.begin() );
+            aX.erase( aX.begin() );
+            aY.erase( aY.begin() );
         }
     }
 
@@ -846,21 +842,21 @@ void SIM_PLOT_TAB::SetTraceData( TRACE* trace, unsigned int aPoints, const doubl
     {
         if( trace->GetType() & SPT_AC_PHASE )
         {
-            for( unsigned int i = 0; i < aPoints; i++ )
-                y[i] = y[i] * 180.0 / M_PI;                 // convert to degrees
+            for( double& pt : aY )
+                pt = pt * 180.0 / M_PI;                     // convert to degrees
         }
         else
         {
-            for( unsigned int i = 0; i < aPoints; i++ )
+            for( double& pt : aY )
             {
                 // log( 0 ) is not valid.
-                if( y[i] != 0 )
-                    y[i] = 20 * log( y[i] ) / log( 10.0 );  // convert to dB
+                if( pt != 0 )
+                    pt = 20 * log( pt ) / log( 10.0 );      // convert to dB
             }
         }
     }
 
-    trace->SetData( x, y );
+    trace->SetData( aX, aY );
 
     if( ( trace->GetType() & SPT_AC_PHASE ) || ( trace->GetType() & SPT_CURRENT ) )
         trace->SetScale( m_axis_x, m_axis_y2 );
diff --git a/eeschema/sim/sim_plot_tab.h b/eeschema/sim/sim_plot_tab.h
index 9470d45e7e..0680877d6f 100644
--- a/eeschema/sim/sim_plot_tab.h
+++ b/eeschema/sim/sim_plot_tab.h
@@ -321,7 +321,7 @@ public:
 
     TRACE* AddTrace( const wxString& aVectorName, int aType );
 
-    void SetTraceData( TRACE* aTrace, unsigned int aPoints, const double* aX, const double* aY );
+    void SetTraceData( TRACE* aTrace, std::vector<double>& aX, std::vector<double>& aY );
 
     bool DeleteTrace( const wxString& aVectorName, int aTraceType );
     void DeleteTrace( TRACE* aTrace );
diff --git a/eeschema/sim/simulator_panel.cpp b/eeschema/sim/simulator_panel.cpp
index e35baed125..9485c4198b 100644
--- a/eeschema/sim/simulator_panel.cpp
+++ b/eeschema/sim/simulator_panel.cpp
@@ -1621,7 +1621,7 @@ void SIMULATOR_PANEL::updateTrace( const wxString& aVectorName, int aTraceType,
                     std::vector<double> sub_y( data_y.begin() + offset,
                                                data_y.begin() + offset + inner );
 
-                    aPlotTab->SetTraceData( trace, inner, sub_x.data(), sub_y.data() );
+                    aPlotTab->SetTraceData( trace, sub_x, sub_y );
                 }
             }
 
@@ -1632,7 +1632,7 @@ void SIMULATOR_PANEL::updateTrace( const wxString& aVectorName, int aTraceType,
     else if( TRACE* trace = aPlotTab->AddTrace( aVectorName, aTraceType ) )
     {
         if( data_y.size() >= size )
-            aPlotTab->SetTraceData( trace, size, data_x.data(), data_y.data() );
+            aPlotTab->SetTraceData( trace, data_x, data_y );
     }
 }