From 5a220abda6ec532027d6c99847e513f138958bf6 Mon Sep 17 00:00:00 2001 From: Christian Karlsson Date: Mon, 4 Nov 2024 17:04:33 +0100 Subject: [PATCH] fix(generate): handle invalid signal name for metadata --- internal/generate/compile.go | 1 + internal/generate/compile_test.go | 14 ++++++++++++++ .../example_metadata_invalid_signal_reference.dbc | 14 ++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 testdata/dbc-invalid/example/example_metadata_invalid_signal_reference.dbc diff --git a/internal/generate/compile.go b/internal/generate/compile.go index 2a6b5b5..ce77fb8 100644 --- a/internal/generate/compile.go +++ b/internal/generate/compile.go @@ -189,6 +189,7 @@ func (c *compiler) addMetadata() { sig, ok := c.db.Signal(def.MessageID.ToCAN(), string(def.SignalName)) if !ok { c.addWarning(&compileError{def: def, reason: "no declared signal"}) + continue } if def.AttributeName == "GenSigStartValue" { sig.DefaultValue = int(def.IntValue) diff --git a/internal/generate/compile_test.go b/internal/generate/compile_test.go index 052815d..e244ab7 100644 --- a/internal/generate/compile_test.go +++ b/internal/generate/compile_test.go @@ -403,3 +403,17 @@ func Test_CopyFrom_PreservesOutOfRangeValues(t *testing.T) { t.Errorf("Expected frames of messages to be identical (%v != %v)", m2.Frame(), original.Frame()) } } + +func TestCompile_MetadataInvalidSignalReference(t *testing.T) { + finish := runTestInDir(t, "../..") + defer finish() + const exampleDBCFile = "testdata/dbc-invalid/example/example_metadata_invalid_signal_reference.dbc" + input, err := os.ReadFile(exampleDBCFile) + assert.NilError(t, err) + result, err := Compile(exampleDBCFile, input) + if err != nil { + t.Fatal(err) + } + // We expect one warning for incorrect signal length in declaration of float32 signal + assert.Equal(t, len(result.Warnings), 1) +} diff --git a/testdata/dbc-invalid/example/example_metadata_invalid_signal_reference.dbc b/testdata/dbc-invalid/example/example_metadata_invalid_signal_reference.dbc new file mode 100644 index 0000000..8941ab4 --- /dev/null +++ b/testdata/dbc-invalid/example/example_metadata_invalid_signal_reference.dbc @@ -0,0 +1,14 @@ +VERSION "" + +NS_ : + +BS_: + +BU_: DBG IO + +BO_ 42 AMessage: 8 Vector__XXX + SG_ ASignal : 7|16@0- (1,0) [-32768|32767] "%" Vector__XXX + +BA_DEF_ SG_ "GenSigStartValue" FLOAT -3.4E+038 3.4E+038; +BA_DEF_DEF_ "GenSigStartValue" 0; +BA_ "GenSigStartValue" SG_ 42 AnotherSignalName 0;