git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 11/12] Cleanup Config's MAGIC_EMPTY_VALUE to be more safe

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jul 21, 2009, 20:19 UTC
Message-ID
<1248207570-13880-12-git-send-email-spearce@spearce.org>
In-Reply-To
<1248207570-13880-11-git-send-email-spearce@spearce.org>

The magic value "%%magic%%empty%%" is just too magic; if it ever did appear as a value in a key string Config would have treated it as a true value instead of as a string value. We also had to special case conversions of it to the empty string in a string context. Instead we create a special String object using the empty string as a template, and use reference equality against that to indicate the magic empty value.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/lib/Config.java           |   28 +++++++++----------
 1 files changed, 13 insertions(+), 15 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Config.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Config.java
index 974ffea..e4528b1 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Config.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Config.java
@@ -76,9 +76,14 @@
 	private Map<String, Object> byName;
 
 	/**
-	 * Magic value indicating a missing entry
+	 * Magic value indicating a missing entry.
+	 * <p>
+	 * This value is tested for reference equality in some contexts, so we
+	 * must ensure it is a special copy of the empty string.  It also must
+	 * be treated like the empty string.
 	 */
-	private static final String MAGIC_EMPTY_VALUE = "%%magic%%empty%%";
+	private static final String MAGIC_EMPTY_VALUE = new StringBuilder(0)
+			.toString();
 
 	/**
 	 * The constructor for configuration file
@@ -293,7 +298,7 @@ public boolean getBoolean(final String section, String subsection,
 		if (n == null)
 			return defaultValue;
 
-		if (MAGIC_EMPTY_VALUE.equals(n) || "yes".equalsIgnoreCase(n)
+		if (MAGIC_EMPTY_VALUE == n || "yes".equalsIgnoreCase(n)
 				|| "true".equalsIgnoreCase(n) || "1".equals(n)
 				|| "on".equalsIgnoreCase(n)) {
 			return true;
@@ -321,11 +326,7 @@ public boolean getBoolean(final String section, String subsection,
 	 */
 	public String getString(final String section, String subsection,
 			final String name) {
-		String val = getRawString(section, subsection, name);
-		if (MAGIC_EMPTY_VALUE.equals(val)) {
-			return "";
-		}
-		return val;
+		return getRawString(section, subsection, name);
 	}
 
 	/**
@@ -345,16 +346,13 @@ public String getString(final String section, String subsection,
 		if (o instanceof List) {
 			final List lst = (List) o;
 			final String[] r = new String[lst.size()];
-			for (int i = 0; i < r.length; i++) {
-				final String val = ((Entry) lst.get(i)).value;
-				r[i] = MAGIC_EMPTY_VALUE.equals(val) ? "" : val;
-			}
+			for (int i = 0; i < r.length; i++)
+				r[i] = ((Entry) lst.get(i)).value;
 			return r;
 		}
 
 		if (o instanceof Entry) {
-			final String val = ((Entry) o).value;
-			return new String[] { MAGIC_EMPTY_VALUE.equals(val) ? "" : val };
+			return new String[] { ((Entry) o).value };
 		}
 
 		if (baseConfig != null)
@@ -700,7 +698,7 @@ protected void printConfig(final PrintWriter r) {
 				}
 				r.print(e.name);
 				if (e.value != null) {
-					if (!MAGIC_EMPTY_VALUE.equals(e.value)) {
+					if (MAGIC_EMPTY_VALUE != e.value) {
 						r.print(" = ");
 						r.print(escapeValue(e.value));
 					}
-- 
1.6.4.rc1.186.g60aa0c
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 12 of 18 in “Cleanup Config class”
  1. 00/12 Cleanup Config classShawn O. Pearce, Jul 21, 2009
  2. 01/12 Use NB.readFully(File) to slurp complete file contentsShawn O. Pearce, Jul 21, 2009
  3. 02/12 Correct name of fileRead member of Config classShawn O. Pearce, Jul 21, 2009
  4. 03/12 Add setLong to ConfigShawn O. Pearce, Jul 21, 2009
  5. 04/12 Fix Config setInt(..., 0) to store "0" not "0 g"Shawn O. Pearce, Jul 21, 2009
  6. 05/12 Rename Config.unsetString to just unset()Shawn O. Pearce, Jul 21, 2009
  7. 06/12 Remove pointless null assignments in ConfigShawn O. Pearce, Jul 21, 2009
  8. 07/12 Clarify section and subsection values in Config codeShawn O. Pearce, Jul 21, 2009
  9. 08/12 Don't subclass PrintWriter when writing the ConfigShawn O. Pearce, Jul 21, 2009
  10. 09/12 Use a Java 5 style iteration over the Config entries listShawn O. Pearce, Jul 21, 2009
  11. 10/12 Match config subsection names using case sensitive searchShawn O. Pearce, Jul 21, 2009
  12. 11/12 Cleanup Config's MAGIC_EMPTY_VALUE to be more safeShawn O. Pearce, Jul 21, 2009
  13. 12/12 Remove unreferenced REMOTE_SECTION from RepositoryConfigShawn O. Pearce, Jul 21, 2009
  14. Robin RosenbergJul 21, 2009
  15. Shawn O. PearceJul 21, 2009
  16. Constantine PlotnikovJul 22, 2009
  17. Robin RosenbergJul 22, 2009
  18. Ensure Config readers handle case insensitive names correctlyShawn O. Pearce, Jul 24, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.