Ignore:
Timestamp:
2023-10-05T15:52:30+02:00 (14 months ago)
Author:
taylor.smock
Message:

Fix AIOOBE when reading more data from a file than indicated by file header

Location:
applications/editors/josm/plugins/FIT/src/main/java/org/openstreetmap/josm/plugins/fit/lib
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/FIT/src/main/java/org/openstreetmap/josm/plugins/fit/lib/FitReader.java

    r36151 r36160  
    2727import org.openstreetmap.josm.plugins.fit.lib.records.internal.FitRecordNormalHeader;
    2828import org.openstreetmap.josm.plugins.fit.lib.records.internal.IField;
     29import org.openstreetmap.josm.plugins.fit.lib.utils.CountingInputStream;
    2930import org.openstreetmap.josm.plugins.fit.lib.utils.NumberUtils;
    3031
     
    4849     */
    4950    public static FitData[] read(InputStream inputStream, FitReaderOptions... options) throws FitException {
    50         final var bufferedInputStream = inputStream.markSupported() ? inputStream
    51                 : new BufferedInputStream(inputStream);
     51        final var bufferedInputStream = new CountingInputStream(
     52                inputStream.markSupported() ? inputStream : new BufferedInputStream(inputStream));
    5253        final var optionsSet = options.length == 0 ? EnumSet.noneOf(FitReaderOptions.class)
    5354                : EnumSet.of(options[0], options);
     
    5556        try {
    5657            final var header = readFitHeader(bufferedInputStream);
     58            final var headerSize = bufferedInputStream.bytesRead();
    5759            bufferedInputStream.mark(1);
    5860            var localMessageHeaders = new FitDefinitionMessage[1];
     
    6163            var offsetAddition = 0;
    6264            byte lastOffset = 0;
    63             while (bufferedInputStream.read() != -1) {
     65            while (bufferedInputStream.read() != -1
     66                    && bufferedInputStream.bytesRead() < header.dataSize() - headerSize) {
    6467                bufferedInputStream.reset();
    6568                final var nextRecordHeader = readNextRecordHeader(bufferedInputStream);
     
    125128        } catch (FitException fitException) {
    126129            handleException(optionsSet, fitException);
    127         } catch (IOException ioException) {
     130        } catch (IllegalArgumentException | IOException ioException) {
    128131            handleException(optionsSet, new FitException(ioException));
    129132        }
     
    199202        final var globalMessageNumber = NumberUtils.decodeInt(2, littleEndian, inputStream);
    200203        final int numberOfFields = inputStream.read();
    201         final var fitFields = new ArrayList<FitField>(numberOfFields);
     204        final var fitFields = new ArrayList<FitField>(Math.max(0, numberOfFields));
    202205        for (var i = 0; i < numberOfFields; i++) {
    203206            fitFields.add(readNextField(inputStream));
Note: See TracChangeset for help on using the changeset viewer.