You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2019/11/18 14:53:33 UTC
[dubbo] branch master updated: [Dubbo-5215] get & set & remove in
org.apache.dubbo.common.utils.Stack take no consideration that index plus
mSize is negative (#5231)
This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 6898691 [Dubbo-5215] get & set & remove in org.apache.dubbo.common.utils.Stack take no consideration that index plus mSize is negative (#5231)
6898691 is described below
commit 6898691b99b7d3c2be82fc506856835fcc4b2ce8
Author: lkj41110 <80...@qq.com>
AuthorDate: Mon Nov 18 22:53:04 2019 +0800
[Dubbo-5215] get & set & remove in org.apache.dubbo.common.utils.Stack take no consideration that index plus mSize is negative (#5231)
fix #5215
---
.../java/org/apache/dubbo/common/utils/Stack.java | 284 ++++++++++-----------
.../org/apache/dubbo/common/utils/StackTest.java | 29 +++
2 files changed, 171 insertions(+), 142 deletions(-)
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/Stack.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/Stack.java
index 1283d83..b7ec70c 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/Stack.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/Stack.java
@@ -1,143 +1,143 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF 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 org.apache.dubbo.common.utils;
-
-import java.util.ArrayList;
-import java.util.EmptyStackException;
-import java.util.List;
-
-/**
- * Stack.
- */
-
-public class Stack<E> {
- private int mSize = 0;
-
- private List<E> mElements = new ArrayList<E>();
-
- public Stack() {
- }
-
- /**
- * push.
- *
- * @param ele
- */
- public void push(E ele) {
- if (mElements.size() > mSize) {
- mElements.set(mSize, ele);
- } else {
- mElements.add(ele);
- }
- mSize++;
- }
-
- /**
- * pop.
- *
- * @return the last element.
- */
- public E pop() {
- if (mSize == 0) {
- throw new EmptyStackException();
- }
- return mElements.set(--mSize, null);
- }
-
- /**
- * peek.
- *
- * @return the last element.
- */
- public E peek() {
- if (mSize == 0) {
- throw new EmptyStackException();
- }
- return mElements.get(mSize - 1);
- }
-
- /**
- * get.
- *
- * @param index index.
- * @return element.
- */
- public E get(int index) {
- if (index >= mSize) {
- throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + mSize);
- }
-
- return index < 0 ? mElements.get(index + mSize) : mElements.get(index);
- }
-
- /**
- * set.
- *
- * @param index index.
- * @param value element.
- * @return old element.
- */
- public E set(int index, E value) {
- if (index >= mSize) {
- throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + mSize);
- }
-
- return mElements.set(index < 0 ? index + mSize : index, value);
- }
-
- /**
- * remove.
- *
- * @param index
- * @return element
- */
- public E remove(int index) {
- if (index >= mSize) {
- throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + mSize);
- }
-
- E ret = mElements.remove(index < 0 ? index + mSize : index);
- mSize--;
- return ret;
- }
-
- /**
- * get stack size.
- *
- * @return size.
- */
- public int size() {
- return mSize;
- }
-
- /**
- * is empty.
- *
- * @return empty or not.
- */
- public boolean isEmpty() {
- return mSize == 0;
- }
-
- /**
- * clear stack.
- */
- public void clear() {
- mSize = 0;
- mElements.clear();
- }
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.dubbo.common.utils;
+
+import java.util.ArrayList;
+import java.util.EmptyStackException;
+import java.util.List;
+
+/**
+ * Stack.
+ */
+
+public class Stack<E> {
+ private int mSize = 0;
+
+ private List<E> mElements = new ArrayList<E>();
+
+ public Stack() {
+ }
+
+ /**
+ * push.
+ *
+ * @param ele
+ */
+ public void push(E ele) {
+ if (mElements.size() > mSize) {
+ mElements.set(mSize, ele);
+ } else {
+ mElements.add(ele);
+ }
+ mSize++;
+ }
+
+ /**
+ * pop.
+ *
+ * @return the last element.
+ */
+ public E pop() {
+ if (mSize == 0) {
+ throw new EmptyStackException();
+ }
+ return mElements.set(--mSize, null);
+ }
+
+ /**
+ * peek.
+ *
+ * @return the last element.
+ */
+ public E peek() {
+ if (mSize == 0) {
+ throw new EmptyStackException();
+ }
+ return mElements.get(mSize - 1);
+ }
+
+ /**
+ * get.
+ *
+ * @param index index.
+ * @return element.
+ */
+ public E get(int index) {
+ if (index >= mSize || index + mSize < 0) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + mSize);
+ }
+
+ return index < 0 ? mElements.get(index + mSize) : mElements.get(index);
+ }
+
+ /**
+ * set.
+ *
+ * @param index index.
+ * @param value element.
+ * @return old element.
+ */
+ public E set(int index, E value) {
+ if (index >= mSize || index + mSize < 0) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + mSize);
+ }
+
+ return mElements.set(index < 0 ? index + mSize : index, value);
+ }
+
+ /**
+ * remove.
+ *
+ * @param index
+ * @return element
+ */
+ public E remove(int index) {
+ if (index >= mSize || index + mSize < 0) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + mSize);
+ }
+
+ E ret = mElements.remove(index < 0 ? index + mSize : index);
+ mSize--;
+ return ret;
+ }
+
+ /**
+ * get stack size.
+ *
+ * @return size.
+ */
+ public int size() {
+ return mSize;
+ }
+
+ /**
+ * is empty.
+ *
+ * @return empty or not.
+ */
+ public boolean isEmpty() {
+ return mSize == 0;
+ }
+
+ /**
+ * clear stack.
+ */
+ public void clear() {
+ mSize = 0;
+ mElements.clear();
+ }
}
\ No newline at end of file
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StackTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StackTest.java
index 4b113e8..6a32f22 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StackTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StackTest.java
@@ -83,6 +83,17 @@ public class StackTest {
}
@Test
+ public void testIllegalGetNegative() throws Exception {
+ Stack<String> stack = new Stack<String>();
+ stack.push("one");
+ stack.get(-1);
+
+ Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
+ stack.get(-10);
+ });
+ }
+
+ @Test
public void testIllegalSet() throws Exception {
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
Stack<String> stack = new Stack<String>();
@@ -91,10 +102,28 @@ public class StackTest {
}
@Test
+ public void testIllegalSetNegative() throws Exception {
+ Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
+ Stack<String> stack = new Stack<String>();
+ stack.set(-1, "illegal");
+ });
+ }
+
+ @Test
public void testIllegalRemove() throws Exception {
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
Stack<String> stack = new Stack<String>();
stack.remove(1);
});
}
+
+ @Test
+ public void testIllegalRemoveNegative() throws Exception {
+ Stack<String> stack = new Stack<String>();
+ stack.push("one");
+
+ Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
+ stack.remove(-2);
+ });
+ }
}