From c77b4ebd98706f76f25381585c8bf951ed6cf3da Mon Sep 17 00:00:00 2001
From: Gerhard Gonter <ggonter@gmail.com>
Date: Thu, 26 Apr 2018 18:10:21 +0200
Subject: [PATCH] do not use substr out of bounds

---
 modules/util/Util/hexdump.pm | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/modules/util/Util/hexdump.pm b/modules/util/Util/hexdump.pm
index e9df0fa..0cce957 100644
--- a/modules/util/Util/hexdump.pm
+++ b/modules/util/Util/hexdump.pm
@@ -20,35 +20,35 @@ sub hexdump
   my ($i, $c, $v);
 
   my $run= 1;
+  my $dl= length ($data);
   DATA: while ($run)
   {
     my $char= '';
     my $hex= '';
     my $offx= sprintf ('%08X', $off);
 
-    for ($i= 0; $i < 16; $i++)
+    BYTE: for ($i= 0; $i < 16; $i++)
     {
-      $c= substr ($data, $off+$i, 1);
+      $hex .= ' ' if ($i == 8);
 
-      if ($i == 8)
+      if ($off+$i < $dl)
       {
-        $hex  .= ' ';
-      }
+        $c= substr ($data, $off+$i, 1);
 
-      if ($c ne '')
-      {
-        $v= unpack ('C', $c);
-        $c= '.' if ($v < 0x20 || $v >= 0x7F);
+        if (defined ($c) && $c ne '')
+        {
+          $v= unpack ('C', $c);
+          $c= '.' if ($v < 0x20 || $v >= 0x7F);
 
-        $char .= $c;
-        $hex .= sprintf (' %02X', $v);
-      }
-      else
-      {
-        $char .= ' ';
-        $hex  .= '   ';
-        $run= 0;
+          $char .= $c;
+          $hex .= sprintf (' %02X', $v);
+          next BYTE;
+        }
       }
+
+      $char .= ' ';
+      $hex  .= '   ';
+      $run= 0;
     }
 
     print FX "$offx $hex  |$char|\n";
-- 
GitLab