Verify leaf node squashing 95/84095/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 30 Aug 2019 12:11:09 +0000 (14:11 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 30 Aug 2019 12:12:11 +0000 (14:12 +0200)
There is a very slight possiblity of somebody playing tricks
to inject Leaf node with a DataContainerChild value, in which case
decoding would break. Verify this does not happen.

JIRA: YANGTOOLS-1019
Change-Id: Idbd6457817b9db5f2b2d4b3fafce57169ace6ec1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/LazyLeafOperations.java

index 2dba8075b888c888bac06776aaeb7e030b03481c..dffa923f2a077ef33066da27110867f5923f7a44 100644 (file)
@@ -86,7 +86,7 @@ public final class LazyLeafOperations {
 
     public static void putChild(final Map<PathArgument, Object> map, final DataContainerChild<?, ?> child) {
         final DataContainerChild<?, ?> node = requireNonNull(child);
-        map.put(child.getIdentifier(), EXPENDABLE ? encodeExpendableChild(node) : node);
+        map.put(node.getIdentifier(), EXPENDABLE ? encodeExpendableChild(node) : node);
     }
 
     @SuppressWarnings({ "rawtypes", "unchecked" })
@@ -110,8 +110,13 @@ public final class LazyLeafOperations {
         return value instanceof DataContainerChild ? (DataContainerChild<?, ?>) value  : coerceLeaf(key, value);
     }
 
-    private static @NonNull Object encodeExpendableChild(final DataContainerChild<?, ?> key) {
-        return key instanceof LeafNode ? ((LeafNode<?>) key).getValue() : requireNonNull(key);
+    private static @NonNull Object encodeExpendableChild(final @NonNull DataContainerChild<?, ?> node) {
+        return node instanceof LeafNode ? verifyEncode(((LeafNode<?>) node).getValue()) : node;
+    }
+
+    private static @NonNull Object verifyEncode(final @NonNull Object value) {
+        verify(!(value instanceof DataContainerChild), "Unexpected leaf value %s", value);
+        return value;
     }
 
     private static @NonNull DataContainerChild<?, ?> verifyCast(final @NonNull Object value) {