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

CodecOutputList does not implement java.util.list.add(int index, E element) correctly #7938

Closed
jozefbalga opened this issue May 15, 2018 · 3 comments
Assignees
Milestone

Comments

@jozefbalga
Copy link

Expected behavior

When an element is added to the beginning of CodecOutputList, the remaining elements should be shifted to the right, see https://docs.oracle.com/javase/8/docs/api/java/util/List.html#add-int-E-

Actual behavior

The new element is added to the correct position, but the remaining elements are modified.

Steps to reproduce

Create a new CodecOutputList, add integer 1, then add interger 0 to the position 0. The element at position 1 is null

Minimal yet complete reproducer code (or URL to code)

package io.netty.handler.codec;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import io.netty.handler.codec.CodecOutputList;

public class CodecOuputListTest {

@Test
public void testArrayListAdd() {
	List<Object> arrayList = new ArrayList<>();
	arrayList.add(1);
	arrayList.add(0, 0);
	Assert.assertEquals(2, arrayList.size());
	Assert.assertEquals(0, arrayList.get(0));
	Assert.assertEquals(1, arrayList.get(1));
}

@Test
public void testCodecOutputListAdd() {
	List<Object> codecOutputList = CodecOutputList.newInstance();
	codecOutputList.add(1);
	codecOutputList.add(0, 0);
	Assert.assertEquals(2, codecOutputList.size());
	Assert.assertEquals(0, codecOutputList.get(0));
	Assert.assertNotNull(codecOutputList.get(1));
	Assert.assertEquals(1, codecOutputList.get(1));
}

}

Netty version

4.1.25

JVM version (e.g. java -version)

1.8.0_152

OS version (e.g. uname -a)

Linux wks-balga 4.14.35 #1 SMP Mon Apr 23 06:09:12 UTC 2018 x86_64 Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz GenuineIntel GNU/Linux

@normanmaurer
Copy link
Member

@jozefbalga thanks.... working on a fix.

normanmaurer added a commit that referenced this issue May 15, 2018
…ent) is called.

Motivation:

We did not correctly copy elements in some cases when add(index, element) was used.

Modifications:

- Correctly detect when copy is neede and when not.
- Add test case.

Result:

Fixes #7938.
@normanmaurer
Copy link
Member

@jozefbalga PTAL #7939

@normanmaurer
Copy link
Member

Fixed by #7939

@normanmaurer normanmaurer added this to the 4.1.26.Final milestone May 15, 2018
@normanmaurer normanmaurer self-assigned this May 15, 2018
normanmaurer added a commit that referenced this issue May 15, 2018
…ent) is called. (#7939)

Motivation:

We did not correctly copy elements in some cases when add(index, element) was used.

Modifications:

- Correctly detect when copy is neede and when not.
- Add test case.

Result:

Fixes #7938.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants