Skip to content

Commit

Permalink
Merge pull request hyperledger-web3j#585 from argentlabs/fix_NPE_issu…
Browse files Browse the repository at this point in the history
…e402

fixed NPE when calling a view function in a contract that returns an out-of-gas error, issue 402
  • Loading branch information
conor10 authored Jun 11, 2018
2 parents 5827382 + 8480736 commit f28bc43
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion abi/src/main/java/org/web3j/abi/FunctionReturnDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Bytes32;
import org.web3j.utils.Numeric;
import org.web3j.utils.Strings;

import static org.web3j.abi.TypeDecoder.MAX_BYTE_LENGTH_FOR_HEX_STRING;

Expand All @@ -36,7 +37,7 @@ public static List<Type> decode(
String rawInput, List<TypeReference<Type>> outputParameters) {
String input = Numeric.cleanHexPrefix(rawInput);

if (input.isEmpty()) {
if (Strings.isEmpty(input)) {
return Collections.emptyList();
} else {
return build(input, outputParameters);
Expand Down
3 changes: 2 additions & 1 deletion utils/src/main/java/org/web3j/utils/Numeric.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public static String prependHexPrefix(String input) {
}

public static boolean containsHexPrefix(String input) {
return input.length() > 1 && input.charAt(0) == '0' && input.charAt(1) == 'x';
return !Strings.isEmpty(input) && input.length() > 1
&& input.charAt(0) == '0' && input.charAt(1) == 'x';
}

public static BigInteger toBigInt(byte[] value, int offset, int length) {
Expand Down
6 changes: 6 additions & 0 deletions utils/src/test/java/org/web3j/utils/NumericTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,10 @@ public void testIsIntegerValue() {
assertFalse(Numeric.isIntegerValue(BigDecimal.valueOf(1.1)));
assertFalse(Numeric.isIntegerValue(BigDecimal.valueOf(-1.1)));
}

@Test
public void testHandleNPE() {
assertFalse(Numeric.containsHexPrefix(null));
assertFalse(Numeric.containsHexPrefix(""));
}
}

0 comments on commit f28bc43

Please sign in to comment.