Skip to content

Commit 8c2e7fc

Browse files
authored
warn on json string too big (#434)
1 parent 9d091d2 commit 8c2e7fc

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/main/java/com/arangodb/internal/http/HttpConnection.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public class HttpConnection implements Connection {
8282
"utf-8");
8383
private static final ContentType CONTENT_TYPE_VPACK = ContentType.create("application/x-velocypack");
8484

85+
// max safe UTF-8 json string length, so that it can be converted to byte array
86+
private static final int MAX_JSON_LENGTH = (Integer.MAX_VALUE - 8) / 4;
87+
8588
public static class Builder {
8689
private String user;
8790
private String password;
@@ -291,7 +294,12 @@ private HttpRequestBase requestWithBody(final HttpEntityEnclosingRequestBase htt
291294
Arrays.copyOfRange(body.getBuffer(), body.getStart(), body.getStart() + body.getByteSize()),
292295
CONTENT_TYPE_VPACK));
293296
} else {
294-
httpRequest.setEntity(new StringEntity(body.toString(), CONTENT_TYPE_APPLICATION_JSON_UTF8));
297+
String json = body.toString();
298+
if (json.length() > MAX_JSON_LENGTH) {
299+
LOGGER.warn("Json string length is greater than safe threshold (" + MAX_JSON_LENGTH + "). " +
300+
"This could cause memory allocation errors.");
301+
}
302+
httpRequest.setEntity(new StringEntity(json, CONTENT_TYPE_APPLICATION_JSON_UTF8));
295303
}
296304
}
297305
return httpRequest;

0 commit comments

Comments
 (0)