Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultDataBuffer fails to transform its content to a string #31873

Closed
access-control-rtfm opened this issue Dec 20, 2023 · 2 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@access-control-rtfm
Copy link

access-control-rtfm commented Dec 20, 2023

Affects: 6.*.*


The following test fails:

import static org.junit.jupiter.api.Assertions.*;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;

class DefaultDataBufferStringTransformationTest {

    @Test
    void test() {
        Charset charset = StandardCharsets.UTF_8;
        DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer(DefaultDataBufferFactory.DEFAULT_INITIAL_CAPACITY);
        String name = "Müller";
        int repeatCount = 19;
        for(int i = 0; i < repeatCount; i++) {
            dataBuffer.write(name, charset);
        }
        String expected = name.repeat(repeatCount);
        String result = dataBuffer.toString(charset);
        assertEquals(expected, result);
    }

}

May be it has something to do with this issue.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 20, 2023
@access-control-rtfm access-control-rtfm changed the title DefaultDataBuffer issue to transform its content to a string. DefaultDataBuffer fails to transform its content to a string. Dec 20, 2023
@sdeleuze
Copy link
Contributor

I can indeed reproduce:

Expected :MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
Actual   :MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerler

@poutsma Maybe you want to tackle this one since it could be indeed potentially related to #30966.

@poutsma poutsma self-assigned this Dec 21, 2023
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Dec 21, 2023
@sbrannen
Copy link
Member

sbrannen commented Dec 21, 2023

Out of curiosity, I took a glance at this yesterday, and I think the issue might be in DataBuffer#write(CharSequence, Charset) instead of in DefaultDataBuffer.

If you add System.out.println(toString(StandardCharsets.UTF_8)); to the write() method immediately after writePosition(dest.position());, you'll see the following.

Müller
MüllerMüller
MüllerMüllerMüller
MüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMül
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüller
MüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerMüllerler

The error occurs when the current ByteBuffer does not contain enough remaining space to hold the content of the supplied CharSequence and the DataBuffer's capacity has to be increased.

In the last three lines of the above output you can see that first Mül is written, then Mül is gone, and then ler is written. So the initial Mül is effectively lost.

This occurs when adding the encoded CharSet content of length L to the current buffer with size B exceeds the initial capacity C divided by 2.

So, when B + L > C/2.

C = DefaultDataBufferFactory.DEFAULT_INITIAL_CAPACITY = 256.

C / 2 = 128

For the 18th iteration: B + L = 7 * 18 = 126 (7 because ü takes up two positions when encoded), and 127 < 128. So no error occurs.

For the 19th iteration: B + L = 7 * 19 = 133, and 133 > 128, and content is lost as described above.

@poutsma poutsma added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 8, 2024
@poutsma poutsma added this to the 6.1.3 milestone Jan 8, 2024
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-6.0.x labels Jan 8, 2024
@poutsma poutsma changed the title DefaultDataBuffer fails to transform its content to a string. DefaultDataBuffer fails to transform its content to a string Jan 9, 2024
@poutsma poutsma closed this as completed in 3452354 Jan 9, 2024
poutsma added a commit that referenced this issue Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants