From abb7d288e9e278a0e7428846909c2d5c790c5e3a Mon Sep 17 00:00:00 2001
From: Gabe Black <gabeblack@google.com>
Date: Thu, 23 Aug 2018 00:41:22 -0700
Subject: systemc: Use sc_assert to check the number of interfaces.

The sc_port code had been using the .at() function of the vector class,
but when that failed it threw an exception, and it was very difficult
to tell where the exception came from from how gem5 crashed. This
change switches to sc_assert (the systemc version of assert) which
makes the cause/location of failures much more obvious.

Change-Id: I1cd51c86f47b314be551c304b014c44cfa030175
Reviewed-on: https://gem5-review.googlesource.com/12262
Reviewed-by: Gabe Black <gabeblack@google.com>
Maintainer: Gabe Black <gabeblack@google.com>
---
 src/systemc/ext/core/sc_port.hh | 49 +++++++++++++++++++++++++++++++++++------
 1 file changed, 42 insertions(+), 7 deletions(-)

(limited to 'src/systemc')

diff --git a/src/systemc/ext/core/sc_port.hh b/src/systemc/ext/core/sc_port.hh
index 73f5362b6..f9e50da2e 100644
--- a/src/systemc/ext/core/sc_port.hh
+++ b/src/systemc/ext/core/sc_port.hh
@@ -105,14 +105,44 @@ class sc_port_b : public sc_port_base
     virtual void bind(IF &i) { sc_port_base::bind(i); }
     virtual void bind(sc_port_b<IF> &p) { sc_port_base::bind(p); }
 
-    IF *operator -> () { return _interfaces.at(0); }
-    const IF *operator -> () const { return _interfaces.at(0); }
+    IF *
+    operator -> ()
+    {
+        sc_assert(!_interfaces.empty());
+        return _interfaces[0];
+    }
+    const IF *
+    operator -> () const
+    {
+        sc_assert(!_interfaces.empty());
+        return _interfaces[0];
+    }
 
-    IF *operator [] (int n) { return _interfaces.at(n); }
-    const IF *operator [] (int n) const { return _interfaces.at(n); }
+    IF *
+    operator [] (int n)
+    {
+        sc_assert(_interfaces.size() > n);
+        return _interfaces[n];
+    }
+    const IF *
+    operator [] (int n) const
+    {
+        sc_assert(_interfaces.size() > n);
+        return _interfaces[n];
+    }
 
-    sc_interface *get_interface() { return _interfaces.at(0); }
-    const sc_interface *get_interface() const { return _interfaces.at(0); }
+    sc_interface *
+    get_interface()
+    {
+        sc_assert(!_interfaces.empty());
+        return _interfaces[0];
+    }
+    const sc_interface *
+    get_interface() const
+    {
+        sc_assert(!_interfaces.empty());
+        return _interfaces[0];
+    }
 
   protected:
     void before_end_of_elaboration() {}
@@ -151,7 +181,12 @@ class sc_port_b : public sc_port_base
   private:
     std::vector<IF *> _interfaces;
 
-    sc_interface *_gem5Interface(int n) const { return _interfaces.at(n); }
+    sc_interface *
+    _gem5Interface(int n) const
+    {
+        sc_assert(_interfaces.size() > n);
+        return _interfaces[n];
+    }
     void
     _gem5AddInterface(sc_interface *i)
     {
-- 
cgit v1.2.3