Skip to content

Commit d2fb05f

Browse files
committed
[SPARK-8301] added additional null checks
1 parent 79cb55b commit d2fb05f

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ public static UTF8String fromString(String str) {
5656
* Updates the UTF8String with String.
5757
*/
5858
public UTF8String set(final String str) {
59-
try {
60-
bytes = str.getBytes("utf-8");
61-
} catch (UnsupportedEncodingException e) {
62-
// Turn the exception into unchecked so we can find out about it at runtime, but
63-
// don't need to add lots of boilerplate code everywhere.
64-
PlatformDependent.throwException(e);
59+
if (str == null) {
60+
bytes = new byte[0];
61+
} else {
62+
try {
63+
bytes = str.getBytes("utf-8");
64+
} catch (UnsupportedEncodingException e) {
65+
// Turn the exception into unchecked so we can find out about it at runtime, but
66+
// don't need to add lots of boilerplate code everywhere.
67+
PlatformDependent.throwException(e);
68+
}
6569
}
6670
return this;
6771
}
@@ -70,7 +74,7 @@ public UTF8String set(final String str) {
7074
* Updates the UTF8String with byte[], which should be encoded in UTF-8.
7175
*/
7276
public UTF8String set(final byte[] bytes) {
73-
this.bytes = bytes;
77+
this.bytes = (bytes != null) ? bytes : new byte[0];
7478
return this;
7579
}
7680

@@ -185,6 +189,7 @@ public UTF8String clone() {
185189

186190
@Override
187191
public int compareTo(final UTF8String other) {
192+
if (other == null) return 1;
188193
final byte[] b = other.getBytes();
189194
for (int i = 0; i < bytes.length && i < b.length; i++) {
190195
int res = bytes[i] - b[i];

0 commit comments

Comments
 (0)