@@ -538,6 +538,9 @@ private Map<ZipEntry, NameAndComment> populateFromCentralDirectory()
538538 ze .setExternalAttributes (ZipLong .getValue (CFH_BUF , off ));
539539 off += WORD ;
540540
541+ if (archive .length () - archive .getFilePointer () < fileNameLen ) {
542+ throw new EOFException ();
543+ }
541544 final byte [] fileName = new byte [fileNameLen ];
542545 archive .readFully (fileName );
543546 ze .setName (entryEncoding .decode (fileName ), fileName );
@@ -547,12 +550,18 @@ private Map<ZipEntry, NameAndComment> populateFromCentralDirectory()
547550 // data offset will be filled later
548551 entries .add (ze );
549552
553+ if (archive .length () - archive .getFilePointer () < extraLen ) {
554+ throw new EOFException ();
555+ }
550556 final byte [] cdExtraData = new byte [extraLen ];
551557 archive .readFully (cdExtraData );
552558 ze .setCentralDirectoryExtra (cdExtraData );
553559
554560 setSizesAndOffsetFromZip64Extra (ze , offset , diskStart );
555561
562+ if (archive .length () - archive .getFilePointer () < commentLen ) {
563+ throw new EOFException ();
564+ }
556565 final byte [] comment = new byte [commentLen ];
557566 archive .readFully (comment );
558567 ze .setComment (entryEncoding .decode (comment ));
@@ -878,9 +887,18 @@ private void resolveLocalFileHeaderData(final Map<ZipEntry, NameAndComment>
878887 }
879888 lenToSkip -= skipped ;
880889 }
890+ if (archive .length () - archive .getFilePointer () < extraFieldLen ) {
891+ throw new EOFException ();
892+ }
881893 final byte [] localExtraData = new byte [extraFieldLen ];
882894 archive .readFully (localExtraData );
883- ze .setExtra (localExtraData );
895+ try {
896+ ze .setExtra (localExtraData );
897+ } catch (RuntimeException ex ) {
898+ final ZipException z = new ZipException ("Invalid extra data in entry " + ze .getName ());
899+ z .initCause (ex );
900+ throw z ;
901+ }
884902 offsetEntry .dataOffset = offset + LFH_OFFSET_FOR_FILENAME_LENGTH
885903 + SHORT + SHORT + fileNameLen + extraFieldLen ;
886904
0 commit comments