From 665b3fd285a4b09bdee125f77e7714f9ba11192f Mon Sep 17 00:00:00 2001 From: Dustin Pho Date: Mon, 12 Sep 2016 13:25:02 -0700 Subject: [PATCH] Adding checks in Scanner's setStartRow and setStopRow for invalid row key sizes. --- .../java/org/apache/hadoop/hbase/client/Scan.java | 16 +++++++++++++ .../org/apache/hadoop/hbase/client/TestScan.java | 28 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Scan.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Scan.java index ee3ed43..f318eda 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Scan.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Scan.java @@ -373,8 +373,16 @@ public class Scan extends Query { * next closest row after the specified row. * @param startRow row to start scanner at or after * @return this + * @throws IllegalArgumentException if startRow does not meet criteria + * for a row key (length exceeds MAX_ROW_LENGTH) */ public Scan setStartRow(byte [] startRow) { + if (Bytes.len(startRow) > HConstants.MAX_ROW_LENGTH) { + throw new IllegalArgumentException( + "startRow's length must be less than or equal to " + + HConstants.MAX_ROW_LENGTH + " to meet the criteria" + + " for a row key."); + } this.startRow = startRow; return this; } @@ -389,8 +397,16 @@ public class Scan extends Query { * use {@link #setRowPrefixFilter(byte[])}. * The 'trailing 0' will not yield the desired result.

* @return this + * @throws IllegalArgumentException if stopRow does not meet criteria + * for a row key (length exceeds MAX_ROW_LENGTH) */ public Scan setStopRow(byte [] stopRow) { + if (Bytes.len(stopRow) > HConstants.MAX_ROW_LENGTH) { + throw new IllegalArgumentException( + "stopRow's length must be less than or equal to " + + HConstants.MAX_ROW_LENGTH + " to meet the criteria" + + " for a row key."); + } this.stopRow = stopRow; return this; } diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestScan.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestScan.java index 129914f..16c74df 100644 --- a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestScan.java +++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestScan.java @@ -25,6 +25,8 @@ import java.io.IOException; import java.util.Arrays; import java.util.Set; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.exceptions.IllegalArgumentIOException; import org.apache.hadoop.hbase.protobuf.ProtobufUtil; import org.apache.hadoop.hbase.protobuf.generated.ClientProtos; import org.apache.hadoop.hbase.security.visibility.Authorizations; @@ -132,5 +134,31 @@ public class TestScan { fail("should not throw exception"); } } + + @Test + public void testSetStartRowAndSetStopRow() { + Scan scan = new Scan(); + scan.setStartRow(null); + scan.setStartRow(new byte[1]); + scan.setStartRow(new byte[HConstants.MAX_ROW_LENGTH]); + try { + scan.setStartRow(new byte[HConstants.MAX_ROW_LENGTH+1]); + fail("should've thrown exception"); + } catch (IllegalArgumentException iae) { + } catch (Exception e) { + fail("expected IllegalArgumentException to be thrown"); + } + + scan.setStopRow(null); + scan.setStopRow(new byte[1]); + scan.setStopRow(new byte[HConstants.MAX_ROW_LENGTH]); + try { + scan.setStopRow(new byte[HConstants.MAX_ROW_LENGTH+1]); + fail("should've thrown exception"); + } catch (IllegalArgumentException iae) { + } catch (Exception e) { + fail("expected IllegalArgumentException to be thrown"); + } + } } -- 2.8.0-rc2