From 087c7b86dea44a4073481aeb5ef6f8f9474c7817 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Fri, 7 Jan 2011 00:15:35 +0000
Subject: [PATCH] findbugs core,client,crypto,data,stat,i2np

---
 core/java/src/net/i2p/I2PAppContext.java      |  8 ++-
 .../src/net/i2p/client/I2PSessionImpl.java    |  6 +--
 .../net/i2p/client/I2PSessionMuxedImpl.java   |  2 +-
 .../src/net/i2p/client/I2PSimpleSession.java  |  2 +-
 .../src/net/i2p/crypto/CryptixAESEngine.java  |  2 +-
 core/java/src/net/i2p/data/Base32.java        | 15 +++---
 core/java/src/net/i2p/data/Base64.java        | 15 +++---
 core/java/src/net/i2p/data/DataHelper.java    |  2 +-
 .../java/src/net/i2p/data/PrivateKeyFile.java | 54 ++++++++++++-------
 .../net/i2p/data/i2cp/DestReplyMessage.java   |  7 +--
 core/java/src/net/i2p/stat/FrequencyStat.java |  2 +-
 core/java/src/net/i2p/stat/Rate.java          |  2 +-
 core/java/src/net/i2p/stat/RateStat.java      |  2 +-
 .../net/i2p/data/i2np/I2NPMessageImpl.java    |  6 +--
 .../net/i2p/data/i2np/TunnelDataMessage.java  |  2 +-
 15 files changed, 76 insertions(+), 51 deletions(-)

