Browse Source

SONAR-10943 task failing with char 0 in message finishes normally

even on Postgres
tags/7.5
Sébastien Lesaint 6 years ago
parent
commit
087f4fc5ea

+ 24
- 13
server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java View File

@@ -239,7 +239,7 @@ public class CeActivityDto {
}

public CeActivityDto setErrorMessage(@Nullable String errorMessage) {
this.errorMessage = ensureNotTooBig(errorMessage, MAX_SIZE_ERROR_MESSAGE);
this.errorMessage = ensureNotTooBig(removeCharZeros(errorMessage), MAX_SIZE_ERROR_MESSAGE);
return this;
}

@@ -253,17 +253,6 @@ public class CeActivityDto {
return this;
}

@CheckForNull
private static String ensureNotTooBig(@Nullable String str, int maxSize) {
if (str == null) {
return null;
}
if (str.length() <= maxSize) {
return str;
}
return str.substring(0, maxSize);
}

@CheckForNull
public String getErrorStacktrace() {
return errorStacktrace;
@@ -271,7 +260,7 @@ public class CeActivityDto {

@CheckForNull
public CeActivityDto setErrorStacktrace(@Nullable String errorStacktrace) {
this.errorStacktrace = errorStacktrace;
this.errorStacktrace = removeCharZeros(errorStacktrace);
return this;
}

@@ -306,4 +295,26 @@ public class CeActivityDto {
", hasScannerContext=" + hasScannerContext +
'}';
}

@CheckForNull
private static String ensureNotTooBig(@Nullable String str, int maxSize) {
if (str == null) {
return null;
}
if (str.length() <= maxSize) {
return str;
}
return str.substring(0, maxSize);
}

@CheckForNull
private static String removeCharZeros(@Nullable String str) {
if (str == null || str.isEmpty()) {
return str;
}
return str.codePoints()
.filter(c -> c != "\u0000".codePointAt(0))
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
}

+ 71
- 0
server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDtoTest.java View File

@@ -0,0 +1,71 @@
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db.ce;

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.Random;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;

@RunWith(DataProviderRunner.class)
public class CeActivityDtoTest {
private CeActivityDto underTest = new CeActivityDto();

@Test
@UseDataProvider("stringsWithChar0")
public void setStacktrace_filters_out_char_zero(String withChar0, String expected) {
underTest.setErrorStacktrace(withChar0);

assertThat(underTest.getErrorStacktrace()).isEqualTo(expected);
}

@Test
@UseDataProvider("stringsWithChar0")
public void setErrorMessage_filters_out_char_zero(String withChar0, String expected) {
underTest.setErrorMessage(withChar0);

assertThat(underTest.getErrorMessage()).isEqualTo(expected);
}

@Test
public void setErrorMessage_truncates_to_1000_after_removing_char_zero() {
String before = randomAlphanumeric(50);
String after = randomAlphanumeric(950);
String truncated = randomAlphanumeric(1 + new Random().nextInt(50));
underTest.setErrorMessage(before + "\u0000" + after + truncated);

assertThat(underTest.getErrorMessage()).isEqualTo(before + after);
}

@DataProvider
public static Object[][] stringsWithChar0() {
return new Object[][] {
{"\u0000", ""},
{"\u0000\u0000\u0000\u0000", ""},
{"\u0000 \u0000a\u0000b\u0000 ", " ab "},
{"a \u0000 0message", "a 0message"}
};
}
}

Loading…
Cancel
Save