Browse Source

#1663: Add various safe-guards against unintended use to Language and SolutionWriter.

pull/2134/head
Siegfried Pammer 6 years ago
parent
commit
6bd3f5fe74
  1. 6
      ILSpy/Languages/ILLanguage.cs
  2. 4
      ILSpy/Languages/Language.cs
  3. 30
      ILSpy/SolutionWriter.cs

6
ILSpy/Languages/ILLanguage.cs

@ -16,6 +16,7 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Linq; using System.Linq;
@ -165,6 +166,11 @@ namespace ICSharpCode.ILSpy
var metadata = module.Metadata; var metadata = module.Metadata;
var dis = CreateDisassembler(output, options); var dis = CreateDisassembler(output, options);
if (options.FullDecompilation && options.SaveAsProjectDirectory != null)
{
throw new NotSupportedException($"Language '{Name}' does not support exporting assemblies as projects!");
}
// don't automatically load additional assemblies when an assembly node is selected in the tree view // don't automatically load additional assemblies when an assembly node is selected in the tree view
using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad(assembly.AssemblyList)) using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad(assembly.AssemblyList))
{ {

4
ILSpy/Languages/Language.cs

@ -130,6 +130,10 @@ namespace ICSharpCode.ILSpy
var asm = assembly.GetPEFileOrNull(); var asm = assembly.GetPEFileOrNull();
if (asm == null) if (asm == null)
return null; return null;
if (options.FullDecompilation && options.SaveAsProjectDirectory != null)
{
throw new NotSupportedException($"Language '{Name}' does not support exporting assemblies as projects!");
}
var metadata = asm.Metadata; var metadata = asm.Metadata;
if (metadata.IsAssembly) if (metadata.IsAssembly)
{ {

30
ILSpy/SolutionWriter.cs

@ -113,9 +113,17 @@ namespace ICSharpCode.ILSpy
n => WriteProject(n, language, solutionDirectory, ct))) n => WriteProject(n, language, solutionDirectory, ct)))
.ConfigureAwait(false); .ConfigureAwait(false);
if (projects.Count == 0)
{
result.WriteLine();
result.WriteLine("Solution could not be created, because none of the selected assemblies could be decompiled into a project.");
}
else
{
await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects)) await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects))
.ConfigureAwait(false); .ConfigureAwait(false);
} }
}
catch (AggregateException ae) catch (AggregateException ae)
{ {
if (ae.Flatten().InnerExceptions.All(e => e is OperationCanceledException)) if (ae.Flatten().InnerExceptions.All(e => e is OperationCanceledException))
@ -167,8 +175,23 @@ namespace ICSharpCode.ILSpy
void WriteProject(LoadedAssembly loadedAssembly, Language language, string targetDirectory, CancellationToken ct) void WriteProject(LoadedAssembly loadedAssembly, Language language, string targetDirectory, CancellationToken ct)
{ {
targetDirectory = Path.Combine(targetDirectory, loadedAssembly.ShortName); targetDirectory = Path.Combine(targetDirectory, loadedAssembly.ShortName);
if (language.ProjectFileExtension == null)
{
statusOutput.Add("-------------");
statusOutput.Add($"Language '{language.Name}' does not support exporting assemblies as projects!");
return;
}
string projectFileName = Path.Combine(targetDirectory, loadedAssembly.ShortName + language.ProjectFileExtension); string projectFileName = Path.Combine(targetDirectory, loadedAssembly.ShortName + language.ProjectFileExtension);
if (File.Exists(targetDirectory))
{
statusOutput.Add("-------------");
statusOutput.Add($"Failed to create a directory '{targetDirectory}':{Environment.NewLine}A file with the same name already exists!");
return;
}
if (!Directory.Exists(targetDirectory)) if (!Directory.Exists(targetDirectory))
{ {
try try
@ -177,6 +200,7 @@ namespace ICSharpCode.ILSpy
} }
catch (Exception e) catch (Exception e)
{ {
statusOutput.Add("-------------");
statusOutput.Add($"Failed to create a directory '{targetDirectory}':{Environment.NewLine}{e}"); statusOutput.Add($"Failed to create a directory '{targetDirectory}':{Environment.NewLine}{e}");
return; return;
} }
@ -200,8 +224,14 @@ namespace ICSharpCode.ILSpy
} }
} }
} }
catch (NotSupportedException e)
{
statusOutput.Add("-------------");
statusOutput.Add($"Failed to decompile the assembly '{loadedAssembly.FileName}':{Environment.NewLine}{e.Message}");
}
catch (Exception e) when (!(e is OperationCanceledException)) catch (Exception e) when (!(e is OperationCanceledException))
{ {
statusOutput.Add("-------------");
statusOutput.Add($"Failed to decompile the assembly '{loadedAssembly.FileName}':{Environment.NewLine}{e}"); statusOutput.Add($"Failed to decompile the assembly '{loadedAssembly.FileName}':{Environment.NewLine}{e}");
} }
} }

Loading…
Cancel
Save