HhH666-ux opened a new pull request, #16543:
URL: https://github.com/apache/dubbo/pull/16543

   ## What is the purpose of the change
   
   `Bytes.getMD5(InputStream)` returns a wrong MD5 whenever the stream length 
is not a multiple of the internal 8 KiB buffer, and returns the digest of an 
**empty** input for streams that report `available() == 0` while data is still 
readable. Fixes #16451.
   
   ## Brief changelog
   
   `getMD5(InputStream, int)` had two defects:
   
   1. The read loop filled the whole buffer and then called `md.update(buf)`, 
hashing the **entire** buffer instead of the `total` bytes actually read. The 
last chunk of any stream whose length is not a multiple of `bs` therefore 
hashed stale tail bytes from the previous chunk, producing a wrong digest. This 
is the normal case for `FileInputStream` on file systems that short-read 
(network mounts, encrypted volumes), which is exactly what `getMD5(File)` 
forwards to.
   2. The outer loop was gated on `while (is.available() > 0)`. `available()` 
reports the bytes readable *without blocking*, not the remaining length, so a 
stream that momentarily reports `0` (pipelined/socket-backed/deflated streams) 
terminated the loop before any byte was consumed.
   
   Both are fixed by the canonical digest loop:
   
   ```java
   for (int read = is.read(buf); read != -1; read = is.read(buf)) {
       md.update(buf, 0, read);
   }
   ```
   
   The buffer-filling inner loop becomes unnecessary once the digest consumes 
exactly the bytes read.
   
   ## Verifying this change
   
   Added two regressions to `BytesTest`, both comparing against 
`getMD5(byte[])`:
   
   - `testMD5ForStreamWithShortFinalRead`: a random `8192 + 17` byte stream 
(short final chunk).
   - `testMD5ForStreamWithZeroAvailable`: a stream whose `available()` always 
returns `0` but whose `read()` serves data.
   
   Both fail on the previous implementation (verified locally) and pass with 
this change. `mvn -f dubbo-common/pom.xml test -Dtest=BytesTest` and 
`spotless:check` pass.
   
   Follow this checklist to request gating of a pull request:
   - [x] Make sure there is a 
[GitHub_issue](https://github.com/apache/dubbo/issues) field for the change 
(usually above).
   - [x] Write a pull request description that is detailed enough to understand 
what the pull request does, how, and why.
   - [x] Write necessary unit-test to verify your logic correction. More 
hard-to-mock scenarios are considered acceptable: verify them via integration 
tests or e2e tests.
   - [ ] `./mvnw -T 1C clean install -DskipTests=false 
-DskipIntegrationTests=false` passes locally — not run locally in full; focused 
module tests and spotless pass (module-level `mvn -f dubbo-common/pom.xml test` 
and `spotless:check`).
   - [x] If this contribution is large, please file an [Apache Individual 
Contributor License Agreement](http://www.apache.org/licenses/icla.pdf) — 
change is small.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to