From c1b0b19d64bad50197c2888ad4a72d429206985a Mon Sep 17 00:00:00 2001 From: Roman Leonenkov Date: Fri, 30 Jun 2023 00:37:48 +0100 Subject: [PATCH] fix: index out of range in examples/credit-card-form when ccn is empty (#770) --- examples/credit-card-form/main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/credit-card-form/main.go b/examples/credit-card-form/main.go index ba5b2b3363..71b3bf58bf 100644 --- a/examples/credit-card-form/main.go +++ b/examples/credit-card-form/main.go @@ -53,14 +53,15 @@ func ccnValidator(s string) error { return fmt.Errorf("CCN is too long") } + if len(s) == 0 || len(s)%5 != 0 && (s[len(s)-1] < '0' || s[len(s)-1] > '9') { + return fmt.Errorf("CCN is invalid") + } + // The last digit should be a number unless it is a multiple of 4 in which // case it should be a space if len(s)%5 == 0 && s[len(s)-1] != ' ' { return fmt.Errorf("CCN must separate groups with spaces") } - if len(s)%5 != 0 && (s[len(s)-1] < '0' || s[len(s)-1] > '9') { - return fmt.Errorf("CCN is invalid") - } // The remaining digits should be integers c := strings.ReplaceAll(s, " ", "")