diff --git a/ygot/pathstrings.go b/ygot/pathstrings.go index 095fce4b..796a93d0 100644 --- a/ygot/pathstrings.go +++ b/ygot/pathstrings.go @@ -294,6 +294,14 @@ func extractKV(in string) (string, map[string]string, error) { inEscape = false } + if inKey { + return "", nil, fmt.Errorf("received an unterminated key in element %s", in) + } + + if inEscape { + return "", nil, fmt.Errorf("received a trailing escape character in element %s", in) + } + if len(keys) == 0 { name = buf.String() } diff --git a/ygot/pathstrings_test.go b/ygot/pathstrings_test.go index 2b188124..8b844da4 100644 --- a/ygot/pathstrings_test.go +++ b/ygot/pathstrings_test.go @@ -357,6 +357,16 @@ func TestStringToPath(t *testing.T) { in: `/foo/bar[[bar=baz]`, wantSliceErr: "received an unescaped [ in key of element bar", wantStructuredErr: "received an unescaped [ in key of element bar", + }, { + name: "unterminated key", + in: `/foo/bar[baz=bat`, + wantSliceErr: "received an unterminated key in element bar[baz=bat", + wantStructuredErr: "received an unterminated key in element bar[baz=bat", + }, { + name: "unterminated key with escaped ]", + in: `/foo/bar[baz=bat\]`, + wantSliceErr: `received an unterminated key in element bar[baz=bat\]`, + wantStructuredErr: `received an unterminated key in element bar[baz=bat\]`, }, { name: "element with unescaped ]", in: `/foo/bar]`, @@ -485,6 +495,18 @@ func TestPathToSchemaPath(t *testing.T) { Element: []string{"interfaces", "interface[name=eth0]", "config", "description"}, }, want: "/interfaces/interface/config/description", + }, { + name: "element path with an unterminated predicate", + inPath: &gnmipb.Path{ + Element: []string{"interfaces", "interface[name=eth0"}, + }, + wantErrSubstring: "received an unterminated key in element interface[name=eth0", + }, { + name: "element path with a trailing escape character", + inPath: &gnmipb.Path{ + Element: []string{"interfaces", `interface\`}, + }, + wantErrSubstring: `received a trailing escape character in element interface\`, }, { name: "elem path with no keys", inPath: &gnmipb.Path{