fix: std path safety
This commit is contained in:
parent
6848ed36b9
commit
30c87577fe
4 changed files with 59 additions and 19 deletions
|
@ -17,6 +17,11 @@ Changelog structure reference:
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
### Fixed
|
||||
|
||||
- Project
|
||||
- Safer standard library path resolution
|
||||
|
||||
## [17.1.0]
|
||||
|
||||
### Fixed
|
||||
|
|
|
@ -147,7 +147,7 @@ public class ZigProjectSettingsPanel implements MyDisposable {
|
|||
toolchainVersion.setForeground(JBColor.foreground());
|
||||
|
||||
if (!stdFieldOverride.isSelected())
|
||||
pathToStdField.setText(StringUtil.orEmpty(stdPath));
|
||||
pathToStdField.setText(StringUtil.orEmpty(stdPath.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,8 @@ import org.jetbrains.annotations.NotNull;
|
|||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.Icon;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
@ -49,11 +51,30 @@ public class ZigSyntheticLibrary extends SyntheticLibrary implements ItemPresent
|
|||
val service = ZigProjectSettingsService.getInstance(project);
|
||||
val state = service.getState();
|
||||
this.roots = ApplicationManager.getApplication().executeOnPooledThread(() -> {
|
||||
var roots = pathToVFS(state.getExplicitPathToStd());
|
||||
if (roots != null) {
|
||||
return roots;
|
||||
}
|
||||
val toolchain = state.getToolchain();
|
||||
blk: try {
|
||||
val ePathStr = state.getExplicitPathToStd();
|
||||
if (ePathStr == null) {
|
||||
break blk;
|
||||
}
|
||||
val ePath = Path.of(ePathStr);
|
||||
if (ePath.isAbsolute()) {
|
||||
var roots = pathToVFS(ePath);
|
||||
if (roots != null) {
|
||||
return roots;
|
||||
}
|
||||
} else if (toolchain != null) {
|
||||
val stdPath = toolchain.getLocation().resolve(ePath);
|
||||
if (stdPath.isAbsolute()) {
|
||||
var roots = pathToVFS(stdPath);
|
||||
if (roots != null) {
|
||||
return roots;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (InvalidPathException ignored) {
|
||||
|
||||
}
|
||||
if (toolchain != null) {
|
||||
val stdPath =
|
||||
toolchain.zig().getStdPath().orElse(null);
|
||||
|
@ -69,16 +90,13 @@ public class ZigSyntheticLibrary extends SyntheticLibrary implements ItemPresent
|
|||
.orElse("Zig"));
|
||||
}
|
||||
|
||||
private static @Nullable Collection<VirtualFile> pathToVFS(String path) {
|
||||
if (path != null && !path.isEmpty()) {
|
||||
val thePath = PathUtil.pathFromString(path);
|
||||
if (thePath != null) {
|
||||
val file = VfsUtil.findFile(thePath, true);
|
||||
if (file != null) {
|
||||
val children = file.getChildren();
|
||||
if (children != null && children.length > 0)
|
||||
return Arrays.asList(children);
|
||||
}
|
||||
private static @Nullable Collection<VirtualFile> pathToVFS(Path path) {
|
||||
if (path != null) {
|
||||
val file = VfsUtil.findFile(path, true);
|
||||
if (file != null) {
|
||||
val children = file.getChildren();
|
||||
if (children != null && children.length > 0)
|
||||
return Arrays.asList(children);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
@ -86,7 +104,8 @@ public class ZigSyntheticLibrary extends SyntheticLibrary implements ItemPresent
|
|||
@Override
|
||||
public @NotNull Collection<VirtualFile> getSourceRoots() {
|
||||
try {
|
||||
return roots.get();
|
||||
val rootsRaw = roots.get();
|
||||
return rootsRaw == null ? Collections.emptySet() : rootsRaw;
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
return Collections.emptySet();
|
||||
|
|
|
@ -25,6 +25,7 @@ import com.intellij.openapi.application.ApplicationManager;
|
|||
import lombok.val;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
@ -32,7 +33,7 @@ import java.util.concurrent.ExecutionException;
|
|||
public class ZigCompilerTool extends AbstractZigTool{
|
||||
public static final String TOOL_NAME = "zig";
|
||||
private final Lazy<Optional<String>> version;
|
||||
private final Lazy<Optional<String>> stdPath;
|
||||
private final Lazy<Optional<Path>> stdPath;
|
||||
|
||||
public ZigCompilerTool(AbstractZigToolchain toolchain) {
|
||||
super(toolchain, TOOL_NAME);
|
||||
|
@ -47,7 +48,22 @@ public class ZigCompilerTool extends AbstractZigTool{
|
|||
});
|
||||
stdPath = new Lazy<>(() -> {
|
||||
try {
|
||||
return baseFuture.get().map(ZigToolchainEnvironmentSerializable::stdDirectory);
|
||||
return baseFuture.get().map(ZigToolchainEnvironmentSerializable::stdDirectory)
|
||||
.map(pathStr -> {
|
||||
try {
|
||||
val path = Path.of(pathStr);
|
||||
if (path.isAbsolute()) {
|
||||
return path;
|
||||
}
|
||||
val resolvedPath = toolchain.getLocation().resolve(path);
|
||||
if (resolvedPath.isAbsolute()) {
|
||||
return resolvedPath;
|
||||
}
|
||||
return null;
|
||||
} catch (InvalidPathException e) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
@ -61,7 +77,7 @@ public class ZigCompilerTool extends AbstractZigTool{
|
|||
|
||||
}
|
||||
|
||||
public Optional<String> getStdPath() {
|
||||
public Optional<Path> getStdPath() {
|
||||
return stdPath.get();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue