Skip to content

Commit ccf92c7

Browse files
committed
Prevent a exception if path contains incorrect characters like '<' & '>'.
1 parent de1bb74 commit ccf92c7

File tree

18 files changed

+72
-45
lines changed

18 files changed

+72
-45
lines changed

Boot2/Nitra.Compiler/CompilingNitraProject.n

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Nemerle;
1+
using Nemerle;
22
using Nemerle.Collections;
33
using Nemerle.Compiler;
44
using Nemerle.Imperative;
@@ -82,16 +82,16 @@ namespace Nitra.Compiler
8282
mutable source;
8383
when (_loadedSources.TryGetValue(filePath, out source))
8484
return source;
85-
85+
8686
def isExecutable = filePath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || filePath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase);
87-
if (!isExecutable && IOFile.Exists(filePath))
87+
if (!isExecutable && NitraUtils.IsFileExists(filePath))
8888
{
8989
def text = IOFile.ReadAllText(filePath, Encoding.UTF8);
9090
source = SourceSnapshot(text, filePath)
9191
}
9292
else
9393
source = SingleLineSourceSnapshot("", filePath);
94-
94+
9595
_loadedSources[filePath] = source;
9696

9797
source

Boot2/Nitra.Grammar/ProjectSystem/NemerleFile.n

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Nemerle;
1+
using Nemerle;
22
using Nemerle.Compiler;
33
using Nemerle.Collections;
44
using Nemerle.Text;
@@ -67,7 +67,7 @@ namespace Nitra.ProjectSystem
6767
base(project, index);
6868
def fullName = FullName;
6969
def isExecutable = fullName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || fullName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase);
70-
if (!isExecutable && File.Exists(fullName))
70+
if (!isExecutable && NitraUtils.IsFileExists(fullName))
7171
{
7272
def text = File.ReadAllText(FullName, Encoding.UTF8);
7373
_source = SourceSnapshot(text, this, 0);

Boot2/Nitra.Grammar/Utils/Utils.n

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ namespace Nitra
7979
{
8080
def fullName = file.FullName;
8181
def isExecutable = fullName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || fullName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase);
82-
if (!isExecutable && File.Exists(fullName))
82+
if (!isExecutable && NitraUtils.IsFileExists(fullName))
8383
Compiler.Location(fullName, startLine, startCol, endLine, endCol)
8484
else
8585
Compiler.Location.Default

Boot2/Nitra.Runtime/NitraUtils.n

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Nemerle;
1+
using Nemerle;
22
using Nemerle.Collections;
33
using Nemerle.Imperative;
44
using Nemerle.Text;
@@ -10,6 +10,7 @@ using System;
1010
using System.Collections.Generic;
1111
using System.Collections.Immutable;
1212
using System.Globalization;
13+
using System.IO;
1314
using System.Linq;
1415

1516
using SCG = System.Collections.Generic;
@@ -95,5 +96,18 @@ namespace Nitra
9596
| _ => BindResult.Multiple(res.ToImmutableArray())
9697
}
9798
}
99+
100+
public IsFileExists(path : string) : bool
101+
{
102+
when (FilePathHasInvalidChars(path))
103+
return false;
104+
105+
File.Exists(path)
106+
}
107+
108+
public FilePathHasInvalidChars(path : string) : bool
109+
{
110+
!string.IsNullOrEmpty(path) && path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0
111+
}
98112
} // module
99113
} // namespace

Boot2/Nitra.Runtime/ProjectSystem/FileBased/FsFile.n

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Nitra.ProjectSystem
2323
{
2424
base(statistics);
2525
Language = language;
26-
assert3(IO.File.Exists(filePath));
26+
assert3(NitraUtils.IsFileExists(filePath));
2727
FilePath = filePath;
2828
when (fsProject != null)
2929
fsProject.FsFiles.Add(this);

Boot2/Nitra.Runtime/Serialization/MetadataReader.n

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ namespace Nitra.Serialization
221221
{
222222
def isExecutable = filePath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || filePath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase);
223223
def source =
224-
if (!isExecutable && File.Exists(filePath))
224+
if (!isExecutable && NitraUtils.IsFileExists(filePath))
225225
{
226226
def text = File.ReadAllText(filePath, Encoding.UTF8);
227227
SourceSnapshot(text, filePath)

Nitra.LanguageCompiler/CommandPromptReader.n

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ namespace Nitra.LanguageCompiler
128128
{
129129
def asmRef = Path.GetFullPath(assemblyReference);
130130

131-
when (!File.Exists(asmRef))
131+
when (!NitraUtils.IsFileExists(asmRef))
132132
{
133133
WriteLine($"Error: The '$asmRef' file does not exist.");
134134
usage();

Nitra.LanguageCompiler/Main.n

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ namespace Nitra.LanguageCompiler
140140
def path = Path.GetFullPath(path1);
141141
if (Directory.Exists(path))
142142
FileSystem.CopyDirectory(path, binPath, UIOption.OnlyErrorDialogs);
143-
else if (File.Exists(path))
143+
else if (NitraUtils.IsFileExists(path))
144144
{
145145
def fileName = Path.GetFileName(path);
146146
def outPath = Path.Combine(binPath, fileName);
147147
File.Copy(path, outPath, overwrite=true);
148148
def pdbPath = Path.ChangeExtension(path, ".pdb");
149-
when (File.Exists(pdbPath))
149+
when (NitraUtils.IsFileExists(pdbPath))
150150
{
151151
def pdbFileName = Path.GetFileName(pdbPath);
152152
def pdbOutPath = Path.Combine(binPath, pdbFileName);
@@ -217,7 +217,7 @@ namespace Nitra.LanguageCompiler
217217
def prjName = config.ProjectName;
218218
def guidsPath = Path.Combine(config.OutputPath, prjName, guidsFileName);
219219
def templateGuids = ReadGuids(templateGuidsPath);
220-
def guids = if (config.NewGuids || !File.Exists(guidsPath)) Hashtable() else ReadGuids(guidsPath);
220+
def guids = if (config.NewGuids || !NitraUtils.IsFileExists(guidsPath)) Hashtable() else ReadGuids(guidsPath);
221221
mutable replaces = vars;
222222

223223
foreach ((templateConstName, templateGuid) in templateGuids.KeyValuePairs)
@@ -308,7 +308,7 @@ namespace Nitra.LanguageCompiler
308308
ExpandTemplates(VsPackageTemplatePath, Path.Combine(config.OutputPath, prjName), vars, config);
309309

310310
def snkPath = Path.Combine(config.OutputPath, prjName, "Key.snk");
311-
unless (File.Exists(snkPath))
311+
unless (NitraUtils.IsFileExists(snkPath))
312312
CreateSnk(snkPath);
313313
}
314314

Nitra.TestsConverter/Main.n

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,43 @@ module Program
1515
{
1616
when (args.Length != 1)
1717
Prompt(1);
18-
18+
1919
def path = args[0];
20-
20+
2121
when (!Directory.Exists(path))
2222
Prompt(2);
2323

2424
def configPath = Path.Combine(path, "config.xml");
25-
25+
2626
when (!File.Exists(configPath))
2727
Prompt(3);
2828

2929
def dirs = Directory.GetDirectories(path);
30-
30+
3131
mutable totalConverted = 0;
32-
32+
3333
foreach (dir in dirs)
3434
{
3535
def testName = Path.GetFileNameWithoutExtension(dir);
3636
def projectPath = Path.Combine(dir, testName);
37-
37+
3838
when (Directory.Exists(projectPath))
3939
{
4040
Error($<#Can't create dirrectory for '$testName' test. The dirrectory '$projectPath' allredy exists.#>);
4141
continue;
4242
}
43-
43+
4444
_ = Directory.CreateDirectory(projectPath);
45-
45+
4646
def files = Enumerable.Concat(Directory.EnumerateFiles(dir, "*.test"), Directory.EnumerateFiles(dir, "*.gold"));
4747
foreach (file in files)
4848
File.Move(file, Path.Combine(projectPath, Path.GetFileName(file)));
49-
49+
5050
totalConverted++;
5151
}
52-
52+
5353
def files = Directory.GetFiles(path, "*.test");
54-
54+
5555
foreach (file in files)
5656
{
5757
def testName = Path.GetFileNameWithoutExtension(file);
@@ -61,27 +61,27 @@ module Program
6161
Error($<#Can't create dirrectory for '$testName' test. The dirrectory '$solutionPath' allredy exists.#>);
6262
continue;
6363
}
64-
64+
6565
_ = Directory.CreateDirectory(solutionPath);
6666
def projectPath = Path.Combine(solutionPath, testName);
6767
_ = Directory.CreateDirectory(projectPath);
68-
68+
6969
def destTestFilePath = Path.Combine(projectPath, testName) + ".test";
7070
File.Move(file, destTestFilePath);
71-
71+
7272
def destGoldFilePath = Path.Combine(projectPath, testName) + ".gold";
7373
def goldPath = Path.ChangeExtension(file, ".gold");
7474
when (File.Exists(goldPath))
7575
File.Move(goldPath, destGoldFilePath);
76-
76+
7777
totalConverted++;
7878
}
7979

8080
def totalTests = dirs.Length + files.Length;
8181
WriteLine($"Converted $totalConverted of $totalTests tests.");
8282
0;
8383
}
84-
84+
8585
Prompt(errorNum : int = 0) : void
8686
{
8787
WriteLine(<#usings: TestsConverter.exe "path to test-suit folder"#>);

Nitra.TestsLauncher/Main.n

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,4 @@ namespace Nitra.TestsLauncher
247247
suites.SelectMany(x => x.GetAllTests()).Select(x => x.Name.Length).Max() + 3
248248
}
249249
}
250-
}
250+
}

0 commit comments

Comments
 (0)