Commit d95a5fd5 authored by akwizgran's avatar akwizgran

Use lower default limit for BDF strings and raws.

parent ed2c0336
Pipeline #1544 passed with stage
in 8 minutes and 20 seconds
...@@ -8,6 +8,7 @@ import java.io.IOException; ...@@ -8,6 +8,7 @@ import java.io.IOException;
public interface BdfReader { public interface BdfReader {
int DEFAULT_NESTED_LIMIT = 5; int DEFAULT_NESTED_LIMIT = 5;
int DEFAULT_MAX_BUFFER_SIZE = 64 * 1024;
boolean eof() throws IOException; boolean eof() throws IOException;
...@@ -39,13 +40,13 @@ public interface BdfReader { ...@@ -39,13 +40,13 @@ public interface BdfReader {
boolean hasString() throws IOException; boolean hasString() throws IOException;
String readString(int maxLength) throws IOException; String readString() throws IOException;
void skipString() throws IOException; void skipString() throws IOException;
boolean hasRaw() throws IOException; boolean hasRaw() throws IOException;
byte[] readRaw(int maxLength) throws IOException; byte[] readRaw() throws IOException;
void skipRaw() throws IOException; void skipRaw() throws IOException;
......
...@@ -9,5 +9,6 @@ public interface BdfReaderFactory { ...@@ -9,5 +9,6 @@ public interface BdfReaderFactory {
BdfReader createReader(InputStream in); BdfReader createReader(InputStream in);
BdfReader createReader(InputStream in, int nestedLimit); BdfReader createReader(InputStream in, int nestedLimit,
int maxBufferSize);
} }
...@@ -8,6 +8,7 @@ import java.io.InputStream; ...@@ -8,6 +8,7 @@ import java.io.InputStream;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import static org.briarproject.bramble.api.data.BdfReader.DEFAULT_MAX_BUFFER_SIZE;
import static org.briarproject.bramble.api.data.BdfReader.DEFAULT_NESTED_LIMIT; import static org.briarproject.bramble.api.data.BdfReader.DEFAULT_NESTED_LIMIT;
@Immutable @Immutable
...@@ -16,11 +17,13 @@ class BdfReaderFactoryImpl implements BdfReaderFactory { ...@@ -16,11 +17,13 @@ class BdfReaderFactoryImpl implements BdfReaderFactory {
@Override @Override
public BdfReader createReader(InputStream in) { public BdfReader createReader(InputStream in) {
return new BdfReaderImpl(in, DEFAULT_NESTED_LIMIT); return new BdfReaderImpl(in, DEFAULT_NESTED_LIMIT,
DEFAULT_MAX_BUFFER_SIZE);
} }
@Override @Override
public BdfReader createReader(InputStream in, int nestedLimit) { public BdfReader createReader(InputStream in, int nestedLimit,
return new BdfReaderImpl(in, nestedLimit); int maxBufferSize) {
return new BdfReaderImpl(in, nestedLimit, maxBufferSize);
} }
} }
...@@ -37,15 +37,16 @@ class BdfReaderImpl implements BdfReader { ...@@ -37,15 +37,16 @@ class BdfReaderImpl implements BdfReader {
private static final byte[] EMPTY_BUFFER = new byte[0]; private static final byte[] EMPTY_BUFFER = new byte[0];
private final InputStream in; private final InputStream in;
private final int nestedLimit; private final int nestedLimit, maxBufferSize;
private boolean hasLookahead = false, eof = false; private boolean hasLookahead = false, eof = false;
private byte next; private byte next;
private byte[] buf = new byte[8]; private byte[] buf = new byte[8];
BdfReaderImpl(InputStream in, int nestedLimit) { BdfReaderImpl(InputStream in, int nestedLimit, int maxBufferSize) {
this.in = in; this.in = in;
this.nestedLimit = nestedLimit; this.nestedLimit = nestedLimit;
this.maxBufferSize = maxBufferSize;
} }
private void readLookahead() throws IOException { private void readLookahead() throws IOException {
...@@ -91,8 +92,8 @@ class BdfReaderImpl implements BdfReader { ...@@ -91,8 +92,8 @@ class BdfReaderImpl implements BdfReader {
if (hasBoolean()) return readBoolean(); if (hasBoolean()) return readBoolean();
if (hasLong()) return readLong(); if (hasLong()) return readLong();
if (hasDouble()) return readDouble(); if (hasDouble()) return readDouble();
if (hasString()) return readString(Integer.MAX_VALUE); if (hasString()) return readString();
if (hasRaw()) return readRaw(Integer.MAX_VALUE); if (hasRaw()) return readRaw();
if (hasList()) return readList(level); if (hasList()) return readList(level);
if (hasDictionary()) return readDictionary(level); if (hasDictionary()) return readDictionary(level);
throw new FormatException(); throw new FormatException();
...@@ -245,11 +246,11 @@ class BdfReaderImpl implements BdfReader { ...@@ -245,11 +246,11 @@ class BdfReaderImpl implements BdfReader {
} }
@Override @Override
public String readString(int maxLength) throws IOException { public String readString() throws IOException {
if (!hasString()) throw new FormatException(); if (!hasString()) throw new FormatException();
hasLookahead = false; hasLookahead = false;
int length = readStringLength(); int length = readStringLength();
if (length < 0 || length > maxLength) throw new FormatException(); if (length < 0 || length > maxBufferSize) throw new FormatException();
if (length == 0) return ""; if (length == 0) return "";
readIntoBuffer(length); readIntoBuffer(length);
return new String(buf, 0, length, "UTF-8"); return new String(buf, 0, length, "UTF-8");
...@@ -279,11 +280,11 @@ class BdfReaderImpl implements BdfReader { ...@@ -279,11 +280,11 @@ class BdfReaderImpl implements BdfReader {
} }
@Override @Override
public byte[] readRaw(int maxLength) throws IOException { public byte[] readRaw() throws IOException {
if (!hasRaw()) throw new FormatException(); if (!hasRaw()) throw new FormatException();
hasLookahead = false; hasLookahead = false;
int length = readRawLength(); int length = readRawLength();
if (length < 0 || length > maxLength) throw new FormatException(); if (length < 0 || length > maxBufferSize) throw new FormatException();
if (length == 0) return EMPTY_BUFFER; if (length == 0) return EMPTY_BUFFER;
byte[] b = new byte[length]; byte[] b = new byte[length];
readIntoBuffer(b, length); readIntoBuffer(b, length);
...@@ -381,7 +382,7 @@ class BdfReaderImpl implements BdfReader { ...@@ -381,7 +382,7 @@ class BdfReaderImpl implements BdfReader {
BdfDictionary dictionary = new BdfDictionary(); BdfDictionary dictionary = new BdfDictionary();
readDictionaryStart(); readDictionaryStart();
while (!hasDictionaryEnd()) while (!hasDictionaryEnd())
dictionary.put(readString(Integer.MAX_VALUE), readObject(level + 1)); dictionary.put(readString(), readObject(level + 1));
readDictionaryEnd(); readDictionaryEnd();
return dictionary; return dictionary;
} }
......
...@@ -59,8 +59,8 @@ class MetadataParserImpl implements MetadataParser { ...@@ -59,8 +59,8 @@ class MetadataParserImpl implements MetadataParser {
if (reader.hasBoolean()) return reader.readBoolean(); if (reader.hasBoolean()) return reader.readBoolean();
if (reader.hasLong()) return reader.readLong(); if (reader.hasLong()) return reader.readLong();
if (reader.hasDouble()) return reader.readDouble(); if (reader.hasDouble()) return reader.readDouble();
if (reader.hasString()) return reader.readString(Integer.MAX_VALUE); if (reader.hasString()) return reader.readString();
if (reader.hasRaw()) return reader.readRaw(Integer.MAX_VALUE); if (reader.hasRaw()) return reader.readRaw();
if (reader.hasList()) return reader.readList(); if (reader.hasList()) return reader.readList();
if (reader.hasDictionary()) return reader.readDictionary(); if (reader.hasDictionary()) return reader.readDictionary();
throw new FormatException(); throw new FormatException();
......
...@@ -35,8 +35,8 @@ public class MessagingModule { ...@@ -35,8 +35,8 @@ public class MessagingModule {
@Provides @Provides
PrivateMessageFactory providePrivateMessageFactory( PrivateMessageFactory providePrivateMessageFactory(
ClientHelper clientHelper) { PrivateMessageFactoryImpl privateMessageFactory) {
return new PrivateMessageFactoryImpl(clientHelper); return privateMessageFactory;
} }
@Provides @Provides
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment