From c45ef62ba4d695846cbf50f2ce7b785d26722713 Mon Sep 17 00:00:00 2001 From: Andrey Loskutov Date: Fri, 22 Apr 2022 19:20:42 +0200 Subject: [PATCH] Added check for not intended default workspace location use (#35) See original bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=514333 that was only partly fixed. The original fix added -Dosgi.dataAreaRequiresExplicitInit=true system property and changed org.eclipse.core.internal.runtime.DataArea in the way that it doesn't allow to initialize itself if the workspace location was not explicitly specified yet - either via -data or via workspace selection prompt. But the fix in DataArea does not work if bad client code calls Platform.getLocation() *before* calling Plugin.getStateLocation(), because Platform.getLocation() doesn't run into changed DataArea code at all. Exact this happened in commit https://github.com/eclipse-platform/eclipse.platform.resources/commit/a8a8d824145ea1f309eb0b4ade83995544eead50 where workspace init order was slightly changed: original code initialized LocalMetaArea first (which then failed as supposed in assertLocationInitialized() if workspace was not set yet), and after that initialized WorkspaceRoot (which uses Platform.getLocation() after the check). Changed code initialized WorkspaceRoot first, and because this uses Platform.getLocation(), the code silently initializes default workspace location even if -Dosgi.dataAreaRequiresExplicitInit=true is set. The current fix makes sure that if the system flag is given, we disallow silent workspace location initialization by adding similar check in Platform.getLocation(). Fixes issue https://github.com/eclipse-platform/eclipse.platform.runtime/issues/35 --- .../internal/runtime/InternalPlatform.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/InternalPlatform.java b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/InternalPlatform.java index eae43217b..414ccd368 100644 --- a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/InternalPlatform.java +++ b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/InternalPlatform.java @@ -79,6 +79,9 @@ public final class InternalPlatform { public static final String PROP_NL = "osgi.nl"; //$NON-NLS-1$ public static final String PROP_OS = "osgi.os"; //$NON-NLS-1$ + // OSGI IDE specific property, copied from DataArea + private static final String PROP_REQUIRES_EXPLICIT_INIT = "osgi.dataAreaRequiresExplicitInit"; //$NON-NLS-1$ ; + // Eclipse System Properties public static final String PROP_PRODUCT = "eclipse.product"; //$NON-NLS-1$ public static final String PROP_WS = "osgi.ws"; //$NON-NLS-1$ @@ -341,11 +344,24 @@ public Location getInstanceLocation() { public IPath getLocation() throws IllegalStateException { if (cachedInstanceLocation == null) { Location location = getInstanceLocation(); - if (location == null) + if (location == null) { return null; + } + if (!location.isSet()) { + boolean explicitInitRequired = Boolean + .parseBoolean(getBundleContext().getProperty(PROP_REQUIRES_EXPLICIT_INIT)); + if (explicitInitRequired) { + // See bug 514333: don't allow clients to initialize instance location if the + // instance area is not explicitly defined yet + throw new IllegalStateException(CommonMessages.meta_instanceDataUnspecified); + } + } + + // Note: if not explicitly set, this call will resolve to default location + // therefore we have the check above, but only if PROP_REQUIRES_EXPLICIT_INIT is set. URL url = location.getURL(); if (url == null) { - throw new IllegalStateException("instance location is not (yet) set"); //$NON-NLS-1$ + throw new IllegalStateException("Instance location is not (yet) set"); //$NON-NLS-1$ } // This makes the assumption that the instance location is a file: URL File file = new File(url.getFile());