From 45f68e349e3f870fee5d4604fcc8115938484c52 Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Thu, 30 Mar 2023 07:45:01 +0100 Subject: FOP-3125: NPE when using broken link and PDF 1.5 --- .../apache/fop/render/pdf/PDFDocumentHandler.java | 1 + .../render/pdf/PDFDocumentNavigationHandler.java | 6 ++++ .../DocumentNavigationHandlerTestCase.java | 22 ++++++++++-- .../fop/render/extensions/linkmissingdest.if.xml | 42 ++++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 fop-core/src/test/resources/org/apache/fop/render/extensions/linkmissingdest.if.xml diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java index ae2053061..09eb12c54 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java @@ -183,6 +183,7 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler { /** {@inheritDoc} */ public void endDocument() throws IFException { + documentNavigationHandler.registerIncompleteActions(); pdfDoc.getResources().addFonts(pdfDoc, fontInfo); try { if (pdfDoc.isLinearizationEnabled()) { diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java index 005c82a6f..552139980 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java @@ -29,6 +29,7 @@ import org.apache.fop.pdf.PDFDocument; import org.apache.fop.pdf.PDFFactory; import org.apache.fop.pdf.PDFGoTo; import org.apache.fop.pdf.PDFLink; +import org.apache.fop.pdf.PDFObject; import org.apache.fop.pdf.PDFOutline; import org.apache.fop.pdf.PDFReference; import org.apache.fop.pdf.PDFStructElem; @@ -214,4 +215,9 @@ public class PDFDocumentNavigationHandler implements IFDocumentNavigationHandler return action.getID(); } + public void registerIncompleteActions() { + for (Object action : incompleteActions.values()) { + getPDFDoc().addObject((PDFObject) action); + } + } } diff --git a/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java b/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java index 1b8efd112..0578d27e7 100644 --- a/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java @@ -109,13 +109,14 @@ public class DocumentNavigationHandlerTestCase { @Test public void testGotoXYMergedIF() throws Exception { InputStream ifXml = getClass().getResourceAsStream("link.if.xml"); - ByteArrayOutputStream pdf = iFToPDF(ifXml); + ByteArrayOutputStream pdf = iFToPDF(ifXml, ""); Assert.assertTrue(pdf.toString().contains("/S /GoTo")); } - private ByteArrayOutputStream iFToPDF(InputStream is) throws Exception { + private ByteArrayOutputStream iFToPDF(InputStream is, String fopxconf) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); - FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent(); + FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI(), + new ByteArrayInputStream(fopxconf.getBytes())).newFOUserAgent(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); Source src = new StreamSource(is); IFDocumentHandler documentHandler @@ -255,4 +256,19 @@ public class DocumentNavigationHandlerTestCase { GoToXYAction a = (GoToXYAction) b.getAction(); Assert.assertEquals(a.getPageIndex(), 0); } + + @Test + public void testGotoXYMissingDestIF() throws Exception { + InputStream ifXml = getClass().getResourceAsStream("linkmissingdest.if.xml"); + String fopxconf = "\n" + + " true\n" + + " \n" + + " \n" + + " 1.5\n" + + " \n" + + " \n" + + ""; + ByteArrayOutputStream pdf = iFToPDF(ifXml, fopxconf); + Assert.assertTrue(pdf.toString().contains("/S /GoTo")); + } } diff --git a/fop-core/src/test/resources/org/apache/fop/render/extensions/linkmissingdest.if.xml b/fop-core/src/test/resources/org/apache/fop/render/extensions/linkmissingdest.if.xml new file mode 100644 index 000000000..f2e6eacb0 --- /dev/null +++ b/fop-core/src/test/resources/org/apache/fop/render/extensions/linkmissingdest.if.xml @@ -0,0 +1,42 @@ + + +
+ + + + +creator +Document title + + + +
+ + + + + + + + + + + + + + + + + +test + + + + + + + + + + +
-- cgit v1.2.3 >40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/*
 * SonarQube Scanner
 * Copyright (C) 2011-2019 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.sonarsource.scanner.cli;

import java.util.Map;
import java.util.Properties;
import org.sonarsource.scanner.api.EmbeddedScanner;
import org.sonarsource.scanner.api.ScanProperties;

/**
 * Arguments :
 * <ul>
 * <li>scanner.home: optional path to Scanner home (root directory with sub-directories bin, lib and conf)</li>
 * <li>scanner.settings: optional path to runner global settings, usually ${scanner.home}/conf/sonar-scanner.properties.
 * This property is used only if ${scanner.home} is not defined</li>
 * <li>project.home: path to project root directory. If not set, then it's supposed to be the directory where the runner is executed</li>
 * <li>project.settings: optional path to project settings. Default value is ${project.home}/sonar-project.properties.</li>
 * </ul>
 *
 * @since 1.0
 */
public class Main {
  private static final String SEPARATOR = "------------------------------------------------------------------------";
  private final Exit exit;
  private final Cli cli;
  private final Conf conf;
  private EmbeddedScanner runner;
  private ScannerFactory runnerFactory;
  private Logs logger;

  Main(Exit exit, Cli cli, Conf conf, ScannerFactory runnerFactory, Logs logger) {
    this.exit = exit;
    this.cli = cli;
    this.conf = conf;
    this.runnerFactory = runnerFactory;
    this.logger = logger;
  }

  public static void main(String[] args) {
    Logs logs = new Logs(System.out, System.err);
    Exit exit = new Exit();
    Cli cli = new Cli(exit, logs).parse(args);
    Main main = new Main(exit, cli, new Conf(cli, logs, System.getenv()), new ScannerFactory(logs), logs);
    main.execute();
  }

  void execute() {
    Stats stats = new Stats(logger).start();

    int status = Exit.INTERNAL_ERROR;
    try {
      Properties p = conf.properties();
      checkSkip(p);
      configureLogging(p);
      init(p);
      runner.start();
      logger.info(String.format("Analyzing on %s", conf.isSonarCloud(null) ? "SonarCloud" : ("SonarQube server " + runner.serverVersion())));
      execute(stats, p);
      status = Exit.SUCCESS;
    } catch (Throwable e) {
      displayExecutionResult(stats, "FAILURE");
      showError("Error during SonarScanner execution", e, cli.isDebugEnabled());
      status = isUserError(e) ? Exit.USER_ERROR : Exit.INTERNAL_ERROR;
    } finally {
      exit.exit(status);
    }

  }

  private void checkSkip(Properties properties) {
    if ("true".equalsIgnoreCase(properties.getProperty(ScanProperties.SKIP))) {
      logger.info("SonarScanner analysis skipped");
      exit.exit(Exit.SUCCESS);
    }
  }

  private void init(Properties p) {
    SystemInfo.print(logger);
    if (cli.isDisplayVersionOnly()) {
      exit.exit(Exit.SUCCESS);
    }

    runner = runnerFactory.create(p);
  }

  private void configureLogging(Properties props) {
    if ("true".equals(props.getProperty("sonar.verbose"))
      || "DEBUG".equalsIgnoreCase(props.getProperty("sonar.log.level"))
      || "TRACE".equalsIgnoreCase(props.getProperty("sonar.log.level"))) {
      logger.setDebugEnabled(true);
    }
  }

  private void execute(Stats stats, Properties p) {
    runner.execute((Map) p);
    displayExecutionResult(stats, "SUCCESS");
  }

  private void displayExecutionResult(Stats stats, String resultMsg) {
    logger.info(SEPARATOR);
    logger.info("EXECUTION " + resultMsg);
    logger.info(SEPARATOR);
    stats.stop();
    logger.info(SEPARATOR);
  }

  private void showError(String message, Throwable e, boolean debug) {
    if (debug || !isUserError(e)) {
      logger.error(message, e);
    } else {
      logger.error(message);
      logger.error(e.getMessage());
      String previousMsg = "";
      for (Throwable cause = e.getCause(); cause != null
        && cause.getMessage() != null
        && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) {
        logger.error("Caused by: " + cause.getMessage());
        previousMsg = cause.getMessage();
      }
    }

    if (!cli.isDebugEnabled()) {
      logger.error("");
      suggestDebugMode();
    }
  }

  private static boolean isUserError(Throwable e) {
    // class not available at compile time (loaded by isolated classloader)
    return "org.sonar.api.utils.MessageException".equals(e.getClass().getName());
  }

  private void suggestDebugMode() {
    if (!cli.isEmbedded()) {
      logger.error("Re-run SonarScanner using the -X switch to enable full debug logging.");
    }
  }

}