Skip to content

Commit

Permalink
Correctly copy existing elements when CodecOutputList.add(index, elem…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
normanmaurer committed May 15, 2018
1 parent 3d2e231 commit 3ca301f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void add(int index, Object element) {
expandArray();
}

if (index != size - 1) {
if (index != size) {
System.arraycopy(array, index, array, index + 1, size - index);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2018 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec;

import org.junit.Test;


import static org.junit.Assert.*;

public class CodecOutputListTest {

@Test
public void testCodecOutputListAdd() {
CodecOutputList codecOutputList = CodecOutputList.newInstance();
try {
assertEquals(0, codecOutputList.size());
assertTrue(codecOutputList.isEmpty());

codecOutputList.add(1);
assertEquals(1, codecOutputList.size());
assertFalse(codecOutputList.isEmpty());
assertEquals(1, codecOutputList.get(0));

codecOutputList.add(0, 0);
assertEquals(2, codecOutputList.size());
assertFalse(codecOutputList.isEmpty());
assertEquals(0, codecOutputList.get(0));
assertEquals(1, codecOutputList.get(1));

codecOutputList.add(1, 2);
assertEquals(3, codecOutputList.size());
assertFalse(codecOutputList.isEmpty());
assertEquals(0, codecOutputList.get(0));
assertEquals(2, codecOutputList.get(1));
assertEquals(1, codecOutputList.get(2));
} finally {
codecOutputList.recycle();
}
}
}

0 comments on commit 3ca301f

Please sign in to comment.