From 0cb46fa03195ef553d7b277ef76d2fb1e66e6329 Mon Sep 17 00:00:00 2001 From: Mike Cohen Date: Tue, 23 Jan 2024 20:15:40 +1000 Subject: [PATCH] Issue error when array max_count is not an int. --- array.go | 8 +++++++- parser_test.go | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/array.go b/array.go index fcdd776..57d2ae8 100644 --- a/array.go +++ b/array.go @@ -52,7 +52,13 @@ func (self *ArrayParser) New(profile *Profile, options *ordereddict.Dict) (Parse // Default to 0 length result.options.Count, _ = options.GetInt64("count") - result.options.MaxCount, _ = options.GetInt64("max_count") + max_count_any, pres := options.Get("max_count") + if pres { + result.options.MaxCount, pres = to_int64(max_count_any) + if !pres { + return nil, fmt.Errorf("Array max_count must be an int not %T", max_count_any) + } + } if result.options.MaxCount == 0 { result.options.MaxCount = 1000 diff --git a/parser_test.go b/parser_test.go index ee40d48..f7ae79a 100644 --- a/parser_test.go +++ b/parser_test.go @@ -106,6 +106,29 @@ func TestStructParser(t *testing.T) { goldie.Assert(t, "TestStructParser", serialized) } +func TestArrayParserError(t *testing.T) { + profile := NewProfile() + AddModel(profile) + + scope := MakeScope() + scope.SetLogger(log.New(os.Stderr, " ", 0)) + + definition := ` +[ + ["TestStruct", 0, [ + ["Field1", 2, "Array", { + "max_count": "x=>x.Length", + "type": "uint8" + }], + ] +]] +` + err := profile.ParseStructDefinitions(definition) + assert.Error(t, err) + + assert.Contains(t, err.Error(), "Array max_count must be an int not string") +} + func TestArrayParser(t *testing.T) { profile := NewProfile() AddModel(profile)