Skip to content

Commit

Permalink
Fix marschalling with very long long messages
Browse files Browse the repository at this point in the history
  • Loading branch information
schwabe committed Dec 28, 2024
1 parent 88be0be commit 4a982ab
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
20 changes: 17 additions & 3 deletions main/src/main/java/de/blinkt/openvpn/core/LogItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public LogItem(byte[] in, int length) throws UnsupportedEncodingException {
throw new IndexOutOfBoundsException("String length " + len + " is bigger than remaining bytes " + bb.remaining());
byte[] utf8bytes = new byte[len];
bb.get(utf8bytes);
mMessage = new String(utf8bytes, "UTF-8");
mMessage = new String(utf8bytes, StandardCharsets.UTF_8);
}
int numArgs = bb.getInt();
if (numArgs > 30) {
Expand Down Expand Up @@ -209,8 +209,22 @@ public LogItem(byte[] in, int length) throws UnsupportedEncodingException {

private void marschalString(String str, ByteBuffer bb) throws UnsupportedEncodingException {
byte[] utf8bytes = str.getBytes(StandardCharsets.UTF_8);
bb.putInt(utf8bytes.length);
bb.put(utf8bytes);

byte[] elipse = {'.', '.', '.', '[','t','o','o', ' ', 'l','o','n','g',']'};

int maxStringLength = Math.min(8192, bb.remaining()-128);

if (utf8bytes.length > maxStringLength)
{
bb.putInt(maxStringLength + elipse.length);
bb.put(utf8bytes, 0, maxStringLength);
bb.put(elipse);
}
else
{
bb.putInt(utf8bytes.length);
bb.put(utf8bytes);
}
}

private String unmarschalString(ByteBuffer bb) throws UnsupportedEncodingException {
Expand Down
35 changes: 35 additions & 0 deletions main/src/test/java/de/blinkt/openvpn/core/TestLog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2012-2024 Arne Schwabe
* Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
*/

package de.blinkt.openvpn.core

import org.junit.Assert
import org.junit.Test

class TestLog {

@Test
fun testMarschTooLong()
{
/* generate a string that is 16k long */
var longtsring = "";

while (longtsring.length < 16384)
{
longtsring += "very very long string indeed"
}
val li = LogItem(VpnStatus.LogLevel.VERBOSE, longtsring)

val libytes = li.marschaledBytes;

Assert.assertTrue(libytes.size > 2000);
Assert.assertTrue(libytes.size < 12000);


val liback = LogItem(libytes, libytes.size)
val msgback = liback.getString(null)
Assert.assertTrue(msgback.endsWith("...[too long]"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import android.annotation.SuppressLint;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.junit.Assert;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -42,7 +41,7 @@ public void testWriteByteArray() throws IOException {
lfh.writeEscapedBytes(testUnescaped);

byte[] result = byteArrayOutputStream.toByteArray();
Assert.assertTrue(Arrays.equals(expectedEscaped, result));
Assert.assertArrayEquals(expectedEscaped, result);
}

@Test
Expand Down

0 comments on commit 4a982ab

Please sign in to comment.