From 339ca8154c758227089f950d9bc08992204bcc89 Mon Sep 17 00:00:00 2001 From: Brendan O'Donoghue Date: Sat, 24 Aug 2024 15:01:03 +0100 Subject: [PATCH] Fix ut-of-bounds read inside norm_inf by returning 0.0 for 0 len arrays in norm_inf, closes #284 (#285) --- 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]); }