diff --git a/edexOsgi/com.raytheon.edex.plugin.binlightning/src/com/raytheon/edex/plugin/binlightning/total/ChecksumByteBuffer.java b/edexOsgi/com.raytheon.edex.plugin.binlightning/src/com/raytheon/edex/plugin/binlightning/total/ChecksumByteBuffer.java index 9e3354d200..65302c0bbc 100644 --- a/edexOsgi/com.raytheon.edex.plugin.binlightning/src/com/raytheon/edex/plugin/binlightning/total/ChecksumByteBuffer.java +++ b/edexOsgi/com.raytheon.edex.plugin.binlightning/src/com/raytheon/edex/plugin/binlightning/total/ChecksumByteBuffer.java @@ -34,6 +34,7 @@ import com.raytheon.uf.common.numeric.UnsignedNumbers; * ------------ ---------- ----------- -------------------------- * Jun 03, 2014 3226 bclement Initial creation * Jun 09, 2014 3226 bclement Added ByteBuffer constructor + * Jul 01, 2015 4581 skorolev Added condition in the getSum(int) * * * @@ -54,7 +55,6 @@ public class ChecksumByteBuffer { private long packetSum; - /** * @see ByteBuffer#wrap(byte[]) * @param data @@ -80,6 +80,11 @@ public class ChecksumByteBuffer { int start = buff.position(); int end = start + numberOfBytes; long rval = 0; + if (buff.remaining() < numberOfBytes) { + throw new IllegalArgumentException("Unable to get checksum for " + + numberOfBytes + " bytes, only " + buff.remaining() + + " bytes in buffer."); + } for (int i = start; i < end; ++i) { rval += UnsignedNumbers.ubyteToShort(buff.get(i)); } diff --git a/edexOsgi/com.raytheon.edex.plugin.binlightning/src/com/raytheon/edex/plugin/binlightning/total/TotalLightningDecoder.java b/edexOsgi/com.raytheon.edex.plugin.binlightning/src/com/raytheon/edex/plugin/binlightning/total/TotalLightningDecoder.java index b89c47d527..47e4c29c70 100644 --- a/edexOsgi/com.raytheon.edex.plugin.binlightning/src/com/raytheon/edex/plugin/binlightning/total/TotalLightningDecoder.java +++ b/edexOsgi/com.raytheon.edex.plugin.binlightning/src/com/raytheon/edex/plugin/binlightning/total/TotalLightningDecoder.java @@ -60,6 +60,7 @@ import com.raytheon.uf.common.wmo.WMOTimeParser; * Jun 09, 2014 3226 bclement added encryption support * Jun 10, 2014 3226 bclement added filter support * Jun 19, 2014 3226 bclement added validator callback + * Jul 07, 2015 4581 skorolev Corrected decodeStrikes to avoid BufferUnderflowException. * * * @@ -110,6 +111,8 @@ public class TotalLightningDecoder { } }; + private static final int SHORT_SIZE = 2; + /** * Parse total lightning data into BinLightningRecords * @@ -186,7 +189,6 @@ public class TotalLightningDecoder { + ", WMO Header: " + wmoHdr, e); } - /** * @param wmoHdr * @param fileName @@ -196,8 +198,7 @@ public class TotalLightningDecoder { * @throws DecoderException */ private PluginDataObject[] decodeInternal(WMOHeader wmoHdr, - String fileName, byte[] pdata) - throws DecoderException { + String fileName, byte[] pdata) throws DecoderException { if (!validFlashPacket(pdata, COMBINATION_PACKET_HEADER_SIZE)) { /* assume data is encrypted if we can't understand it */ pdata = decrypt(wmoHdr, fileName, pdata); @@ -212,7 +213,6 @@ public class TotalLightningDecoder { } } - /** * @param wmoHdr * @param fileName @@ -248,16 +248,22 @@ public class TotalLightningDecoder { ChecksumByteBuffer buff = new ChecksumByteBuffer(pdata); while (buff.position() < buff.size()) { int startingPostion = buff.position(); - int totalBytes = UnsignedNumbers.ushortToInt(buff.getShort()); - if (totalBytes > (buff.size() - startingPostion)) { - if (validFlashPacket(pdata, buff.position())) { - log.error("Truncated total lightning packet in file: " - + fileName); - } else { - int extra = buff.size() - startingPostion; - log.warn("Extra data at end of lightning packets: " + extra - + " bytes"); + int totalBytes = 0; + if ((buff.size() - startingPostion) >= SHORT_SIZE) { + totalBytes = UnsignedNumbers.ushortToInt(buff.getShort()); + if (totalBytes > (buff.size() - startingPostion)) { + if (validFlashPacket(pdata, buff.position())) { + log.error("Truncated total lightning packet in file: " + + fileName); + } else { + int extra = buff.size() - startingPostion; + log.warn("Extra data at end of lightning packets: " + + extra + " bytes"); + } + break; } + } else { + log.warn("Extra data at end of lightning packets: 1 byte"); break; } /* start flash packet */ @@ -364,8 +370,8 @@ public class TotalLightningDecoder { boolean rval = mungedSum == expected; if (!rval) { - log.debug("Checksum failed: expected " + expected - + " got " + mungedSum); + log.debug("Checksum failed: expected " + expected + " got " + + mungedSum); } if (total) { buff.resetAllSums();