[Demonstration 1]

[Buggy Method]
@RequestMapping(value = "/user/sharelog", method = RequestMethod.POST)
public void saveShareLog(@RequestBody VisitLog visitLog, HttpServletRequest request) {
    String visitIp = request.getRemoteAddr();
    Integer logType;
    if (null != visitLog.refUserId) {
        logType = CommonUtils.LogType.REWARD_SHARE.getKey();
    } else {
        logType = CommonUtils.LogType.SIMPLE_SHARE.getKey();
    }
    visitLogService.addShareLog(visitLog, logType, visitIp);
}

[Buggy Hunk]
String visitIp = request.getRemoteAddr();

[Patch]
String visitIp = CommonUtils.getRequestIP(request);

[Demonstration 2]

[Buggy Method]
@Override
public void run() {
    final Map<Integer, Pair<Address, CloseableByteBufferHandler>> establishedConnections = new HashMap<>();
    final boolean[] closed = new boolean[] { false };
    try {
        OutputStream sout;
        InputStream sin;
        try {
            sout = new GZIPOutputStream(socket.getOutputStream(), true);
            sin = new GZIPInputStream(socket.getInputStream());
        } catch (IOException ee) {
            try {
                socket.close();
            } catch (IOException se) {
            }
            throw ee;
        }
        final DataOutputStream out = new DataOutputStream(sout);
        DataInputStream in = new DataInputStream(sin);
        LOGGER.debug("Accepted connection from: {}", socket.getInetAddress());
        while (true) {
            LOGGER.trace("Server waiting for connection ID");
            final int connectionId = in.readInt();
            LOGGER.trace("Server received connection ID: {}", connectionId);
            int len = in.readInt();
            if (len < 0) {
                int command = -len;
                if (command == ProxyCommons.Commands.ESTABLISH_CONNECTION) {
                    final Address address = in.readBoolean() ? new Address(in.readUTF(), in.readInt()) : null;
                    ReadyFactory factory = proxyServerSide.read(in);
                    //%% Ready r = new QueueReady(queue, factory.create());
                    Ready r = factory.create();
                    r.connect(address, new ReadyConnection() {

                        @Override
                        public void failed(IOException e) {
                            LOGGER.warn("Could not connect to {}", address, e);
                            synchronized (out) {
                                try {
                                    out.writeInt(connectionId);
                                    out.writeInt(-ProxyCommons.Commands.FAIL_CONNECTION);
                                    out.flush();
                                } catch (IOException ioe) {
                                    try {
                                        out.close();
                                    } catch (IOException se) {
                                    }
                                }
                            }
                        }

                        @Override
                        public void connected(FailableCloseableByteBufferHandler write) {
                            synchronized (establishedConnections) {
                                if (closed[0]) {
                                    write.close();
                                    return;
                                }
                                establishedConnections.put(connectionId, new Pair<Address, CloseableByteBufferHandler>(address, write));
                            }
                            synchronized (out) {
                                try {
                                    out.writeInt(connectionId);
                                    out.writeInt(-ProxyCommons.Commands.ESTABLISH_CONNECTION);
                                    out.flush();
                                } catch (IOException ioe) {
                                    try {
                                        out.close();
                                    } catch (IOException se) {
                                    }
                                }
                            }
                        }

                        @Override
                        public void close() {
                            synchronized (out) {
                                try {
                                    out.writeInt(connectionId);
                                    out.writeInt(0);
                                    out.flush();
                                } catch (IOException ioe) {
                                    try {
                                        out.close();
                                    } catch (IOException se) {
                                    }
                                }
                            }
                        }

                        @Override
                        public void handle(Address address, ByteBuffer buffer) {
                            if (!buffer.hasRemaining()) {
                                return;
                            }
                            synchronized (out) {
                                try {
                                    out.writeInt(connectionId);
                                    out.writeInt(buffer.remaining());
                                    if (address == null) {
                                        out.writeBoolean(false);
                                    } else {
                                        out.writeBoolean(true);
                                        out.writeUTF(address.getHost());
                                        out.writeInt(address.getPort());
                                    }
                                    out.write(buffer.array(), buffer.arrayOffset(), buffer.remaining());
                                    out.flush();
                                } catch (IOException ioe) {
                                    try {
                                        out.close();
                                    } catch (IOException se) {
                                    }
                                }
                            }
                            buffer.position(buffer.position() + buffer.remaining());
                        }
                    });
                }
            } else if (len == 0) {
                Pair<Address, CloseableByteBufferHandler> connection;
                synchronized (establishedConnections) {
                    connection = establishedConnections.remove(connectionId);
                }
                if (connection != null) {
                    if (connection.second != null) {
                        connection.second.close();
                    }
                }
            } else {
                Pair<Address, CloseableByteBufferHandler> connection;
                synchronized (establishedConnections) {
                    connection = establishedConnections.get(connectionId);
                }
                Address a;
                if (in.readBoolean()) {
                    a = new Address(in.readUTF(), in.readInt());
                } else {
                    a = (connection == null) ? null : connection.first;
                }
                byte[] b = new byte[len];
                in.readFully(b);
                if (connection != null) {
                    if (!addressesToFilter.contains(a)) {
                        if (connection.second != null) {
                            connection.second.handle(a, ByteBuffer.wrap(b));
                        }
                    }
                } else {
                    LOGGER.debug("Invalid connection ID: {}", connectionId);
                }
            }
        }
    } catch (IOException e) {
        LOGGER.info("Socket closed by peer: {}", e.getMessage());
        try {
            socket.close();
        } catch (IOException ioe) {
        }
        List<CloseableByteBufferHandler> toClose = new LinkedList<>();
        synchronized (establishedConnections) {
            closed[0] = true;
            for (Pair<Address, CloseableByteBufferHandler> connection : establishedConnections.values()) {
                if (connection.second != null) {
                    toClose.add(connection.second);
                }
            }
        }
        for (CloseableByteBufferHandler c : toClose) {
            c.close();
        }
    }
}

[Buggy Hunk]
out.write(buffer.array(), buffer.arrayOffset(), buffer.remaining());

[Patch]
out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());

[Demonstration 3]

[Buggy Method]
private static RedditClient createRedditClient() throws RedditAuthException {
    UserAgent userAgent = UserAgent.of(PLATFORM, APP_ID, VERSION, USERNAME);
    Credentials credentials = Credentials.script(USERNAME, PASSWORD, CLIENT_ID, CLIENT_SECRET);
    RedditClient redditClient = new RedditClient(userAgent);
    redditClient.setLoggingMode(LoggingMode.ON_FAIL);
    try {
        OAuthData authData = redditClient.getOAuthHelper().easyAuth(credentials);
        redditClient.authenticate(authData);
    } catch (OAuthException e) {
        throw new RedditAuthException("Could not authorize application", e);
    }
    return redditClient;
}

[Buggy Hunk]
Credentials credentials = Credentials.script(USERNAME, PASSWORD, CLIENT_ID, CLIENT_SECRET);

[Patch]
Credentials credentials = Credentials.userless(CLIENT_ID, CLIENT_SECRET, UUID.randomUUID());