]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10943 tasks failing with char 0 in message finish normally 3177/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 18 Jul 2018 15:52:54 +0000 (17:52 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 19 Jul 2018 08:49:32 +0000 (10:49 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java
server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDtoTest.java [new file with mode: 0644]

index 692244129fdd13957bdfa2ef6471930214699c96..07e272b70c7b875599d8e2fecc8f4a8d43c43e55 100644 (file)
@@ -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 (file)
index 0000000..7782760
--- /dev/null
@@ -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"}
+    };
+  }
+}