Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.nio.BufferUnderflowException #847

Closed
nalizadeh opened this issue Jan 27, 2019 · 13 comments · Fixed by #849
Closed

java.nio.BufferUnderflowException #847

nalizadeh opened this issue Jan 27, 2019 · 13 comments · Fixed by #849
Assignees
Labels
Milestone

Comments

@nalizadeh
Copy link

Hello community,
I am not sure if this is a bug or a mistake of mine. I'm trying to upload images from an HTML page, it works quite well with small images (max size = 16kb), but with large images the server throws the exception java.nio.BufferUnderflowException.

grafik

Is there a way to increase the frame capacity here?
I use the Firefox Quantum as a browser.

@PhilipRoman
Copy link
Collaborator

Could you reproduce this with Chrome? Thanks!

@nalizadeh
Copy link
Author

nalizadeh commented Jan 27, 2019

Yes I get the same exception with Chrome (version 71.0.3578.98)

@PhilipRoman
Copy link
Collaborator

Interesting... can you give us a link to your client side source code?

@nalizadeh
Copy link
Author

nalizadeh commented Jan 27, 2019

This is my simple html client side code

Click to expand
<!DOCTYPE html>
<html>
<head>
    <style>
        #messages {
            text-align: left;
            width: 50%;
            padding: 1em;
            border: 1px solid black;
        }
    </style>
    <title>WebSocket Client</title>
</head>
<body>
<div class="container">
    <div id="messages" class="messages"></div>
    <div class="input-fields">
        <p>Type a message and hit send:</p>
        <input id="message"/>
        <button id="send">Send</button>

        <p>Select an image and hit send:</p>
        <input type="file" id="file" accept="image/*"/>

        <button id="sendImage">Send Image</button>
    </div>
</div>
</body>
<script>

    const messageWindow = document.getElementById("messages");
    const sendButton = document.getElementById("send");
    const messageInput = document.getElementById("message");
    const fileInput = document.getElementById("file");
    const sendImageButton = document.getElementById("sendImage");
	
    const socket = new WebSocket("ws://localhost:8080");
    socket.binaryType = "arraybuffer";

    socket.onopen = function (event) {
        addMessageToWindow("Connected");
    };

    socket.onmessage = function (event) {
        if (event.data instanceof ArrayBuffer) {
            addMessageToWindow("Got Image:");
            addImageToWindow(event.data);
        } else {
            addMessageToWindow("Got Message: " + event.data);
        }
    };

    sendButton.onclick = function (event) {
        sendMessage(messageInput.value);
        messageInput.value = "";
    };

    sendImageButton.onclick = function (event) {
    	
		var reader = new FileReader();
		
		reader.onload = function() {
			sendMessage(this.result);
		};
		reader.readAsArrayBuffer(fileInput.files[0]);
        fileInput.value = null;
    };

    function sendMessage(message) {
        socket.send(message);
        addMessageToWindow("Sent Message: " + message);
    }

    function addMessageToWindow(message) {
        messageWindow.innerHTML += "<div>" + message + "</div>";
    }

    function addImageToWindow(image) {
        let url = URL.createObjectURL(new Blob([image]));
        messageWindow.innerHTML += "<img src='" + url + "'/>";
    }
</script>
</html>

@PhilipRoman
Copy link
Collaborator

PhilipRoman commented Jan 28, 2019

I can't reproduce this behaviour. I can successfully upload & download images of size over 40 MB. I'd appreciate if you could post the following details:

  • specific image file which causes it to fail
  • server side code
  • java version & Java-WebSocket library version

@marci4
Copy link
Collaborator

marci4 commented Jan 28, 2019

