]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10943 task failing with char 0 in message finishes normally
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 18 Jul 2018 08:10:57 +0000 (10:10 +0200)
committerSonarTech <sonartech@sonarsource.com>
Thu, 19 Jul 2018 18:21:25 +0000 (20:21 +0200)
even on Postgres

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 df5d9aa8b008edcb5aae54c1357bacfeb2c509c5..19be3c4951a35a44fb519af2574f2b4a26b6a3ee 100644 (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();
+  }
 }
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..d5991d9
--- /dev/null
@@ -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"}
+    };
+  }
+}