From 8f130316d20cea32631f55a0ac4c69ffb4bd7d16 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 15 Sep 2023 13:46:04 +0200 Subject: [PATCH] MultipartParser should respect read position This commit ensures that the MultipartParser takes a buffer's read position into account. Closes gh-31110 --- .../http/codec/multipart/MultipartParser.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartParser.java b/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartParser.java index 0def952d78e..1b59f6ddf1e 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartParser.java +++ b/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartParser.java @@ -523,7 +523,7 @@ final class MultipartParser extends BaseSubscriber { if (logger.isTraceEnabled()) { logger.trace("Boundary found @" + endIdx + " in " + buffer); } - int len = endIdx - this.boundaryLength + 1; + int len = endIdx - this.boundaryLength + 1 - boundaryBuffer.readPosition(); if (len > 0) { // whole boundary in buffer. // slice off the body part, and flush @@ -538,10 +538,11 @@ final class MultipartParser extends BaseSubscriber { DataBufferUtils.release(boundaryBuffer); DataBuffer prev; while ((prev = this.queue.pollLast()) != null) { - int prevLen = prev.readableByteCount() + len; + int prevByteCount = prev.readableByteCount(); + int prevLen = prevByteCount + len; if (prevLen > 0) { // slice body part of previous buffer, and flush it - DataBuffer body = prev.split(prevLen); + DataBuffer body = prev.split(prevLen + prev.readPosition()); DataBufferUtils.release(prev); enqueue(body); flush(); @@ -550,7 +551,7 @@ final class MultipartParser extends BaseSubscriber { else { // previous buffer only contains boundary bytes DataBufferUtils.release(prev); - len += prev.readableByteCount(); + len += prevByteCount; } } }