From f9b749c6df58eb0fd8bf1ad6a33ad955ed8235f1 Mon Sep 17 00:00:00 2001
From: Brendan O'Donoghue <bodonoghue85@gmail.com>
Date: Sat, 24 Aug 2024 14:43:10 +0100
Subject: [PATCH] Fix ut-of-bounds read inside norm_inf by returning 0.0 for 0
 len arrays in norm_inf, closes #284

---
 src/linalg.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/linalg.c b/src/linalg.c
index 0d0ecea3..09b78a46 100644
--- a/src/linalg.c
+++ b/src/linalg.c
@@ -153,9 +153,14 @@ scs_float SCS(norm_inf)(const scs_float *a, scs_int len) {
 */
 
 scs_float SCS(norm_inf)(const scs_float *a, scs_int len) {
+  /* Follow the semantics of BLASI(lange) for zero-size array. */
+  scs_int idx;
   blas_int bone = 1;
   blas_int blen = (blas_int)len;
-  scs_int idx = (scs_int)BLASI(amax)(&blen, a, &bone);
+  if (len <= 0) {
+    return 0.0;
+  }
+  idx = (scs_int)BLASI(amax)(&blen, a, &bone);
   /* Returned idx is 1-based. */
   return ABS(a[idx - 1]);
 }