From 42d8c59b9e8862eb65bae3413dfd1a42940141c9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 18 Jul 2018 17:52:54 +0200 Subject: [PATCH] SONAR-10943 tasks failing with char 0 in message finish normally --- .../java/org/sonar/db/ce/CeActivityDto.java | 37 ++++++---- .../org/sonar/db/ce/CeActivityDtoTest.java | 71 +++++++++++++++++++ 2 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDtoTest.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java index 692244129fd..07e272b70c7 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java @@ -250,7 +250,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; } @@ -264,17 +264,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; @@ -282,7 +271,7 @@ public class CeActivityDto { @CheckForNull public CeActivityDto setErrorStacktrace(@Nullable String errorStacktrace) { - this.errorStacktrace = errorStacktrace; + this.errorStacktrace = removeCharZeros(errorStacktrace); return this; } @@ -318,4 +307,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(); + } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDtoTest.java new file mode 100644 index 00000000000..7782760dc50 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDtoTest.java @@ -0,0 +1,71 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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"} + }; + } +} -- 2.39.5