fix: Fix ArrayOutOfBound exception when creating YggAddress object

This commit is contained in:
ChronosX88 2019-08-22 16:14:18 +04:00
parent 8e160b1dcd
commit e604759ccd
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
2 changed files with 113 additions and 5 deletions

View File

@ -3,6 +3,115 @@
<JetCodeStyleSettings>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<codeStyleSettings language="XML">
<arrangement>
<rules>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:android</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:id</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>style</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
<order>ANDROID_ATTRIBUTE_ORDER</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>.*</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
</rules>
</arrangement>
</codeStyleSettings>
<codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</codeStyleSettings>

View File

@ -1,9 +1,8 @@
package io.github.chronosx88.yggdrasil.address;
import java.io.ByteArrayOutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class YggAddress {
private static final short IPV6_ADDRESS_LENGTH = 16; // represents an IPv6 address in the yggdrasil address range.
@ -14,7 +13,7 @@ public class YggAddress {
public YggAddress(NodeID nodeID) {
addressBytes = new byte[IPV6_ADDRESS_LENGTH];
List<Byte> temp = new ArrayList<>();
ByteArrayOutputStream temp = new ByteArrayOutputStream();
boolean done = false;
byte ones = 0;
byte bits = 0;
@ -33,14 +32,14 @@ public class YggAddress {
nBits++;
if(nBits == 8) {
nBits = 0;
temp.add(bits);
temp.write(bits);
}
}
byte[] prefix = getPrefix();
System.arraycopy(prefix, 0, addressBytes, 0, prefix.length);
addressBytes[prefix.length] = ones;
System.arraycopy(temp.toArray(new Byte[0]), 0, addressBytes, prefix.length+1, temp.size());
System.arraycopy(temp.toByteArray(), 0, addressBytes, prefix.length+1, addressBytes.length-(prefix.length+1));
try {
address = InetAddress.getByAddress(addressBytes);
} catch (UnknownHostException e) {