diff --git a/src/main/java/net/floodlightcontroller/packet/Data.java b/src/main/java/net/floodlightcontroller/packet/Data.java
index 47762da823dcb586a70d8612db0f6ab5b422cc23..b77e563c0aedb01d72b3547415b5f4d76d0363d7 100644
--- a/src/main/java/net/floodlightcontroller/packet/Data.java
+++ b/src/main/java/net/floodlightcontroller/packet/Data.java
@@ -60,7 +60,7 @@ public class Data extends BasePacket {
 
     @Override
     public IPacket deserialize(byte[] data, int offset, int length) {
-        this.data = Arrays.copyOfRange(data, offset, data.length);
+        this.data = Arrays.copyOfRange(data, offset, offset + length);
         return this;
     }
 
diff --git a/src/main/java/net/floodlightcontroller/packet/IPv4.java b/src/main/java/net/floodlightcontroller/packet/IPv4.java
index 01f886da252bd59fa641f56b4684a0ce28087fdb..81d2c6c079308d268b68bcda1f7707ed492160ad 100644
--- a/src/main/java/net/floodlightcontroller/packet/IPv4.java
+++ b/src/main/java/net/floodlightcontroller/packet/IPv4.java
@@ -371,10 +371,14 @@ public class IPv4 extends BasePacket {
         } else {
             payload = new Data();
         }
-        this.payload = payload.deserialize(data, bb.position(), bb.limit()-bb.position());
+        int payloadLength = this.totalLength - this.headerLength * 4;
+        int remLength = bb.limit()-bb.position();
+        if (remLength < payloadLength)
+            payloadLength = bb.limit()-bb.position();
+        this.payload = payload.deserialize(data, bb.position(), payloadLength);
         this.payload.setParent(this);
 
-        if (this.totalLength != length)
+        if (this.totalLength > length)
             this.isTruncated = true;
         else
             this.isTruncated = false;
diff --git a/src/main/java/net/floodlightcontroller/packet/TCP.java b/src/main/java/net/floodlightcontroller/packet/TCP.java
index 889e4c6a88292d3e2ea704aa3b49e38e93054fdd..06359e4af10df78ce891dc56bbeea316488b97e2 100644
--- a/src/main/java/net/floodlightcontroller/packet/TCP.java
+++ b/src/main/java/net/floodlightcontroller/packet/TCP.java
@@ -283,7 +283,8 @@ public class TCP extends BasePacket {
         }
         
         this.payload = new Data();
-        this.payload = payload.deserialize(data, bb.position(), bb.limit()-bb.position());
+        int remLength = bb.limit()-bb.position();
+        this.payload = payload.deserialize(data, bb.position(), remLength);
         this.payload.setParent(this);
         return this;
     }
diff --git a/src/test/java/net/floodlightcontroller/packet/IPv4Test.java b/src/test/java/net/floodlightcontroller/packet/IPv4Test.java
index 928a2de68fb551f5d27aafaaba7b0e00c4d92c55..acea62d25f195b35f012b8714e70983adf84e8af 100644
--- a/src/test/java/net/floodlightcontroller/packet/IPv4Test.java
+++ b/src/test/java/net/floodlightcontroller/packet/IPv4Test.java
@@ -69,19 +69,43 @@ public class IPv4Test {
         assertTrue(Arrays.equals(expected, actual));
     }
     
+    // A real TLSv1 packet
+    byte[] pktSerialized = 
+            new byte[] { 0x45, 0x00,
+                         0x00, 0x2e, 0x41, (byte) 0xbe, 0x40, 0x00, 0x40, 0x06,
+                         (byte) 0xd4, (byte) 0xf0, (byte) 0xc0, (byte) 0xa8, 
+                         0x02, (byte) 0xdb, (byte) 0xd0, 0x55,
+                         (byte) 0x90, 0x42, (byte) 0xd5, 0x48, 0x01, (byte) 
+                         0xbb, (byte) 0xe3, 0x50,
+                         (byte) 0xb2, 0x2f, (byte) 0xfc, (byte) 0xf8, 
+                         (byte) 0xa8, 0x2c, 0x50, 0x18,
+                         (byte) 0xff, (byte) 0xff, 0x24, 0x3c, 0x00, 
+                         0x00, 0x14, 0x03,
+                         0x01, 0x00, 0x01, 0x01
+    };
+    
     @Test
     public void testDeserialize() {
-        // A real TLSv1 packet
-        byte[] pktSerialized = new byte[] { 0x45, 0x00,
+        IPv4 packet = new IPv4();
+        packet.deserialize(pktSerialized, 0, pktSerialized.length);
+        byte[] pktSerialized1 = packet.serialize();
+        assertTrue(Arrays.equals(pktSerialized, pktSerialized1));
+    }
+
+    @Test
+    public void testDeserializePadded() {
+        // A real TLSv1 packet with crap added to the end
+        byte[] pktSerializedPadded = new byte[] { 0x45, 0x00,
                 0x00, 0x2e, 0x41, (byte) 0xbe, 0x40, 0x00, 0x40, 0x06,
                 (byte) 0xd4, (byte) 0xf0, (byte) 0xc0, (byte) 0xa8, 0x02, (byte) 0xdb, (byte) 0xd0, 0x55,
                 (byte) 0x90, 0x42, (byte) 0xd5, 0x48, 0x01, (byte) 0xbb, (byte) 0xe3, 0x50,
                 (byte) 0xb2, 0x2f, (byte) 0xfc, (byte) 0xf8, (byte) 0xa8, 0x2c, 0x50, 0x18,
                 (byte) 0xff, (byte) 0xff, 0x24, 0x3c, 0x00, 0x00, 0x14, 0x03,
-                0x01, 0x00, 0x01, 0x01
+                0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 
+                0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 
         };
         IPv4 packet = new IPv4();
-        packet.deserialize(pktSerialized, 0, pktSerialized.length);
+        packet.deserialize(pktSerializedPadded, 0, pktSerializedPadded.length);
         byte[] pktSerialized1 = packet.serialize();
         assertTrue(Arrays.equals(pktSerialized, pktSerialized1));
     }