Fix potential ByteBuf leaks in bgp-l3vpn 50/96950/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 20 Jul 2021 15:35:32 +0000 (17:35 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 20 Jul 2021 15:36:50 +0000 (17:36 +0200)
We are using ByteBuf.readBytes() to skip over some content and also
to frame it for parsing. Use ByteBuf.skipBytes() in the case of former
and ByteBuf.readSlice() in case of latter. This speeds things up and
plugs a potential refcount problem.

JIRA: BGPCEP-973
Change-Id: Ib9bf406a5e9de9b5e122c3fb3323a4b772a1a798
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/extensions/l3vpn/src/main/java/org/opendaylight/protocol/bgp/l3vpn/unicast/AbstractVpnNextHopParserSerializer.java

index 9c534ebff7174466c02b60f9ef38e6c8a9eb8c86..3e6545c45c0d201e9e4a5e649aaf98a59623b709 100644 (file)
@@ -7,7 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.l3vpn.unicast;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.bgp.concepts.NextHopUtil;
 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
@@ -26,17 +27,17 @@ public abstract class AbstractVpnNextHopParserSerializer implements NextHopParse
 
     @Override
     public CNextHop parseNextHop(final ByteBuf buffer) throws BGPParsingException {
-        Preconditions.checkArgument(buffer.readableBytes() == (this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH),
+        checkArgument(buffer.readableBytes() == this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH,
                 "Length of byte array for NEXT_HOP should be %s, but is %s",
                 this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH, buffer.readableBytes());
-        buffer.readBytes(RouteDistinguisherUtil.RD_LENGTH);
-        return NextHopUtil.parseNextHop(buffer.readBytes(this.ipAddrLength));
+        buffer.skipBytes(RouteDistinguisherUtil.RD_LENGTH);
+        return NextHopUtil.parseNextHop(buffer.readSlice(this.ipAddrLength));
     }
 
     @Override
     public void serializeNextHop(final CNextHop nextHop, final ByteBuf byteAggregator) {
-        Preconditions.checkArgument(this.ipNextHopCaseClazz.isInstance(nextHop),
-                "cNextHop is not a VPN %s NextHop object.", this.ipNextHopCaseClazz.getSimpleName());
+        checkArgument(this.ipNextHopCaseClazz.isInstance(nextHop), "cNextHop is not a VPN %s NextHop object.",
+            this.ipNextHopCaseClazz.getSimpleName());
         byteAggregator.writeZero(RouteDistinguisherUtil.RD_LENGTH);
         NextHopUtil.serializeNextHop(nextHop, byteAggregator);
     }