From 6902a8392f9986188aa1bc1533a80f15a63644fc Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Fri, 16 Nov 2012 14:45:05 +0000
Subject: [PATCH]  * i2psnark: Fix rare IOOBE (ticket #777)

---
 .../java/src/org/klomp/snark/PeerState.java   | 23 +++++++++----------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerState.java b/apps/i2psnark/java/src/org/klomp/snark/PeerState.java
index 8add00d2b9..305d6ee311 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/PeerState.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/PeerState.java
@@ -356,22 +356,21 @@ class PeerState implements DataLoader
                   + piece + "," + begin + "," + length + ") from "
                   + peer);
 
-    int r = getFirstOutstandingRequest(piece);
-
-    // Unrequested piece number?
-    if (r == -1)
-      {
-        if (_log.shouldLog(Log.INFO))
-          _log.info("Unrequested 'piece: " + piece + ", "
-                      + begin + ", " + length + "' received from "
-                      + peer);
-        return null;
-      }
-
     // Lookup the correct piece chunk request from the list.
     Request req;
     synchronized(this)
       {
+        int r = getFirstOutstandingRequest(piece);
+
+        // Unrequested piece number?
+        if (r == -1) {
+            if (_log.shouldLog(Log.INFO))
+                _log.info("Unrequested 'piece: " + piece + ", "
+                      + begin + ", " + length + "' received from "
+                      + peer);
+            return null;
+        }
+
         req = outstandingRequests.get(r);
         while (req.getPiece() == piece && req.off != begin
                && r < outstandingRequests.size() - 1)
-- 
GitLab