You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by dp...@apache.org on 2017/10/31 19:03:25 UTC

[02/12] logging-log4net git commit: Add validation for logger names

Add validation for logger names

* a logger name must not be null or empty
* a logger name must not be prefixed or suffixed by whitespaces
* each part of a logger name split by a dot must not be null or empty
* each part of a logger name split by a dot must not be prefixed or suffixed by whitespaces

This change relates to LOG4NET-580.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4net/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4net/commit/3c4de1c2
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4net/tree/3c4de1c2
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4net/diff/3c4de1c2

Branch: refs/heads/develop
Commit: 3c4de1c2337bbee0b44ef4e3dfded56aff6a2218
Parents: f692965
Author: Dominik Psenner <dp...@apache.org>
Authored: Tue Oct 31 14:58:00 2017 +0100
Committer: Dominik Psenner <dp...@apache.org>
Committed: Tue Oct 31 15:04:26 2017 +0100

----------------------------------------------------------------------
 src/Repository/Hierarchy/Hierarchy.cs | 42 ++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4net/blob/3c4de1c2/src/Repository/Hierarchy/Hierarchy.cs
----------------------------------------------------------------------
diff --git a/src/Repository/Hierarchy/Hierarchy.cs b/src/Repository/Hierarchy/Hierarchy.cs
index 7d71872..37965e8 100644
--- a/src/Repository/Hierarchy/Hierarchy.cs
+++ b/src/Repository/Hierarchy/Hierarchy.cs
@@ -726,11 +726,22 @@ namespace log4net.Repository.Hierarchy
 			{
 				throw new ArgumentNullException("name");
 			}
+
 			if (factory == null)
 			{
 				throw new ArgumentNullException("factory");
 			}
 
+			if (name == string.Empty)
+			{
+				throw new ArgumentException(nameof(name), "Invalid logger name: a logger name must not be empty.");
+			}
+
+			if (name != name.Trim())
+			{
+				throw new ArgumentException(nameof(name), "Invalid logger name: a logger name must not be prefixed or suffixed by whitespaces.");
+			}
+
 			LoggerKey key = new LoggerKey(name);
 
 			// Synchronize to prevent write conflicts. Read conflicts (in
@@ -840,6 +851,37 @@ namespace log4net.Repository.Hierarchy
 			int length = name.Length;
 			bool parentFound = false;
 
+			if (name == null)
+			{
+				throw new ArgumentException(nameof(log), "Invalid logger name: a logger name must not be null.");
+			}
+
+			if (name == string.Empty)
+			{
+				throw new ArgumentException(nameof(log), "Invalid logger name: a logger name must not be empty.");
+			}
+
+			if (name != name.Trim())
+			{
+				throw new ArgumentException(nameof(log), "Invalid logger name: a logger name must not be prefixed or suffixed with whitespaces.");
+			}
+
+			string[] nameParts = name.Split('.');
+			for (int i = 0; i < nameParts.Length; i++)
+			{
+				string namePart = nameParts[i];
+
+				if (namePart == string.Empty)
+				{
+					throw new ArgumentException(nameof(log), "Invalid logger name: a logger name must not contain a substring of two dots or more.");
+				}
+
+				if (namePart != namePart.Trim())
+				{
+					throw new ArgumentException(nameof(log), "Invalid logger name: each part in a logger name when split by a dot must not be prefixed or suffixed with whitespaces.");
+				}
+			}
+
 			// if name = "w.x.y.z", loop through "w.x.y", "w.x" and "w", but not "w.x.y.z"
 			for (int i = name.LastIndexOf('.', length - 1); i >= 0; i = name.LastIndexOf('.', i - 1))
 			{