From c51efdeb4474ebaf80c0b88e88ac3cbccbddf481 Mon Sep 17 00:00:00 2001
From: Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
Date: Tue, 6 Dec 2022 15:15:50 +0900
Subject: [PATCH] Fix build with -Werror=aggressive-loop-optimizations option
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add size check for d_alen and size.

```
inlined from ‘int main(int, char**)’ at t_array.cpp:27:43:
../../../../bld/hdr/bit/array.tcc:265:7: error: iteration 2305843009213693952 invokes undefined behavior [-Werror=aggressive-loop-optimizations]
  265 |       for (long k = 0; k < d_alen; k++) data[k] = p_data[k];
      |       ^~~
../../../../bld/hdr/bit/array.tcc:265:26: note: within this loop
  265 |       for (long k = 0; k < d_alen; k++) data[k] = p_data[k];
      |                        ~~^~~~~~~~
cc1plus: all warnings being treated as errors
```

Signed-off-by: Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
---
 src/lib/bit/shl/array.tcc | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/lib/bit/shl/array.tcc b/src/lib/bit/shl/array.tcc
index 72724046..2809eda7 100644
--- a/src/lib/bit/shl/array.tcc
+++ b/src/lib/bit/shl/array.tcc
@@ -262,7 +262,9 @@ namespace afnix {
       if ((size <= 0L) || (size <= d_size)) return;
       // allocate a new array and copy
       T* data = new T[size];
-      for (long k = 0; k < d_alen; k++) data[k] = p_data[k];
+      long cnt;
+      size > d_alen ? cnt = d_alen : cnt = size;
+      for (long k = 0; k < cnt; k++) data[k] = p_data[k];
       delete [] p_data; p_data = data; d_size = size;
     }
   };
-- 
2.36.1