diff --git a/core/java/src/net/i2p/I2PAppContext.java b/core/java/src/net/i2p/I2PAppContext.java
index e235511aa0..e5ec21c753 100644
--- a/core/java/src/net/i2p/I2PAppContext.java
+++ b/core/java/src/net/i2p/I2PAppContext.java
@@ -63,7 +63,7 @@ import net.i2p.util.SecureDirectory;
  */
 public class I2PAppContext {
     /** the context that components without explicit root are bound */
-    protected static I2PAppContext _globalAppContext;
+    protected static volatile I2PAppContext _globalAppContext;
     
     private Properties _overrideProps;
     
@@ -117,7 +117,8 @@ public class I2PAppContext {
      *
      */
     public static I2PAppContext getGlobalContext() { 
-        // skip the global lock
+        // skip the global lock - _gAC must be volatile
+        // http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
         I2PAppContext rv = _globalAppContext;
         if (rv != null)
             return rv;
@@ -474,6 +475,9 @@ public class I2PAppContext {
      * provided during the context construction, as well as the ones included in
      * System.getProperties.
      *
+     * WARNING - not overridden in RouterContext, doesn't contain router config settings,
+     * use getProperties() instead.
+     *
      * @return set of Strings containing the names of defined system properties
      */
     public Set getPropertyNames() { 
diff --git a/core/java/src/net/i2p/client/I2PSessionImpl.java b/core/java/src/net/i2p/client/I2PSessionImpl.java
index 10e7695c04..b904b121d8 100644
--- a/core/java/src/net/i2p/client/I2PSessionImpl.java
+++ b/core/java/src/net/i2p/client/I2PSessionImpl.java
@@ -421,7 +421,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
      *
      */
     public byte[] receiveMessage(int msgId) throws I2PSessionException {
-        MessagePayloadMessage msg = _availableMessages.remove(new Long(msgId));
+        MessagePayloadMessage msg = _availableMessages.remove(Long.valueOf(msgId));
         if (msg == null) {
             _log.error("Receive message " + msgId + " had no matches");
             return null;
@@ -468,7 +468,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
      * Recieve a payload message and let the app know its available
      */
     public void addNewMessage(MessagePayloadMessage msg) {
-        Long mid = new Long(msg.getMessageId());
+        Long mid = Long.valueOf(msg.getMessageId());
         _availableMessages.put(mid, msg);
         long id = msg.getMessageId();
         byte data[] = msg.getPayload().getUnencryptedData();
@@ -518,7 +518,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
         
         public void available(long msgId, int size) {
             synchronized (AvailabilityNotifier.this) {
-                _pendingIds.add(new Long(msgId));
+                _pendingIds.add(Long.valueOf(msgId));
                 _pendingSizes.add(Integer.valueOf(size));
                 AvailabilityNotifier.this.notifyAll();
             }
diff --git a/core/java/src/net/i2p/client/I2PSessionMuxedImpl.java b/core/java/src/net/i2p/client/I2PSessionMuxedImpl.java
index 837bc2dbc2..2f12f96f64 100644
--- a/core/java/src/net/i2p/client/I2PSessionMuxedImpl.java
+++ b/core/java/src/net/i2p/client/I2PSessionMuxedImpl.java
@@ -191,7 +191,7 @@ class I2PSessionMuxedImpl extends I2PSessionImpl2 implements I2PSession {
      */
     @Override
     public void addNewMessage(MessagePayloadMessage msg) {
-        Long mid = new Long(msg.getMessageId());
+        Long mid = Long.valueOf(msg.getMessageId());
         _availableMessages.put(mid, msg);
         long id = msg.getMessageId();
         byte data[] = msg.getPayload().getUnencryptedData();
diff --git a/core/java/src/net/i2p/client/I2PSimpleSession.java b/core/java/src/net/i2p/client/I2PSimpleSession.java
index 1c564f0db4..e108fc008b 100644
--- a/core/java/src/net/i2p/client/I2PSimpleSession.java
+++ b/core/java/src/net/i2p/client/I2PSimpleSession.java
@@ -108,7 +108,7 @@ class I2PSimpleSession extends I2PSessionImpl2 {
     /**
      * Only map message handlers that we will use
      */
-    class SimpleMessageHandlerMap extends I2PClientMessageHandlerMap {
+    private static class SimpleMessageHandlerMap extends I2PClientMessageHandlerMap {
         public SimpleMessageHandlerMap(I2PAppContext context) {
             int highest = Math.max(DestReplyMessage.MESSAGE_TYPE, BandwidthLimitsMessage.MESSAGE_TYPE);
             _handlers = new I2CPMessageHandler[highest+1];
diff --git a/core/java/src/net/i2p/crypto/CryptixAESEngine.java b/core/java/src/net/i2p/crypto/CryptixAESEngine.java
index ea2338003c..350821e355 100644
--- a/core/java/src/net/i2p/crypto/CryptixAESEngine.java
+++ b/core/java/src/net/i2p/crypto/CryptixAESEngine.java
@@ -149,7 +149,7 @@ public class CryptixAESEngine extends AESEngine {
     @Override
     public final void decryptBlock(byte payload[], int inIndex, SessionKey sessionKey, byte rv[], int outIndex) {
         if ( (payload == null) || (rv == null) )
-            throw new IllegalArgumentException("null block args [payload=" + payload + " rv="+rv);
+            throw new IllegalArgumentException("null block args");
         if (payload.length - inIndex > rv.length - outIndex)
             throw new IllegalArgumentException("bad block args [payload.len=" + payload.length 
                                                + " inIndex=" + inIndex + " rv.len=" + rv.length 
diff --git a/core/java/src/net/i2p/data/Base32.java b/core/java/src/net/i2p/data/Base32.java
index 3071e0bc01..cbcb93515f 100644
--- a/core/java/src/net/i2p/data/Base32.java
+++ b/core/java/src/net/i2p/data/Base32.java
@@ -72,13 +72,13 @@ public class Base32 {
     }
 
     private static void runApp(String args[]) {
+        if ("encodestring".equalsIgnoreCase(args[0])) {
+            System.out.println(encode(args[1].getBytes()));
+            return;
+        }
+        InputStream in = System.in;
+        OutputStream out = System.out;
         try {
-            if ("encodestring".equalsIgnoreCase(args[0])) {
-                System.out.println(encode(args[1].getBytes()));
-                return;
-            }
-            InputStream in = System.in;
-            OutputStream out = System.out;
             if (args.length >= 3) {
                 out = new FileOutputStream(args[2]);
             }
@@ -95,6 +95,9 @@ public class Base32 {
             }
         } catch (IOException ioe) {
             ioe.printStackTrace(System.err);
+        } finally {
+            try { in.close(); } catch (IOException e) {}
+            try { out.close(); } catch (IOException e) {}
         }
     }
 
diff --git a/core/java/src/net/i2p/data/Base64.java b/core/java/src/net/i2p/data/Base64.java
index fe730af6b4..52d59002d8 100644
--- a/core/java/src/net/i2p/data/Base64.java
+++ b/core/java/src/net/i2p/data/Base64.java
@@ -178,13 +178,13 @@ public class Base64 {
     }
 
     private static void runApp(String args[]) {
+        if ("encodestring".equalsIgnoreCase(args[0])) {
+            System.out.println(encode(args[1].getBytes()));
+            return;
+        }
+        InputStream in = System.in;
+        OutputStream out = System.out;
         try {
-            if ("encodestring".equalsIgnoreCase(args[0])) {
-                System.out.println(encode(args[1].getBytes()));
-                return;
-            }
-            InputStream in = System.in;
-            OutputStream out = System.out;
             if (args.length >= 3) {
                 out = new FileOutputStream(args[2]);
             }
@@ -201,6 +201,9 @@ public class Base64 {
             }
         } catch (IOException ioe) {
             ioe.printStackTrace(System.err);
+        } finally {
+            try { in.close(); } catch (IOException e) {}
+            try { out.close(); } catch (IOException e) {}
         }
     }
 
diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java
index bd6bf260d9..33264edf2b 100644
--- a/core/java/src/net/i2p/data/DataHelper.java
+++ b/core/java/src/net/i2p/data/DataHelper.java
@@ -844,7 +844,7 @@ public class DataHelper {
      */
     public final static void xor(byte lhs[], int startLeft, byte rhs[], int startRight, byte out[], int startOut, int len) {
         if ( (lhs == null) || (rhs == null) || (out == null) )
-            throw new NullPointerException("Invalid params to xor (" + lhs + ", " + rhs + ", " + out + ")");
+            throw new NullPointerException("Null params to xor");
         if (lhs.length < startLeft + len)
             throw new IllegalArgumentException("Left hand side is too short");
         if (rhs.length < startRight + len)
diff --git a/core/java/src/net/i2p/data/PrivateKeyFile.java b/core/java/src/net/i2p/data/PrivateKeyFile.java
index 0c752f9d6a..4f4af3cf9a 100644
--- a/core/java/src/net/i2p/data/PrivateKeyFile.java
+++ b/core/java/src/net/i2p/data/PrivateKeyFile.java
@@ -133,9 +133,15 @@ public class PrivateKeyFile {
      */
     public Destination createIfAbsent() throws I2PException, IOException, DataFormatException {
         if(!this.file.exists()) {
-            FileOutputStream out = new FileOutputStream(this.file);
-            this.client.createDestination(out);
-            out.close();
+            FileOutputStream out = null;
+            try {
+                out = new FileOutputStream(this.file);
+                this.client.createDestination(out);
+            } finally {
+                if (out != null) {
+                    try { out.close(); } catch (IOException ioe) {}
+                }
+            }
         }
         return getDestination();
     }
@@ -243,29 +249,36 @@ public class PrivateKeyFile {
     public I2PSession open() throws I2PSessionException, IOException {
         return this.open(new Properties());
     }
+
     public I2PSession open(Properties opts) throws I2PSessionException, IOException {
-        // open input file
-        FileInputStream in = new FileInputStream(this.file);
-        
-        // create sesssion
-        I2PSession s = this.client.createSession(in, opts);
-        
-        // close file
-        in.close();
-        
-        return s;
+        FileInputStream in = null;
+        try {
+            in = new FileInputStream(this.file);
+            I2PSession s = this.client.createSession(in, opts);
+            return s;
+        } finally {
+            if (in != null) {
+                try { in.close(); } catch (IOException ioe) {}
+            }
+        }
     }
     
     /**
      *  Copied from I2PClientImpl.createDestination()
      */
     public void write() throws IOException, DataFormatException {
-        FileOutputStream out = new FileOutputStream(this.file);
-        this.dest.writeBytes(out);
-        this.privKey.writeBytes(out);
-        this.signingPrivKey.writeBytes(out);
-        out.flush();
-        out.close();
+        FileOutputStream out = null;
+        try {
+            out = new FileOutputStream(this.file);
+            this.dest.writeBytes(out);
+            this.privKey.writeBytes(out);
+            this.signingPrivKey.writeBytes(out);
+            out.flush();
+        } finally {
+            if (out != null) {
+                try { out.close(); } catch (IOException ioe) {}
+            }
+        }
     }
 
     @Override
@@ -377,7 +390,8 @@ public class PrivateKeyFile {
                         }
                     }
                 }
-            } catch (Exception ioe) {
+            } catch (DataFormatException dfe) {
+            } catch (IOException ioe) {
             }
             // not found, continue to the next file
         }
diff --git a/core/java/src/net/i2p/data/i2cp/DestReplyMessage.java b/core/java/src/net/i2p/data/i2cp/DestReplyMessage.java
index 7aaba9c892..67e1351059 100644
--- a/core/java/src/net/i2p/data/i2cp/DestReplyMessage.java
+++ b/core/java/src/net/i2p/data/i2cp/DestReplyMessage.java
@@ -76,10 +76,11 @@ public class DestReplyMessage extends I2CPMessageImpl {
     }
 
     protected byte[] doWriteMessage() throws I2CPMessageException, IOException {
-        if (_dest == null && _hash == null)
-            return new byte[0];  // null response allowed
-        if (_dest == null && _hash != null)
+        if (_dest == null) {
+            if (_hash == null)
+                return new byte[0];  // null response allowed
             return _hash.getData();
+        }
         ByteArrayOutputStream os = new ByteArrayOutputStream(_dest.size());
         try {
             _dest.writeBytes(os);
diff --git a/core/java/src/net/i2p/stat/FrequencyStat.java b/core/java/src/net/i2p/stat/FrequencyStat.java
index dfa7bcfe67..d18a469dbd 100644
--- a/core/java/src/net/i2p/stat/FrequencyStat.java
+++ b/core/java/src/net/i2p/stat/FrequencyStat.java
@@ -89,7 +89,7 @@ public class FrequencyStat {
     /** @since 0.8.2 */
     @Override
     public boolean equals(Object obj) {
-        if ((obj == null) || (obj.getClass() != FrequencyStat.class)) return false;
+        if ((obj == null) || !(obj instanceof FrequencyStat)) return false;
         return _statName.equals(((FrequencyStat)obj)._statName);
     }
 
diff --git a/core/java/src/net/i2p/stat/Rate.java b/core/java/src/net/i2p/stat/Rate.java
index fd0fae49f0..8473d58ebb 100644
--- a/core/java/src/net/i2p/stat/Rate.java
+++ b/core/java/src/net/i2p/stat/Rate.java
@@ -473,7 +473,7 @@ public class Rate {
 
     @Override
     public boolean equals(Object obj) {
-        if ((obj == null) || (obj.getClass() != Rate.class)) return false;
+        if ((obj == null) || !(obj instanceof Rate)) return false;
         if (obj == this) return true;
         Rate r = (Rate) obj;
         return _period == r.getPeriod() && _creationDate == r.getCreationDate() &&
diff --git a/core/java/src/net/i2p/stat/RateStat.java b/core/java/src/net/i2p/stat/RateStat.java
index 79ddec5198..fded9b019f 100644
--- a/core/java/src/net/i2p/stat/RateStat.java
+++ b/core/java/src/net/i2p/stat/RateStat.java
@@ -108,7 +108,7 @@ public class RateStat {
 
     @Override
     public boolean equals(Object obj) {
-        if ((obj == null) || (obj.getClass() != RateStat.class)) return false;
+        if ((obj == null) || !(obj instanceof RateStat)) return false;
         RateStat rs = (RateStat) obj;
         if (DataHelper.eq(getGroupName(), rs.getGroupName()) && DataHelper.eq(getDescription(), rs.getDescription())
             && DataHelper.eq(getName(), rs.getName())) {
diff --git a/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java b/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java
index 6b4ab71f40..021bbf6c17 100644
--- a/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java
+++ b/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java
@@ -116,7 +116,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
             if (!eq)
                 throw new I2NPMessageException("Hash does not match for " + getClass().getName());
 
-            long start = _context.clock().now();
+            //long start = _context.clock().now();
             if (_log.shouldLog(Log.DEBUG))
                 _log.debug("Reading bytes: type = " + type + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration);
             readMessage(buffer, 0, size, type);
@@ -159,7 +159,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
         if (!eq)
             throw new I2NPMessageException("Hash does not match for " + getClass().getName());
 
-        long start = _context.clock().now();
+        //long start = _context.clock().now();
         if (_log.shouldLog(Log.DEBUG))
             _log.debug("Reading bytes: type = " + type + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration);
         readMessage(data, cur, size, type);
@@ -215,7 +215,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
     }
     
     public int toByteArray(byte buffer[]) {
-        long start = _context.clock().now();
+        //long start = _context.clock().now();
 
         int prefixLen = 1 // type
                         + 4 // uniqueId
diff --git a/router/java/src/net/i2p/data/i2np/TunnelDataMessage.java b/router/java/src/net/i2p/data/i2np/TunnelDataMessage.java
index 1ac01d0a6c..ef4930594f 100644
--- a/router/java/src/net/i2p/data/i2np/TunnelDataMessage.java
+++ b/router/java/src/net/i2p/data/i2np/TunnelDataMessage.java
@@ -160,7 +160,7 @@ public class TunnelDataMessage extends I2NPMessageImpl {
     /** write the message body to the output array, starting at the given index */
     protected int writeMessageBody(byte out[], int curIndex) throws I2NPMessageException {
         if ( (_tunnelId <= 0) || (_data == null) )
-            throw new I2NPMessageException("Not enough data to write out (id=" + _tunnelId + " data=" + _data + ")");
+            throw new I2NPMessageException("Not enough data to write out (id=" + _tunnelId + ")");
         if (_data.length <= 0) 
             throw new I2NPMessageException("Not enough data to write out (data.length=" + _data.length + ")");
 
-- 
GitLab