global: Cleanup line lengths and misc lint warnings

I know we decided to ditch the idea of shutting up "Exception
thrown with empty param" warnings but this pesters me too
much and we can instead just treat this as a weird future proofing
thing if and when we end up needing the exception messages.

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2018-05-05 16:26:52 +05:30 committed by Jason A. Donenfeld
parent e2ab2210cd
commit 0b9bcf0f9e
10 changed files with 85 additions and 71 deletions

View File

@ -10,6 +10,7 @@ import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat; import android.support.v7.preference.PreferenceFragmentCompat;
@ -36,7 +37,8 @@ public class SettingsActivity extends AppCompatActivity {
public void ensurePermissions(final String[] permissions, final PermissionRequestCallback cb) { public void ensurePermissions(final String[] permissions, final PermissionRequestCallback cb) {
final List<String> needPermissions = new ArrayList<>(permissions.length); final List<String> needPermissions = new ArrayList<>(permissions.length);
for (final String permission : permissions) { for (final String permission : permissions) {
if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED)
needPermissions.add(permission); needPermissions.add(permission);
} }
if (needPermissions.isEmpty()) { if (needPermissions.isEmpty()) {
@ -47,7 +49,8 @@ public class SettingsActivity extends AppCompatActivity {
} }
final int idx = permissionRequestCounter++; final int idx = permissionRequestCounter++;
permissionRequestCallbacks.put(idx, cb); permissionRequestCallbacks.put(idx, cb);
ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), idx); ActivityCompat.requestPermissions(this,
needPermissions.toArray(new String[needPermissions.size()]), idx);
} }
@Override @Override
@ -72,7 +75,9 @@ public class SettingsActivity extends AppCompatActivity {
} }
@Override @Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { public void onRequestPermissionsResult(final int requestCode,
@NonNull final String[] permissions,
@NonNull final int[] grantResults) {
final PermissionRequestCallback f = permissionRequestCallbacks.get(requestCode); final PermissionRequestCallback f = permissionRequestCallbacks.get(requestCode);
if (f != null) { if (f != null) {
permissionRequestCallbacks.remove(requestCode); permissionRequestCallbacks.remove(requestCode);

View File

@ -109,7 +109,8 @@ public final class WgQuickBackend implements Backend {
try (FileOutputStream stream = new FileOutputStream(tempFile, false)) { try (FileOutputStream stream = new FileOutputStream(tempFile, false)) {
stream.write(config.toString().getBytes(StandardCharsets.UTF_8)); stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
} }
final String command = String.format("wg-quick %s '%s'", state.toString().toLowerCase(), tempFile.getAbsolutePath()); final String command = String.format("wg-quick %s '%s'",
state.toString().toLowerCase(), tempFile.getAbsolutePath());
final int result = rootShell.run(null, command); final int result = rootShell.run(null, command);
// noinspection ResultOfMethodCallIgnored // noinspection ResultOfMethodCallIgnored
tempFile.delete(); tempFile.delete();

View File

@ -46,7 +46,7 @@ public class TunnelEditorFragment extends BaseFragment {
binding.setConfig(new Config.Observable(config, name)); binding.setConfig(new Config.Observable(config, name));
} }
private void onConfigSaved(final Tunnel savedTunnel, final Config config, private void onConfigSaved(final Tunnel savedTunnel,
final Throwable throwable) { final Throwable throwable) {
final String message; final String message;
if (throwable == null) { if (throwable == null) {
@ -141,7 +141,7 @@ public class TunnelEditorFragment extends BaseFragment {
} else { } else {
Log.d(TAG, "Attempting to save config of " + tunnel.getName()); Log.d(TAG, "Attempting to save config of " + tunnel.getName());
tunnel.setConfig(newConfig) tunnel.setConfig(newConfig)
.whenComplete((a, b) -> onConfigSaved(tunnel, a, b)); .whenComplete((a, b) -> onConfigSaved(tunnel, b));
} }
return true; return true;
default: default:
@ -192,7 +192,7 @@ public class TunnelEditorFragment extends BaseFragment {
Log.d(TAG, message); Log.d(TAG, message);
// Now save the rest of configuration changes. // Now save the rest of configuration changes.
Log.d(TAG, "Attempting to save config of renamed tunnel " + tunnel.getName()); Log.d(TAG, "Attempting to save config of renamed tunnel " + tunnel.getName());
renamedTunnel.setConfig(newConfig).whenComplete((a, b) -> onConfigSaved(renamedTunnel, a, b)); renamedTunnel.setConfig(newConfig).whenComplete((a, b) -> onConfigSaved(renamedTunnel, b));
} else { } else {
final String error = ExceptionLoggers.unwrap(throwable).getMessage(); final String error = ExceptionLoggers.unwrap(throwable).getMessage();
message = getString(R.string.tunnel_rename_error, error); message = getString(R.string.tunnel_rename_error, error);

View File

@ -94,7 +94,8 @@ public class TunnelListFragment extends BaseFragment {
asyncWorker.supplyAsync(() -> { asyncWorker.supplyAsync(() -> {
final String[] columns = {OpenableColumns.DISPLAY_NAME}; final String[] columns = {OpenableColumns.DISPLAY_NAME};
String name = null; String name = null;
try (Cursor cursor = contentResolver.query(uri, columns, null, null, null)) { try (Cursor cursor = contentResolver.query(uri, columns,
null, null, null)) {
if (cursor != null && cursor.moveToFirst() && !cursor.isNull(0)) if (cursor != null && cursor.moveToFirst() && !cursor.isNull(0))
name = cursor.getString(0); name = cursor.getString(0);
} }

View File

@ -19,6 +19,7 @@ import android.widget.TextView;
public final class ClipboardUtils { public final class ClipboardUtils {
private ClipboardUtils() { private ClipboardUtils() {
// Prevent instantiation
} }
public static void copyTextView(final View view) { public static void copyTextView(final View view) {

View File

@ -25,28 +25,28 @@ public class ObservableKeyedArrayList<K, E extends Keyed<? extends K>>
@Override @Override
public boolean add(final E e) { public boolean add(final E e) {
if (e == null) if (e == null)
throw new NullPointerException(); throw new NullPointerException("Trying to add a null element");
return super.add(e); return super.add(e);
} }
@Override @Override
public void add(final int index, final E e) { public void add(final int index, final E e) {
if (e == null) if (e == null)
throw new NullPointerException(); throw new NullPointerException("Trying to add a null element");
super.add(index, e); super.add(index, e);
} }
@Override @Override
public boolean addAll(@NonNull final Collection<? extends E> c) { public boolean addAll(@NonNull final Collection<? extends E> c) {
if (c.contains(null)) if (c.contains(null))
throw new NullPointerException(); throw new NullPointerException("Trying to add a collection with null element(s)");
return super.addAll(c); return super.addAll(c);
} }
@Override @Override
public boolean addAll(final int index, @NonNull final Collection<? extends E> c) { public boolean addAll(final int index, @NonNull final Collection<? extends E> c) {
if (c.contains(null)) if (c.contains(null))
throw new NullPointerException(); throw new NullPointerException("Trying to add a collection with null element(s)");
return super.addAll(index, c); return super.addAll(index, c);
} }
@ -100,7 +100,7 @@ public class ObservableKeyedArrayList<K, E extends Keyed<? extends K>>
@Override @Override
public E set(final int index, final E e) { public E set(final int index, final E e) {
if (e == null) if (e == null)
throw new NullPointerException(); throw new NullPointerException("Trying to set a null key");
return super.set(index, e); return super.set(index, e);
} }
} }

View File

@ -95,7 +95,9 @@ public class ObservableSortedKeyedArrayList<K, E extends Keyed<? extends K>>
@Override @Override
public K firstKey() { public K firstKey() {
if (isEmpty()) if (isEmpty())
throw new NoSuchElementException(); // The parameter in the exception is only to shut
// lint up, we never care for the exception message.
throw new NoSuchElementException("Empty set");
return get(0).getKey(); return get(0).getKey();
} }
@ -137,7 +139,9 @@ public class ObservableSortedKeyedArrayList<K, E extends Keyed<? extends K>>
@Override @Override
public K lastKey() { public K lastKey() {
if (isEmpty()) if (isEmpty())
throw new NoSuchElementException(); // The parameter in the exception is only to shut
// lint up, we never care for the exception message.
throw new NoSuchElementException("Empty set");
return get(size() - 1).getKey(); return get(size() - 1).getKey();
} }

View File

@ -50,7 +50,7 @@ public class RootShell {
final File cacheDir = context.getCacheDir(); final File cacheDir = context.getCacheDir();
localBinaryDir = new File(cacheDir, "bin"); localBinaryDir = new File(cacheDir, "bin");
localTemporaryDir = new File(cacheDir, "tmp"); localTemporaryDir = new File(cacheDir, "tmp");
preamble = String.format("export CALLING_PACKAGE=com.wireguard.android; export PATH=\"%s:$PATH\" TMPDIR='%s'; id -u\n", preamble = String.format("export CALLING_PACKAGE=com.wireguard.android PATH=\"%s:$PATH\" TMPDIR='%s'; id -u\n",
localBinaryDir, localTemporaryDir); localBinaryDir, localTemporaryDir);
} }

View File

@ -15,6 +15,7 @@ public class IPCidr {
private final InetAddress address; private final InetAddress address;
private int cidr; private int cidr;
@SuppressWarnings("MagicNumber")
public IPCidr(String in) { public IPCidr(String in) {
cidr = -1; cidr = -1;
final int slash = in.lastIndexOf('/'); final int slash = in.lastIndexOf('/');

View File

@ -21,28 +21,28 @@ import java.util.Arrays;
* <p> * <p>
* References: http://cr.yp.to/ecdh.html, RFC 7748 * References: http://cr.yp.to/ecdh.html, RFC 7748
*/ */
@SuppressWarnings("ALL") @SuppressWarnings("MagicNumber")
public final class Curve25519 { public final class Curve25519 {
// Numbers modulo 2^255 - 19 are broken up into ten 26-bit words. // Numbers modulo 2^255 - 19 are broken up into ten 26-bit words.
private static final int NUM_LIMBS_255BIT = 10; private static final int NUM_LIMBS_255BIT = 10;
private static final int NUM_LIMBS_510BIT = 20; private static final int NUM_LIMBS_510BIT = 20;
private int[] A; private final int[] A;
private int[] AA; private final int[] AA;
private int[] B; private final int[] B;
private int[] BB; private final int[] BB;
private int[] C; private final int[] C;
private int[] CB; private final int[] CB;
private int[] D; private final int[] D;
private int[] DA; private final int[] DA;
private int[] E; private final int[] E;
private long[] t1; private final long[] t1;
private int[] t2; private final int[] t2;
private int[] x_1; private final int[] x_1;
private int[] x_2; private final int[] x_2;
private int[] x_3; private final int[] x_3;
private int[] z_2; private final int[] z_2;
private int[] z_3; private final int[] z_3;
/** /**
* Constructs the temporary state holder for Curve25519 evaluation. * Constructs the temporary state holder for Curve25519 evaluation.
@ -74,11 +74,10 @@ public final class Curve25519 {
* @param x The first value. * @param x The first value.
* @param y The second value. * @param y The second value.
*/ */
private static void cswap(int select, int[] x, int[] y) { private static void cswap(int select, final int[] x, final int[] y) {
int dummy;
select = -select; select = -select;
for (int index = 0; index < NUM_LIMBS_255BIT; ++index) { for (int index = 0; index < NUM_LIMBS_255BIT; ++index) {
dummy = select & (x[index] ^ y[index]); final int dummy = select & (x[index] ^ y[index]);
x[index] ^= dummy; x[index] ^= dummy;
y[index] ^= dummy; y[index] ^= dummy;
} }
@ -93,17 +92,18 @@ public final class Curve25519 {
* @param publicKey The public key to use in the evaluation, or null * @param publicKey The public key to use in the evaluation, or null
* if the base point of the curve should be used. * if the base point of the curve should be used.
*/ */
public static void eval(byte[] result, int offset, byte[] privateKey, byte[] publicKey) { public static void eval(final byte[] result, final int offset,
Curve25519 state = new Curve25519(); final byte[] privateKey, final byte[] publicKey) {
final Curve25519 state = new Curve25519();
try { try {
// Unpack the public key value. If null, use 9 as the base point. // Unpack the public key value. If null, use 9 as the base point.
Arrays.fill(state.x_1, 0); Arrays.fill(state.x_1, 0);
if (publicKey != null) { if (publicKey != null) {
// Convert the input value from little-endian into 26-bit limbs. // Convert the input value from little-endian into 26-bit limbs.
for (int index = 0; index < 32; ++index) { for (int index = 0; index < 32; ++index) {
int bit = (index * 8) % 26; final int bit = (index * 8) % 26;
int word = (index * 8) / 26; final int word = (index * 8) / 26;
int value = publicKey[index] & 0xFF; final int value = publicKey[index] & 0xFF;
if (bit <= (26 - 8)) { if (bit <= (26 - 8)) {
state.x_1[word] |= value << bit; state.x_1[word] |= value << bit;
} else { } else {
@ -139,8 +139,8 @@ public final class Curve25519 {
// Convert x_2 into little-endian in the result buffer. // Convert x_2 into little-endian in the result buffer.
for (int index = 0; index < 32; ++index) { for (int index = 0; index < 32; ++index) {
int bit = (index * 8) % 26; final int bit = (index * 8) % 26;
int word = (index * 8) / 26; final int word = (index * 8) / 26;
if (bit <= (26 - 8)) if (bit <= (26 - 8))
result[offset + index] = (byte) (state.x_2[word] >> bit); result[offset + index] = (byte) (state.x_2[word] >> bit);
else else
@ -159,11 +159,11 @@ public final class Curve25519 {
* @param x The first number to add. * @param x The first number to add.
* @param y The second number to add. * @param y The second number to add.
*/ */
private void add(int[] result, int[] x, int[] y) { private void add(final int[] result, final int[] x, final int[] y) {
int index, carry; int carry;
carry = x[0] + y[0]; carry = x[0] + y[0];
result[0] = carry & 0x03FFFFFF; result[0] = carry & 0x03FFFFFF;
for (index = 1; index < NUM_LIMBS_255BIT; ++index) { for (int index = 1; index < NUM_LIMBS_255BIT; ++index) {
carry = (carry >> 26) + x[index] + y[index]; carry = (carry >> 26) + x[index] + y[index];
result[index] = carry & 0x03FFFFFF; result[index] = carry & 0x03FFFFFF;
} }
@ -198,19 +198,17 @@ public final class Curve25519 {
* *
* @param s The 32-byte secret key. * @param s The 32-byte secret key.
*/ */
private void evalCurve(byte[] s) { private void evalCurve(final byte[] s) {
int sposn = 31; int sposn = 31;
int sbit = 6;
int svalue = s[sposn] | 0x40; int svalue = s[sposn] | 0x40;
int swap = 0; int swap = 0;
int select;
// Iterate over all 255 bits of "s" from the highest to the lowest. // Iterate over all 255 bits of "s" from the highest to the lowest.
// We ignore the high bit of the 256-bit representation of "s". // We ignore the high bit of the 256-bit representation of "s".
for (; ; ) { for (int sbit = 6; ; ) {
// Conditional swaps on entry to this bit but only if we // Conditional swaps on entry to this bit but only if we
// didn't swap on the previous bit. // didn't swap on the previous bit.
select = (svalue >> sbit) & 0x01; final int select = (svalue >> sbit) & 0x01;
swap ^= select; swap ^= select;
cswap(swap, x_2, x_3); cswap(swap, x_2, x_3);
cswap(swap, z_2, z_3); cswap(swap, z_2, z_3);
@ -264,8 +262,8 @@ public final class Curve25519 {
* @param x The first number to multiply. * @param x The first number to multiply.
* @param y The second number to multiply. * @param y The second number to multiply.
*/ */
private void mul(int[] result, int[] x, int[] y) { private void mul(final int[] result, final int[] x, final int[] y) {
int i, j; int i;
// Multiply the two numbers to create the intermediate result. // Multiply the two numbers to create the intermediate result.
long v = x[0]; long v = x[0];
@ -274,7 +272,7 @@ public final class Curve25519 {
} }
for (i = 1; i < NUM_LIMBS_255BIT; ++i) { for (i = 1; i < NUM_LIMBS_255BIT; ++i) {
v = x[i]; v = x[i];
for (j = 0; j < (NUM_LIMBS_255BIT - 1); ++j) { for (int j = 0; j < (NUM_LIMBS_255BIT - 1); ++j) {
t1[i + j] += v * y[j]; t1[i + j] += v * y[j];
} }
t1[i + NUM_LIMBS_255BIT - 1] = v * y[NUM_LIMBS_255BIT - 1]; t1[i + NUM_LIMBS_255BIT - 1] = v * y[NUM_LIMBS_255BIT - 1];
@ -298,11 +296,10 @@ public final class Curve25519 {
* @param result The result. * @param result The result.
* @param x The number to multiply by a24. * @param x The number to multiply by a24.
*/ */
private void mulA24(int[] result, int[] x) { private void mulA24(final int[] result, final int[] x) {
long a24 = 121665; final long a24 = 121665;
long carry = 0; long carry = 0;
int index; for (int index = 0; index < NUM_LIMBS_255BIT; ++index) {
for (index = 0; index < NUM_LIMBS_255BIT; ++index) {
carry += a24 * x[index]; carry += a24 * x[index];
t2[index] = ((int) carry) & 0x03FFFFFF; t2[index] = ((int) carry) & 0x03FFFFFF;
carry >>= 26; carry >>= 26;
@ -317,8 +314,8 @@ public final class Curve25519 {
* @param result The result. Must not overlap with x. * @param result The result. Must not overlap with x.
* @param x The argument. * @param x The argument.
*/ */
private void pow250(int[] result, int[] x) { private void pow250(final int[] result, final int[] x) {
int i, j; int j;
// The big-endian hexadecimal expansion of (2^250 - 1) is: // The big-endian hexadecimal expansion of (2^250 - 1) is:
// 03FFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF // 03FFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
@ -335,7 +332,7 @@ public final class Curve25519 {
for (j = 0; j < 9; ++j) for (j = 0; j < 9; ++j)
square(A, A); square(A, A);
mul(result, A, x); mul(result, A, x);
for (i = 0; i < 23; ++i) { for (int i = 0; i < 23; ++i) {
for (j = 0; j < 10; ++j) for (j = 0; j < 10; ++j)
square(A, A); square(A, A);
mul(result, result, A); mul(result, result, A);
@ -357,7 +354,7 @@ public final class Curve25519 {
* @param result The result. Must not overlap with x. * @param result The result. Must not overlap with x.
* @param x The argument. * @param x The argument.
*/ */
private void recip(int[] result, int[] x) { private void recip(final int[] result, final int[] x) {
// The reciprocal is the same as x ^ (p - 2) where p = 2^255 - 19. // The reciprocal is the same as x ^ (p - 2) where p = 2^255 - 19.
// The big-endian hexadecimal expansion of (p - 2) is: // The big-endian hexadecimal expansion of (p - 2) is:
// 7FFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFEB // 7FFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFEB
@ -383,8 +380,10 @@ public final class Curve25519 {
* modified during the reduction. * modified during the reduction.
* @param size The number of limbs in the high order half of x. * @param size The number of limbs in the high order half of x.
*/ */
private void reduce(int[] result, int[] x, int size) { private void reduce(final int[] result, final int[] x, final int size) {
int index, limb, carry; int index;
int limb;
int carry;
// Calculate (x mod 2^255) + ((x / 2^255) * 19) which will // Calculate (x mod 2^255) + ((x / 2^255) * 19) which will
// either produce the answer we want or it will produce a // either produce the answer we want or it will produce a
@ -436,8 +435,9 @@ public final class Curve25519 {
* *
* @param x The number to reduce, and the result. * @param x The number to reduce, and the result.
*/ */
private void reduceQuick(int[] x) { private void reduceQuick(final int[] x) {
int index, carry; int index;
int carry;
// Perform a trial subtraction of (2^255 - 19) from "x" which is // Perform a trial subtraction of (2^255 - 19) from "x" which is
// equivalent to adding 19 and subtracting 2^255. We add 19 here; // equivalent to adding 19 and subtracting 2^255. We add 19 here;
@ -454,8 +454,8 @@ public final class Curve25519 {
// correct answer but do it in a way that instruction timing will not // correct answer but do it in a way that instruction timing will not
// reveal which value was selected. Borrow will occur if bit 21 of // reveal which value was selected. Borrow will occur if bit 21 of
// "t2" is zero. Turn the bit into a selection mask. // "t2" is zero. Turn the bit into a selection mask.
int mask = -((t2[NUM_LIMBS_255BIT - 1] >> 21) & 0x01); final int mask = -((t2[NUM_LIMBS_255BIT - 1] >> 21) & 0x01);
int nmask = ~mask; final int nmask = ~mask;
t2[NUM_LIMBS_255BIT - 1] &= 0x001FFFFF; t2[NUM_LIMBS_255BIT - 1] &= 0x001FFFFF;
for (index = 0; index < NUM_LIMBS_255BIT; ++index) for (index = 0; index < NUM_LIMBS_255BIT; ++index)
x[index] = (x[index] & nmask) | (t2[index] & mask); x[index] = (x[index] & nmask) | (t2[index] & mask);
@ -467,7 +467,7 @@ public final class Curve25519 {
* @param result The result. * @param result The result.
* @param x The number to square. * @param x The number to square.
*/ */
private void square(int[] result, int[] x) { private void square(final int[] result, final int[] x) {
mul(result, x, x); mul(result, x, x);
} }
@ -478,8 +478,9 @@ public final class Curve25519 {
* @param x The first number to subtract. * @param x The first number to subtract.
* @param y The second number to subtract. * @param y The second number to subtract.
*/ */
private void sub(int[] result, int[] x, int[] y) { private static void sub(final int[] result, final int[] x, final int[] y) {
int index, borrow; int index;
int borrow;
// Subtract y from x to generate the intermediate result. // Subtract y from x to generate the intermediate result.
borrow = 0; borrow = 0;