From c8e4270e391da31c25848aa10e3fdb60c554fe35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Schr=C3=B6der?= Date: Mon, 22 Nov 2021 11:50:04 +0100 Subject: [PATCH 1/2] ua: encode nil NodeIDs as two byte zero ids This patch encodes a 'nil' NodeID as a two byte zero node id. See #506 --- ua/node_id.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ua/node_id.go b/ua/node_id.go index 721845cf..6585ece4 100644 --- a/ua/node_id.go +++ b/ua/node_id.go @@ -405,6 +405,12 @@ func (n *NodeID) Decode(b []byte) (int, error) { } func (n *NodeID) Encode() ([]byte, error) { + // https://github.com/gopcua/opcua/issues/506 + // encode 'nil' node ids as two byte zero values + if n == nil { + return []byte{0, 0}, nil + } + buf := NewBuffer(nil) buf.WriteByte(byte(n.mask)) From 19eef56c860a43a9121c94e32d9b59afba44be7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Schr=C3=B6der?= Date: Mon, 22 Nov 2021 12:01:04 +0100 Subject: [PATCH 2/2] ua: test encode nil node id as zero --- ua/node_id_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ua/node_id_test.go b/ua/node_id_test.go index 72bc5fa4..9e4e89bc 100644 --- a/ua/node_id_test.go +++ b/ua/node_id_test.go @@ -12,6 +12,7 @@ import ( "testing" "github.com/gopcua/opcua/errors" + "github.com/pascaldekloe/goe/verify" ) func TestNodeID(t *testing.T) { @@ -132,6 +133,15 @@ func BenchmarkReflectDecode(b *testing.B) { } } +func TestEncodeNilNodeID(t *testing.T) { + var n *NodeID + b, err := n.Encode() + if err != nil { + t.Fatalf("got error %v want %v", err, nil) + } + verify.Values(t, "", b, []byte{0, 0}) +} + func TestParseNodeID(t *testing.T) { cases := []struct { s string