nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: eclipse/leshan
Pull request #426 (2017-11-09 10:54:15)

#416 : raise CodecException if 2 Tlv resource instance with the same id


Patch:
diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/tlv/LwM2mNodeTlvDecoder.java b/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/tlv/LwM2mNodeTlvDecoder.java
index 13ef1161..c05b3541 100644
--- a/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/tlv/LwM2mNodeTlvDecoder.java
+++ b/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/tlv/LwM2mNodeTlvDecoder.java
@@ -181,7 +181,12 @@ public class LwM2mNodeTlvDecoder {
                 throw new CodecException("Expected TLV of type RESOURCE_INSTANCE but was %s for path %s",
                         tlvChild.getType().name(), path);
 
-            values.put(tlvChild.getIdentifier(), parseTlvValue(tlvChild.getValue(), expectedType, path));
+            Object resourceInstance = parseTlvValue(tlvChild.getValue(), expectedType, path);
+            Object previousResourceInstance = values.put(tlvChild.getIdentifier(), resourceInstance);
+            if (previousResourceInstance != null) {
+                throw new CodecException("2 RESOURCE_INSTANCE (%s,%s) with the same identifier %d for path %s",
+                        previousResourceInstance, resourceInstance, tlvChild.getIdentifier(), path);
+            }
         }
         return values;
     }


Test patch:
diff --git a/leshan-core/src/test/java/org/eclipse/leshan/core/node/codec/LwM2mNodeDecoderTest.java b/leshan-core/src/test/java/org/eclipse/leshan/core/node/codec/LwM2mNodeDecoderTest.java
index 8bc7675d..050d86df 100644
--- a/leshan-core/src/test/java/org/eclipse/leshan/core/node/codec/LwM2mNodeDecoderTest.java
+++ b/leshan-core/src/test/java/org/eclipse/leshan/core/node/codec/LwM2mNodeDecoderTest.java
@@ -31,6 +31,7 @@ import org.eclipse.leshan.core.node.LwM2mObjectInstance;
 import org.eclipse.leshan.core.node.LwM2mPath;
 import org.eclipse.leshan.core.node.LwM2mResource;
 import org.eclipse.leshan.core.node.LwM2mSingleResource;
+import org.eclipse.leshan.core.node.ObjectLink;
 import org.eclipse.leshan.core.node.TimestampedLwM2mNode;
 import org.eclipse.leshan.core.request.ContentFormat;
 import org.eclipse.leshan.tlv.Tlv;
@@ -260,6 +261,16 @@ public class LwM2mNodeDecoderTest {
         assertTrue(resource.getValues().isEmpty());
     }
 
+    @Test(expected = CodecException.class)
+    public void tlv_invalid_multi_resource_2_instance_with_the_same_id() {
+        Tlv resInstance1 = new Tlv(TlvType.RESOURCE_INSTANCE, null, TlvEncoder.encodeObjlnk(new ObjectLink(100, 1)), 0);
+        Tlv resInstance2 = new Tlv(TlvType.RESOURCE_INSTANCE, null, TlvEncoder.encodeObjlnk(new ObjectLink(101, 2)), 0);
+        Tlv multiResource = new Tlv(TlvType.MULTIPLE_RESOURCE, new Tlv[] { resInstance1, resInstance2 }, null, 22);
+        byte[] content = TlvEncoder.encode(new Tlv[] { multiResource }).array();
+
+        decoder.decode(content, ContentFormat.TLV, new LwM2mPath(3, 0, 22), model);
+    }
+
     @Test
     public void json_device_object_instance0() throws CodecException {
         // json content for instance 0 of device object