Was able to reproduce this with 65.0-64 bit running against the ChatServer example using your avatar image @PhilipRoman (https://avatars2.githubusercontent.com/u/33231208?s=400&v=4)

Worked best when trying to upload the same image multiple times

Only affects Firefox currently as it seems..
I was not able to reproduce this with 72.0.3626.71

@PhilipRoman
Copy link
Collaborator

PhilipRoman commented Jan 28, 2019

Very strange. I've tried on Java 8 and Java 11; on Chrome 71.0.3578.98, Firefox Quantum 64.0.2 (64-bit), MS Edge with my avatar and loads of other files I could find, but none of them triggered the error.

@marci4
Copy link
Collaborator

marci4 commented Jan 28, 2019

Looks like the bug cause is #805, in detail my refactoring of translateSingleFramePayloadLength (call by value instead of reference for realpacketsize).

@marci4 marci4 self-assigned this Jan 28, 2019
@marci4 marci4 added this to the Release 1.4.0 milestone Jan 28, 2019
@marci4
Copy link
Collaborator

marci4 commented Jan 28, 2019

@nalizadeh could you confirm that this only affects 1.4.0?

Best regards,
Marcel

@PhilipRoman
Copy link
Collaborator

PhilipRoman commented Jan 29, 2019

Looks like it happens only on 1.4.0 (I was previously testing on 1.3.9)! I tested on library version 1.4.0 and it fails reliably. The issue is not related to client side and can be reproduced by sending a byte buffer from JWS client.

  1. When sending a zero-filled buffer of 16376 bytes, everything is normal
  2. When sending a zero-filled buffer of 16377 bytes, the first exception occurs:
[WebSocketWorker-11] ERROR org.java_websocket.server.WebSocketServer - Error while reading from remote connection
java.nio.BufferUnderflowException
	at java.nio.Buffer.nextGetIndex(Buffer.java:500)
	at java.nio.HeapByteBuffer.get(HeapByteBuffer.java:135)
	at org.java_websocket.drafts.Draft_6455.translateSingleFrame(Draft_6455.java:491)
	at org.java_websocket.drafts.Draft_6455.translateFrame(Draft_6455.java:645)
	at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:378)
	at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:218)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.doDecode(WebSocketServer.java:1015)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:997)
  1. After this, sending a buffer with 16376 bytes again, the following error happens:
[WebSocketWorker-11] ERROR org.java_websocket.WebSocketImpl - Closing due to invalid data in frame
org.java_websocket.exceptions.InvalidFrameException: Unknown opcode 3
	at org.java_websocket.drafts.Draft_6455.toOpcode(Draft_6455.java:769)
	at org.java_websocket.drafts.Draft_6455.translateSingleFrame(Draft_6455.java:476)
	at org.java_websocket.drafts.Draft_6455.translateFrame(Draft_6455.java:628)
	at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:378)
	at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:218)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.doDecode(WebSocketServer.java:1015)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:997)
Error from org.java_websocket.WebSocketImpl@64921d13
org.java_websocket.exceptions.InvalidFrameException: Unknown opcode 3
	at org.java_websocket.drafts.Draft_6455.toOpcode(Draft_6455.java:769)
	at org.java_websocket.drafts.Draft_6455.translateSingleFrame(Draft_6455.java:476)
	at org.java_websocket.drafts.Draft_6455.translateFrame(Draft_6455.java:628)
	at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:378)
	at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:218)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.doDecode(WebSocketServer.java:1015)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:997)

and the client is closed.

If in step 3 the buffer size is 1, the following error occurs instead:

[WebSocketWorker-11] ERROR org.java_websocket.WebSocketImpl - Closing due to invalid data in frame
org.java_websocket.exceptions.InvalidFrameException: bad rsv RSV1: false RSV2: true RSV3: false
	at org.java_websocket.extensions.DefaultExtension.isFrameValid(DefaultExtension.java:64)
	at org.java_websocket.drafts.Draft_6455.translateSingleFrame(Draft_6455.java:505)
	at org.java_websocket.drafts.Draft_6455.translateFrame(Draft_6455.java:628)
	at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:378)
	at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:218)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.doDecode(WebSocketServer.java:1015)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:997)
Error from org.java_websocket.WebSocketImpl@2e92f83c
org.java_websocket.exceptions.InvalidFrameException: bad rsv RSV1: false RSV2: true RSV3: false
	at org.java_websocket.extensions.DefaultExtension.isFrameValid(DefaultExtension.java:64)
	at org.java_websocket.drafts.Draft_6455.translateSingleFrame(Draft_6455.java:505)
	at org.java_websocket.drafts.Draft_6455.translateFrame(Draft_6455.java:628)
	at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:378)
	at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:218)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.doDecode(WebSocketServer.java:1015)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:997)

@nalizadeh
Copy link
Author

specific image file which causes it to fail
-> any large image causes the error

server side code
-> I just took the ChatServer example and changed each "broadcast(..)" with "conn.send(..)"

java version & Java-WebSocket library version
->jdk1.8.0_72, Eclipse Java EE IDE Neon.3 (4.6.3), this includes the latest javax.websocket library

could you confirm that this only affects 1.4.0?
-> yes. I use the latest repository.

Good luck with bug fixing! :)

@marci4
Copy link
Collaborator

marci4 commented Jan 29, 2019

Fixing this tonight!

marci4 added a commit to marci4/Java-WebSocket-Dev that referenced this issue Jan 29, 2019
@marci4 marci4 mentioned this issue Jan 29, 2019
8 tasks
@nalizadeh
Copy link
Author

Now that's solved for me, many Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants