Author: hashutosh
Date: Fri Dec  6 15:01:59 2013
New Revision: 1548551

URL: http://svn.apache.org/r1548551
Log:
HIVE-5909 : locate and instr throw java.nio.BufferUnderflowException when empty 
string as substring (Navis via Ashutosh Chauhan)

Added:
    
hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFUtils.java
Modified:
    
hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUtils.java

Modified: 
hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUtils.java
URL: 
http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUtils.java?rev=1548551&r1=1548550&r2=1548551&view=diff
==============================================================================
--- 
hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUtils.java
 (original)
+++ 
hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUtils.java
 Fri Dec  6 15:01:59 2013
@@ -452,7 +452,14 @@ public final class GenericUDFUtils {
    */
   public static int findText(Text text, Text subtext, int start) {
     // src.position(start) can't accept negative numbers.
-    if (start < 0) {
+    int length = text.getLength() - start;
+    if (start < 0 || length < 0 || length < subtext.getLength()) {
+      return -1;
+    }
+    if (subtext.getLength() == 0) {
+      return 0;
+    }
+    if (length == 0) {
       return -1;
     }
 

Added: 
hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFUtils.java
URL: 
http://svn.apache.org/viewvc/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFUtils.java?rev=1548551&view=auto
==============================================================================
--- 
hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFUtils.java 
(added)
+++ 
hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFUtils.java 
Fri Dec  6 15:01:59 2013
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hive.ql.udf;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils;
+import org.apache.hadoop.io.Text;
+import org.junit.Test;
+
+public class TestGenericUDFUtils extends TestCase {
+
+  @Test
+  public void testFindText() throws Exception {
+
+    // 
http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_locate
+    Assert.assertEquals(3, GenericUDFUtils.findText(new Text("foobarbar"), new 
Text("bar"), 0));
+    Assert.assertEquals(-1, GenericUDFUtils.findText(new Text("foobarbar"), 
new Text("xbar"), 0));
+    Assert.assertEquals(6, GenericUDFUtils.findText(new Text("foobarbar"), new 
Text("bar"), 5));
+
+    Assert.assertEquals(6, GenericUDFUtils.findText(new Text("foobarbar"), new 
Text("bar"), 5));
+    Assert.assertEquals(6, GenericUDFUtils.findText(new Text("foobarbar"), new 
Text("bar"), 6));
+    Assert.assertEquals(-1, GenericUDFUtils.findText(new Text("foobarbar"), 
new Text("bar"), 7));
+    Assert.assertEquals(-1, GenericUDFUtils.findText(new Text("foobarbar"), 
new Text("bar"), 10));
+
+    Assert.assertEquals(-1, GenericUDFUtils.findText(new Text(""), new 
Text("bar"), 0));
+    Assert.assertEquals(0, GenericUDFUtils.findText(new Text(""), new 
Text(""), 0));
+    Assert.assertEquals(0, GenericUDFUtils.findText(new Text("foobar"), new 
Text(""), 0));
+    Assert.assertEquals(0, GenericUDFUtils.findText(new Text("foobar"), new 
Text(""), 6));
+    Assert.assertEquals(-1, GenericUDFUtils.findText(new Text("foobar"), new 
Text(""), 7));
+  }
+}


Reply via email to