Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(Object obj) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(obj);
return this;
}
Expand All @@ -105,6 +106,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(String str) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(str);
return this;
}
Expand All @@ -119,6 +121,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(StringBuffer sb) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(sb);
return this;
}
Expand All @@ -133,6 +136,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(CharSequence s) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(s);
return this;
}
Expand All @@ -147,6 +151,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(CharSequence s, int start, int end) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(s.subSequence(start, end));
return this;
}
Expand All @@ -161,6 +166,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(char[] str) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(str);
return this;
}
Expand All @@ -175,6 +181,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(char[] str, int offset, int len) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(Arrays.copyOfRange(str, offset, len));
return this;
}
Expand All @@ -189,6 +196,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(boolean b) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(b);
return this;
}
Expand All @@ -203,6 +211,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(char c) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(c);
return this;
}
Expand All @@ -217,6 +226,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(int i) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(i);
return this;
}
Expand All @@ -231,6 +241,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(long lng) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(lng);
return this;
}
Expand All @@ -245,6 +256,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(float f) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(f);
return this;
}
Expand All @@ -259,6 +271,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder a(double d) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(d);
return this;
}
Expand All @@ -273,6 +286,7 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
/** {@inheritDoc} */
@Override public GridStringBuilder appendCodePoint(int codePoint) {
if (lenLimit.overflowed(this)) {
initTailIfAbsent();
tail.append(codePoint);
return this;
}
Expand Down Expand Up @@ -311,4 +325,10 @@ private GridStringBuilder onWrite(int lenBeforeWrite) {
public boolean isOverflowed() {
return lenLimit.overflowed(this);
}

/** */
private void initTailIfAbsent() {
if (tail == null)
tail = lenLimit.getTail();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.ignite.internal.util.tostring;

import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.apache.ignite.testframework.junits.common.GridCommonTest;
import org.junit.Test;

/** Test suite to ensure SBLimitedLength works by design */
@GridCommonTest(group = "Utils")
public class SBLimitedLengthSelfTest extends GridCommonAbstractTest {
/** Ensure all append operations are working fine */
@Test
public void testAppend() {
SBLimitedLength strBuilder = stringBuilder(5);
strBuilder.a(1);
assertEquals("1", strBuilder.toString());
strBuilder.a(2L);
assertEquals("12", strBuilder.toString());
strBuilder.a(3f);
assertEquals("123.0", strBuilder.toString());
strBuilder.a(4d);
assertEquals("123.04.0", strBuilder.toString());
strBuilder.a('5');
assertEquals("123.04.05", strBuilder.toString());
strBuilder.a(true);
assertEquals("123.04.05true", strBuilder.toString());
Object obj = "6";
strBuilder.a(obj);
assertEquals("123.04.05true6", strBuilder.toString());
strBuilder.a("7");
assertEquals("123.04.05true67", strBuilder.toString());
strBuilder.a(new StringBuilder().append("8"));
assertEquals("123.04.05true678", strBuilder.toString());
CharSequence charSeq = "9";
strBuilder.a(charSeq);
assertEquals("123.04.05true6789", strBuilder.toString());
strBuilder.a(charSeq, 0, 1);
assertEquals("123.04.05true67899", strBuilder.toString());
}

/** */
@Test
public void testDoesNotThrowNPEOnHeadOverflow() {
SBLimitedLength sbLimitedLength = new SBLimitedLength(256);
sbLimitedLength.initLimit(new SBLengthLimit());
sbLimitedLength.a("a".repeat(7999));
sbLimitedLength.i(7000, "asd");
sbLimitedLength.a("a".repeat(10));
String result = sbLimitedLength.toString();
assertNotNull(result);
assertFalse(result.isEmpty());
assertTrue(result.contains("asd"));
}

/**
* Test that simulates the NPE scenario from handleRecursion.
* When tail is null but overflowed() returns true, append operations should not throw NPE.
*/
@Test
public void testNPEProtectionWithNullTail() {
SBLimitedLength sb = new SBLimitedLength(256);
sb.initLimit(new SBLengthLimit());
sb.a("a".repeat(8000));
sb.i(0, "@0");
sb.a("x");
}

/** Ensure toString works as expected */
@Test
public void testToString() {
SBLimitedLength strBuilder = stringBuilder(2);
strBuilder.a("ab");
assertEquals("ab", strBuilder.toString());
strBuilder.a("cd");
assertEquals("abcd", strBuilder.toString());
}

/**
* Get {@link SBLimitedLength} instance with specific head and tail length
* to simplify test cases
* @param headLength Head length.
*/
private SBLimitedLength stringBuilder(int headLength) {
SBLimitedLength sbLimitedLength = new SBLimitedLength(0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SBLimitedLength sbLimitedLength = new SBLimitedLength(0);
SBLimitedLength sb = new SBLimitedLength(0);


sbLimitedLength.initLimit(new SBLengthLimit() {
@Override boolean overflowed(SBLimitedLength sb) {
return sb.impl().length() > headLength;
}
});

return sbLimitedLength;
Comment thread
EgorBaranovEnjoysTyping marked this conversation as resolved.
}
}



Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.ignite.internal.util.tostring.GridToStringBuilderSelfTest;
import org.apache.ignite.internal.util.tostring.IncludeSensitiveAtomicTest;
import org.apache.ignite.internal.util.tostring.IncludeSensitiveTransactionalTest;
import org.apache.ignite.internal.util.tostring.SBLimitedLengthSelfTest;
import org.apache.ignite.internal.util.tostring.TransactionSensitiveDataTest;
import org.apache.ignite.lang.GridByteArrayListSelfTest;
import org.apache.ignite.spi.discovery.ClusterMetricsSelfTest;
Expand Down Expand Up @@ -92,6 +93,7 @@
GridStringBuilderFactorySelfTest.class,
GridToStringBuilderSelfTest.class,
CircularStringBuilderSelfTest.class,
SBLimitedLengthSelfTest.class,
GridByteArrayListSelfTest.class,
GridMBeanSelfTest.class,
GridMBeanDisableSelfTest.class,
Expand Down