Repository navigation
ZipEndOfCentralDirectoryRecord finds the record signature inside the zip comment - #52255
Conversation
|
Thanks, @zhoubiao188, for all your recent contributions. We only have a finite amount of time to work on the project. When it comes to bugs, that time is best spent identifying and fixing those that will bring the most benefit. The greater the severity of a bug or the more people it affects, the greater the benefit of fixing it. Are you finding these problems based on your own real-word usage of Spring Boot or are you asking an AI to find bugs and help you fix them? The chances of a real-world zip file having a comment that contains |
| while (offset >= 0) { | ||
| if (isValidLocation(dataBlock, buffer, startPos, offset)) { | ||
| debug.log("Found EndOfCentralDirectoryRecord at %s + %s", startPos, offset); | ||
| buffer.position(offset + Integer.BYTES); |
There was a problem hiding this comment.
Why is the buffer's position now being set? It appears to be unnecessary and the tests all pass without it. If it is necessary, a test should be added that fails when it isn't set.
There was a problem hiding this comment.
You are right, it is not necessary. findInBuffer positions the buffer and then reads the signature with a relative getInt(), so when it returns the buffer is already positioned just after the signature bytes. Reading the fields in load relies on exactly that, as the original code did, so nothing has to be repositioned before returning. I have removed the redundant call.
…e comment The 'end of central directory record' is located by scanning backwards through the data for the record signature. The signature bytes are not unique, they can also occur in other places, in particular in the file comment that follows the record. When that happens the scan finds the comment occurrence first (it is closer to the end) and the record is read from the wrong position: java.nio.BufferUnderflowException A candidate is now only accepted when the whole record is available and the comment that it declares doesn't extend beyond the end of the data. Candidates that fail the check are skipped and the scan continues, so the actual record is found. The scan leaves the buffer positioned after the signature bytes that it matched, so the fields of the record can be read straight away without repositioning the buffer. The loadLocatesAndLoadsData and asByteArrayReturnsByteArray test data declared a 7 byte comment without providing the comment bytes, so they are not valid records. They now include the comment that they declare. Signed-off-by: zhoubiao188 <32508238+zhoubiao188@users.noreply.github.com> See spring-projectsgh-52255
…es in the comment" See spring-projectsgh-52255 Signed-off-by: Andy Wilkinson <andy.wilkinson@broadcom.com>
5e3cf85 to
c15691d
Compare
The 'end of central directory record' is located by scanning backwards through the data for the record signature. The signature bytes are not unique, they can also occur in other places, in particular in the file comment that follows the record. When that happens the scan finds the comment occurrence first (it is closer to the end) and the record is read from the wrong position: java.nio.BufferUnderflowException A candidate is now only accepted when the whole record is available and the comment that it declares doesn't extend beyond the end of the data. Candidates that fail the check are skipped and the scan continues, so the actual record is found. The scan leaves the buffer positioned after the signature bytes that it matched, so the fields of the record can be read straight away without repositioning the buffer. The loadLocatesAndLoadsData and asByteArrayReturnsByteArray test data declared a 7 byte comment without providing the comment bytes, so they are not valid records. They now include the comment that they declare. Signed-off-by: zhoubiao188 <32508238+zhoubiao188@users.noreply.github.com> See spring-projectsgh-52255
See spring-projectsgh-52255 Signed-off-by: Andy Wilkinson <andy.wilkinson@broadcom.com>
c15691d to
e7ac487
Compare
The end of central directory record is located by scanning backwards through the data for the record signature:
The signature bytes are not unique. They can also occur in the file comment, which follows the record and is therefore closer to the end of the file, so the scan finds the comment occurrence first and reads the record from the wrong position:
A candidate is now only accepted when the whole record is available in the buffer and the comment that it declares does not extend beyond the end of the data. Candidates that fail the check are skipped and the scan continues, so the actual record is found. The same rule is used by the JDK
ZipFile, which also rejects a signature match when the comment length is not consistent with the file size.The
loadLocatesAndLoadsDataandasByteArrayReturnsByteArraytest data declared a 7 byte comment but did not provide the comment bytes, so they are not valid records. They now include the comment that they declare.