From 4c7c8f596d902c60ec0be78151362239491df41f Mon Sep 17 00:00:00 2001 From: demoManito <1430482733@qq.com> Date: Thu, 7 Mar 2024 16:27:08 +0800 Subject: [PATCH] refactor(scan.go): array index is out of bounds exits the for loop --- scan.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/scan.go b/scan.go index 415b9f0d..ba4a6964 100644 --- a/scan.go +++ b/scan.go @@ -311,13 +311,16 @@ func Scan(rows Rows, db *DB, mode ScanMode) { db.scanIntoStruct(rows, elem, values, fields, joinFields) if !update { + // array index is out of bounds, exits the for loop + if isArrayKind && reflectValue.Len() < int(db.RowsAffected) { + break + } + if !isPtr { elem = elem.Elem() } if isArrayKind { - if reflectValue.Len() >= int(db.RowsAffected) { - reflectValue.Index(int(db.RowsAffected - 1)).Set(elem) - } + reflectValue.Index(int(db.RowsAffected - 1)).Set(elem) } else { reflectValue = reflect.Append(reflectValue, elem) }