Add null checks everywhere in dependency model

This commit is contained in:
Pavel Krymets 2016-03-16 16:31:38 -07:00
parent 88fc96803a
commit 665dc9bcce
14 changed files with 115 additions and 15 deletions

View file

@ -19,6 +19,10 @@ namespace Microsoft.Extensions.DependencyModel
bool serviceable) bool serviceable)
: base(type, name, version, hash, dependencies, serviceable) : base(type, name, version, hash, dependencies, serviceable)
{ {
if (assemblies == null)
{
throw new ArgumentNullException(nameof(assemblies));
}
Assemblies = assemblies.ToArray(); Assemblies = assemblies.ToArray();
} }

View file

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -8,7 +9,7 @@ namespace Microsoft.Extensions.DependencyModel
{ {
public class CompilationOptions public class CompilationOptions
{ {
public IEnumerable<string> Defines { get; } public IReadOnlyList<string> Defines { get; }
public string LanguageVersion { get; } public string LanguageVersion { get; }
@ -26,7 +27,7 @@ namespace Microsoft.Extensions.DependencyModel
public bool? PublicSign { get; } public bool? PublicSign { get; }
public string DebugType { get; } public string DebugType { get; }
public bool? EmitEntryPoint { get; } public bool? EmitEntryPoint { get; }
@ -59,7 +60,11 @@ namespace Microsoft.Extensions.DependencyModel
bool? emitEntryPoint, bool? emitEntryPoint,
bool? generateXmlDocumentation) bool? generateXmlDocumentation)
{ {
Defines = defines; if (defines == null)
{
throw new ArgumentNullException(nameof(defines));
}
Defines = defines.ToArray();
LanguageVersion = languageVersion; LanguageVersion = languageVersion;
Platform = platform; Platform = platform;
AllowUnsafe = allowUnsafe; AllowUnsafe = allowUnsafe;

View file

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using Microsoft.Extensions.Internal; using Microsoft.Extensions.Internal;
namespace Microsoft.Extensions.DependencyModel namespace Microsoft.Extensions.DependencyModel
@ -9,6 +10,14 @@ namespace Microsoft.Extensions.DependencyModel
{ {
public Dependency(string name, string version) public Dependency(string name, string version)
{ {
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(nameof(name));
}
if (string.IsNullOrEmpty(version))
{
throw new ArgumentException(nameof(version));
}
Name = name; Name = name;
Version = version; Version = version;
} }

View file

@ -21,13 +21,9 @@ namespace Microsoft.Extensions.DependencyModel
IEnumerable<RuntimeLibrary> runtimeLibraries, IEnumerable<RuntimeLibrary> runtimeLibraries,
IEnumerable<RuntimeFallbacks> runtimeGraph) IEnumerable<RuntimeFallbacks> runtimeGraph)
{ {
if (targetFramework == null) if (string.IsNullOrEmpty(targetFramework))
{ {
throw new ArgumentNullException(nameof(targetFramework)); throw new ArgumentException(nameof(targetFramework));
}
if (runtime == null)
{
throw new ArgumentNullException(nameof(runtime));
} }
if (compilationOptions == null) if (compilationOptions == null)
{ {

View file

@ -13,6 +13,10 @@ namespace Microsoft.Extensions.DependencyModel
{ {
public DependencyContext Read(Stream stream) public DependencyContext Read(Stream stream)
{ {
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
var lines = new List<DepsFileLine>(); var lines = new List<DepsFileLine>();
using (var reader = new StreamReader(stream)) using (var reader = new StreamReader(stream))
{ {

View file

@ -14,6 +14,10 @@ namespace Microsoft.Extensions.DependencyModel
{ {
public DependencyContext Read(Stream stream) public DependencyContext Read(Stream stream)
{ {
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
using (var streamReader = new StreamReader(stream)) using (var streamReader = new StreamReader(stream))
{ {
using (var reader = new JsonTextReader(streamReader)) using (var reader = new JsonTextReader(streamReader))

View file

@ -49,7 +49,7 @@ namespace Microsoft.Extensions.DependencyModel
internal virtual bool IsEntryAssembly(Assembly assembly) internal virtual bool IsEntryAssembly(Assembly assembly)
{ {
return assembly.GetName() == Assembly.GetEntryAssembly().GetName(); return assembly.GetName() == Assembly.GetEntryAssembly()?.GetName();
} }
internal virtual Stream GetResourceStream(Assembly assembly, string name) internal virtual Stream GetResourceStream(Assembly assembly, string name)
@ -166,7 +166,7 @@ namespace Microsoft.Extensions.DependencyModel
private static string[] GetHostDepsList() private static string[] GetHostDepsList()
{ {
// TODO: Were going to replace this with AppContext.GetData // TODO: We're going to replace this with AppContext.GetData
var appDomainType = typeof(object).GetTypeInfo().Assembly?.GetType("System.AppDomain"); var appDomainType = typeof(object).GetTypeInfo().Assembly?.GetType("System.AppDomain");
var currentDomain = appDomainType?.GetProperty("CurrentDomain")?.GetValue(null); var currentDomain = appDomainType?.GetProperty("CurrentDomain")?.GetValue(null);
var deps = appDomainType?.GetMethod("GetData")?.Invoke(currentDomain, new[] { "APP_CONTEXT_DEPS_FILES" }); var deps = appDomainType?.GetMethod("GetData")?.Invoke(currentDomain, new[] { "APP_CONTEXT_DEPS_FILES" });

View file

@ -16,6 +16,14 @@ namespace Microsoft.Extensions.DependencyModel
{ {
public void Write(DependencyContext context, Stream stream) public void Write(DependencyContext context, Stream stream)
{ {
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
using (var writer = new StreamWriter(stream)) using (var writer = new StreamWriter(stream))
{ {
using (var jsonWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented }) using (var jsonWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented })

View file

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
@ -10,6 +11,27 @@ namespace Microsoft.Extensions.DependencyModel
{ {
public Library(string type, string name, string version, string hash, IEnumerable<Dependency> dependencies, bool serviceable) public Library(string type, string name, string version, string hash, IEnumerable<Dependency> dependencies, bool serviceable)
{ {
if (string.IsNullOrEmpty(type))
{
throw new ArgumentException(nameof(type));
}
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(nameof(name));
}
if (string.IsNullOrEmpty(version))
{
throw new ArgumentException(nameof(version));
}
// Hash could be empty for projects
if (hash == null)
{
throw new ArgumentException(nameof(hash));
}
if (dependencies == null)
{
throw new ArgumentNullException(nameof(dependencies));
}
Type = type; Type = type;
Name = name; Name = name;
Version = version; Version = version;

View file

@ -1,12 +1,22 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
namespace Microsoft.Extensions.DependencyModel namespace Microsoft.Extensions.DependencyModel
{ {
public class ResourceAssembly public class ResourceAssembly
{ {
public ResourceAssembly(string path, string locale) public ResourceAssembly(string path, string locale)
{ {
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(nameof(path));
}
if (string.IsNullOrEmpty(locale))
{
throw new ArgumentException(nameof(locale));
}
Locale = locale; Locale = locale;
Path = path; Path = path;
} }

View file

@ -15,6 +15,14 @@ namespace Microsoft.Extensions.DependencyModel
public RuntimeAssembly(string assemblyName, string path) public RuntimeAssembly(string assemblyName, string path)
{ {
if (string.IsNullOrEmpty(assemblyName))
{
throw new ArgumentException(nameof(assemblyName));
}
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(nameof(path));
}
_assemblyName = assemblyName; _assemblyName = assemblyName;
Path = path; Path = path;
} }

View file

@ -14,9 +14,9 @@ namespace Microsoft.Extensions.DependencyModel
public RuntimeFallbacks(string runtime, IEnumerable<string> fallbacks) public RuntimeFallbacks(string runtime, IEnumerable<string> fallbacks)
{ {
if (runtime == null) if (string.IsNullOrEmpty(runtime))
{ {
throw new ArgumentNullException(nameof(runtime)); throw new ArgumentException(nameof(runtime));
} }
if (fallbacks == null) if (fallbacks == null)
{ {

View file

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -21,6 +22,22 @@ namespace Microsoft.Extensions.DependencyModel
bool serviceable) bool serviceable)
: base(type, name, version, hash, dependencies, serviceable) : base(type, name, version, hash, dependencies, serviceable)
{ {
if (assemblies == null)
{
throw new ArgumentNullException(nameof(assemblies));
}
if (nativeLibraries == null)
{
throw new ArgumentNullException(nameof(nativeLibraries));
}
if (resourceAssemblies == null)
{
throw new ArgumentNullException(nameof(resourceAssemblies));
}
if (subTargets == null)
{
throw new ArgumentNullException(nameof(subTargets));
}
Assemblies = assemblies.ToArray(); Assemblies = assemblies.ToArray();
ResourceAssemblies = resourceAssemblies.ToArray(); ResourceAssemblies = resourceAssemblies.ToArray();
RuntimeTargets = subTargets.ToArray(); RuntimeTargets = subTargets.ToArray();
@ -28,8 +45,8 @@ namespace Microsoft.Extensions.DependencyModel
} }
public IReadOnlyList<RuntimeAssembly> Assemblies { get; } public IReadOnlyList<RuntimeAssembly> Assemblies { get; }
public IReadOnlyList<string> NativeLibraries { get; } public IReadOnlyList<string> NativeLibraries { get; }
public IReadOnlyList<ResourceAssembly> ResourceAssemblies { get; } public IReadOnlyList<ResourceAssembly> ResourceAssemblies { get; }

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -7,6 +8,18 @@ namespace Microsoft.Extensions.DependencyModel
{ {
public RuntimeTarget(string runtime, IEnumerable<RuntimeAssembly> assemblies, IEnumerable<string> nativeLibraries) public RuntimeTarget(string runtime, IEnumerable<RuntimeAssembly> assemblies, IEnumerable<string> nativeLibraries)
{ {
if (string.IsNullOrEmpty(runtime))
{
throw new ArgumentException(nameof(runtime));
}
if (assemblies == null)
{
throw new ArgumentNullException(nameof(assemblies));
}
if (nativeLibraries == null)
{
throw new ArgumentNullException(nameof(nativeLibraries));
}
Runtime = runtime; Runtime = runtime;
Assemblies = assemblies.ToArray(); Assemblies = assemblies.ToArray();
NativeLibraries = nativeLibraries.ToArray(); NativeLibraries = nativeLibraries.ToArray();