{"thread":{"id":"17195","subject":"[JGIT PATCH] Fix RawParseUtils to match on the end of the buffer","startedAt":"2009-01-15T17:02:54Z","lastAt":"2009-01-15T17:02:54Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"100634","messageId":"1232038974-19638-1-git-send-email-spearce@spearce.org","threadId":"17195","inReplyTo":null,"subject":"[JGIT PATCH] Fix RawParseUtils to match on the end of the buffer","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-01-15T17:02:54Z","receivedAt":"2009-01-15T17:02:54Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"When we were matching exactly on the end of the buffer the match\nmethod was failing because it incorrectly tested for >= on the\nbuffer length.  We only needed to test for > the buffer length.\n\nJUnit tests for the match function are included to test for this\ncondition, and others.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../spearce/jgit/util/RawParseUtils_MatchTest.java |   69 ++++++++++++++++++++\n .../src/org/spearce/jgit/util/RawParseUtils.java   |    4 +-\n 2 files changed, 71 insertions(+), 2 deletions(-)\n create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_MatchTest.java\n\ndiff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_MatchTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_MatchTest.java\nnew file mode 100644\nindex 0000000..32270b7\n--- /dev/null\n+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/util/RawParseUtils_MatchTest.java\n@@ -0,0 +1,69 @@\n+/*\n+ * Copyright (C) 2009, Google Inc.\n+ *\n+ * All rights reserved.\n+ *\n+ * Redistribution and use in source and binary forms, with or\n+ * without modification, are permitted provided that the following\n+ * conditions are met:\n+ *\n+ * - Redistributions of source code must retain the above copyright\n+ *   notice, this list of conditions and the following disclaimer.\n+ *\n+ * - Redistributions in binary form must reproduce the above\n+ *   copyright notice, this list of conditions and the following\n+ *   disclaimer in the documentation and/or other materials provided\n+ *   with the distribution.\n+ *\n+ * - Neither the name of the Git Development Community nor the\n+ *   names of its contributors may be used to endorse or promote\n+ *   products derived from this software without specific prior\n+ *   written permission.\n+ *\n+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\n+ * CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES,\n+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+\n+package org.spearce.jgit.util;\n+\n+import junit.framework.TestCase;\n+\n+import org.spearce.jgit.lib.Constants;\n+\n+public class RawParseUtils_MatchTest extends TestCase {\n+\tpublic void testMatch_Equal() {\n+\t\tfinal byte[] src = Constants.encodeASCII(\" differ\\n\");\n+\t\tfinal byte[] dst = Constants.encodeASCII(\"foo differ\\n\");\n+\t\tassertTrue(RawParseUtils.match(dst, 3, src) == 3 + src.length);\n+\t}\n+\n+\tpublic void testMatch_NotEqual() {\n+\t\tfinal byte[] src = Constants.encodeASCII(\" differ\\n\");\n+\t\tfinal byte[] dst = Constants.encodeASCII(\"a differ\\n\");\n+\t\tassertTrue(RawParseUtils.match(dst, 2, src) < 0);\n+\t}\n+\n+\tpublic void testMatch_Prefix() {\n+\t\tfinal byte[] src = Constants.encodeASCII(\"author \");\n+\t\tfinal byte[] dst = Constants.encodeASCII(\"author A. U. Thor\");\n+\t\tassertTrue(RawParseUtils.match(dst, 0, src) == src.length);\n+\t\tassertTrue(RawParseUtils.match(dst, 1, src) < 0);\n+\t}\n+\n+\tpublic void testMatch_TooSmall() {\n+\t\tfinal byte[] src = Constants.encodeASCII(\"author \");\n+\t\tfinal byte[] dst = Constants.encodeASCII(\"author autho\");\n+\t\tassertTrue(RawParseUtils.match(dst, src.length + 1, src) < 0);\n+\t}\n+}\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java\nindex a6a50bb..758e7af 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java\n@@ -71,10 +71,10 @@\n \t *            first position within b, this should match src[0].\n \t * @param src\n \t *            the buffer to test for equality with b.\n-\t * @return ptr += src.length if b[ptr..src.length] == src; else -1.\n+\t * @return ptr + src.length if b[ptr..src.length] == src; else -1.\n \t */\n \tpublic static final int match(final byte[] b, int ptr, final byte[] src) {\n-\t\tif (ptr + src.length >= b.length)\n+\t\tif (ptr + src.length > b.length)\n \t\t\treturn -1;\n \t\tfor (int i = 0; i < src.length; i++, ptr++)\n \t\t\tif (b[ptr] != src[i])\n-- \n1.6.1.233.g5b4a8\n"}]}