From edb01a788585e35cb247f67161b4944a9d1be364 Mon Sep 17 00:00:00 2001 From: René Purrio Date: Wed, 17 Jan 2018 09:03:49 +0100 Subject: Bug 529828 - [sonar] Resolve "Possible null pointer dereference" Change-Id: Iddfb24c63e3454c3de9e539db25c379c7775f06c Signed-off-by: René Purrio --- .../help/internal/base/IndexToolApplication.java | 3 +++ .../eclipse/help/internal/search/SearchIndex.java | 3 +++ .../eclipse/help/internal/standalone/Eclipse.java | 9 ++++++++- .../org/eclipse/help/search/HelpIndexBuilder.java | 20 ++++++++++++++------ .../help/ui/internal/views/ScopeSetManager.java | 3 +++ .../src/org/eclipse/help/internal/HelpData.java | 8 +++++++- .../help/dynamic/DynamicXHTMLProcessorTest.java | 7 ++++++- .../ua/tests/help/dynamic/XMLProcessorTest.java | 14 ++++++++++++-- 8 files changed, 56 insertions(+), 11 deletions(-) diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/base/IndexToolApplication.java b/org.eclipse.help.base/src/org/eclipse/help/internal/base/IndexToolApplication.java index f254fc69d..d050bf870 100644 --- a/org.eclipse.help.base/src/org/eclipse/help/internal/base/IndexToolApplication.java +++ b/org.eclipse.help.base/src/org/eclipse/help/internal/base/IndexToolApplication.java @@ -100,6 +100,9 @@ public class IndexToolApplication implements IApplication { private static void delete(File file) throws IOException { if (file.isDirectory()) { File files[] = file.listFiles(); + if(files == null) { + throw new IOException("Content from directory '" + file.getAbsolutePath() + "' can not be listed."); //$NON-NLS-1$ //$NON-NLS-2$ + } for (int i = 0; i < files.length; i++) { delete(files[i]); } diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java b/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java index 0903ed4ef..c8fd3e5ca 100644 --- a/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java +++ b/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java @@ -210,6 +210,9 @@ public class SearchIndex implements IHelpSearchIndex { private void deleteDir(File indexDir) { File[] files = indexDir.listFiles(); + if(files == null) { + files = new File[0]; + } for (File file : files) { if (file.isDirectory()) deleteDir(file); diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/standalone/Eclipse.java b/org.eclipse.help.base/src/org/eclipse/help/internal/standalone/Eclipse.java index 53d2b8339..4416ecaf3 100644 --- a/org.eclipse.help.base/src/org/eclipse/help/internal/standalone/Eclipse.java +++ b/org.eclipse.help.base/src/org/eclipse/help/internal/standalone/Eclipse.java @@ -10,7 +10,11 @@ *******************************************************************************/ package org.eclipse.help.internal.standalone; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.List; /** @@ -176,6 +180,9 @@ public class Eclipse extends Thread { throw new Exception("Plugins directory " + pluginsDir.getAbsolutePath() //$NON-NLS-1$ + " does not exists. Pass a correct -eclipsehome option"); //$NON-NLS-1$ File[] plugins = pluginsDir.listFiles(); + if(plugins == null) { + throw new IOException("Content from plugins directory '" + pluginsDir.getAbsolutePath() + "' can not be listed."); //$NON-NLS-1$ //$NON-NLS-2$ + } for (int i = 0; i < plugins.length; i++) { String file = plugins[i].getName(); if (file.startsWith("org.eclipse.equinox.launcher_") && file.endsWith(".jar") && !plugins[i].isDirectory()) //$NON-NLS-1$ //$NON-NLS-2$ diff --git a/org.eclipse.help.base/src/org/eclipse/help/search/HelpIndexBuilder.java b/org.eclipse.help.base/src/org/eclipse/help/search/HelpIndexBuilder.java index 776ddb487..0ab6701a2 100644 --- a/org.eclipse.help.base/src/org/eclipse/help/search/HelpIndexBuilder.java +++ b/org.eclipse.help.base/src/org/eclipse/help/search/HelpIndexBuilder.java @@ -352,7 +352,7 @@ public class HelpIndexBuilder { * nl/language/country/ locale dirs that contain files. We will * produce an index for each one. */ - private void computeLocaleDirs(boolean fragment) { + private void computeLocaleDirs(boolean fragment) throws CoreException { if (!fragment) { LocaleDir dir = new LocaleDir(null, "/"); //$NON-NLS-1$ dir.addDirectory(destination); @@ -366,7 +366,7 @@ public class HelpIndexBuilder { File nl = new File(destination, "nl"); //$NON-NLS-1$ if (!nl.exists() || !nl.isDirectory()) return; - File [] languages = nl.listFiles(); + File [] languages = listFiles(nl); HashSet locales = new HashSet<>(); for (int i=0; i name.endsWith(ScopeSet.EXT) || name.endsWith(HistoryScopeSet.EXT)); + if(files == null) { + files = new File[0]; + } for (int i = 0; i < files.length; i++) { File file = files[i]; String name = file.getName(); diff --git a/org.eclipse.help/src/org/eclipse/help/internal/HelpData.java b/org.eclipse.help/src/org/eclipse/help/internal/HelpData.java index d18c16103..909798061 100644 --- a/org.eclipse.help/src/org/eclipse/help/internal/HelpData.java +++ b/org.eclipse.help/src/org/eclipse/help/internal/HelpData.java @@ -152,7 +152,13 @@ public class HelpData { * Allow unit tests to override for providing test data. */ public InputStream getHelpDataFile(String filePath) throws IOException { - return Platform.getProduct().getDefiningBundle().getEntry(filePath).openStream(); + IProduct product = Platform.getProduct(); + Bundle definingBundle = product != null ? product.getDefiningBundle() : null; + URL entry = definingBundle != null ? definingBundle.getEntry(filePath) : null; + if(entry == null) { + throw new IOException("No entry to '"+filePath+"' could be found or caller does not have the appropriate permissions.");//$NON-NLS-1$ //$NON-NLS-2$ + } + return entry.openStream(); } /* diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java index 8baec7e6a..176f6bbdd 100644 --- a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java @@ -15,6 +15,7 @@ import static org.junit.Assert.assertTrue; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.URL; import java.nio.charset.StandardCharsets; import javax.xml.parsers.ParserConfigurationException; @@ -48,7 +49,11 @@ public class DynamicXHTMLProcessorTest { protected InputStream getProcessedInput(String path, Bundle bundle) throws IOException, SAXException, ParserConfigurationException, TransformerException, TransformerConfigurationException { - try (InputStream in = bundle.getEntry(path).openStream()) { + URL url = bundle.getEntry(path); + if(url == null ) { + throw new IOException("No entry to '"+path+"' could be found or caller does not have the appropriate permissions.");//$NON-NLS-1$ //$NON-NLS-2$ + } + try (InputStream in = url.openStream()) { String href = '/' + bundle.getBundleId() + path; return DynamicXHTMLProcessor.process(href, in, "en", true); } diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/XMLProcessorTest.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/XMLProcessorTest.java index c146999c1..0628d7026 100644 --- a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/XMLProcessorTest.java +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/XMLProcessorTest.java @@ -10,7 +10,9 @@ *******************************************************************************/ package org.eclipse.ua.tests.help.dynamic; +import java.io.IOException; import java.io.InputStream; +import java.net.URL; import org.eclipse.core.runtime.Platform; import org.eclipse.help.internal.base.HelpEvaluationContext; @@ -45,8 +47,16 @@ public class XMLProcessorTest { }; XMLProcessor processor = new XMLProcessor(handlers); Bundle bundle = UserAssistanceTestPlugin.getDefault().getBundle(); - try (InputStream in = bundle.getEntry(FileUtil.getResultFile(path)).openStream(); - InputStream in2 = processor.process(bundle.getEntry(path).openStream(), + URL url1 = bundle.getEntry(FileUtil.getResultFile(path)); + if(url1 == null) { + throw new IOException("No entry to '"+FileUtil.getResultFile(path)+"' could be found or caller does not have the appropriate permissions.");//$NON-NLS-1$ //$NON-NLS-2$ + } + URL url2 = bundle.getEntry(path); + if(url2 == null) { + throw new IOException("No entry to '"+path+"' could be found or caller does not have the appropriate permissions.");//$NON-NLS-1$ //$NON-NLS-2$ + } + try (InputStream in = url1.openStream(); + InputStream in2 = processor.process(url2.openStream(), '/' + bundle.getSymbolicName() + '/' + path, "UTF-8")) { XMLUtil.assertXMLEquals("XML content was not processed correctly: " + path, in, in2); } -- cgit v1.2.1