From c5a1be2e93186df3c20ed2c0512dfd2e1ce7799e Mon Sep 17 00:00:00 2001
From: Marcus Müller <mmueller@gnuradio.org>
Date: Mon, 25 Jan 2021 23:50:04 +0100
Subject: digital: packed<->unpacked: check chunk sizes, incl. NDBEBUG builds
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Also, get rid of impossible asserts(), and throw exceptions instead of
assert(0)-aborting, as we now have facilities to handle failed
block::work().

Signed-off-by: Marcus Müller <mmueller@gnuradio.org>
---
 gr-blocks/lib/unpacked_to_packed_impl.cc | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

(limited to 'gr-blocks/lib/unpacked_to_packed_impl.cc')

diff --git a/gr-blocks/lib/unpacked_to_packed_impl.cc b/gr-blocks/lib/unpacked_to_packed_impl.cc
index e535051985..5ffb0433e4 100644
--- a/gr-blocks/lib/unpacked_to_packed_impl.cc
+++ b/gr-blocks/lib/unpacked_to_packed_impl.cc
@@ -15,7 +15,8 @@
 
 #include "unpacked_to_packed_impl.h"
 #include <gnuradio/io_signature.h>
-#include <cassert>
+#include <boost/format.hpp>
+#include <stdexcept>
 
 namespace gr {
 namespace blocks {
@@ -39,8 +40,12 @@ unpacked_to_packed_impl<T>::unpacked_to_packed_impl(unsigned int bits_per_chunk,
       d_endianness(endianness),
       d_index(0)
 {
-    assert(bits_per_chunk <= d_bits_per_type);
-    assert(bits_per_chunk > 0);
+    if (bits_per_chunk > d_bits_per_type) {
+        GR_LOG_ERROR(this->d_logger,
+                     boost::format("Requested to put %d in a %d bit chunk") %
+                         bits_per_chunk % d_bits_per_type);
+        throw std::domain_error("can't have more bits in chunk than in output type");
+    }
 
     this->set_relative_rate((uint64_t)bits_per_chunk, (uint64_t)this->d_bits_per_type);
 }
@@ -83,7 +88,6 @@ int unpacked_to_packed_impl<T>::general_work(int noutput_items,
 {
     unsigned int index_tmp = d_index;
 
-    assert(input_items.size() == output_items.size());
     const int nstreams = input_items.size();
 
     for (int m = 0; m < nstreams; m++) {
@@ -122,7 +126,8 @@ int unpacked_to_packed_impl<T>::general_work(int noutput_items,
             break;
 
         default:
-            assert(0);
+            GR_LOG_ERROR(this->d_logger, "unknown endianness");
+            throw std::runtime_error("unknown endianness");
         }
     }
 
-- 
cgit v1.2.3