From 1c2888c156b0579fe8ae5611e887cb2cfabe7dd0 Mon Sep 17 00:00:00 2001 From: Michele Rastelli Date: Mon, 9 May 2022 13:20:37 +0200 Subject: [PATCH] warn on json string too big --- .../com/arangodb/internal/http/HttpConnection.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/arangodb/internal/http/HttpConnection.java b/src/main/java/com/arangodb/internal/http/HttpConnection.java index c13534825..7cc380e22 100644 --- a/src/main/java/com/arangodb/internal/http/HttpConnection.java +++ b/src/main/java/com/arangodb/internal/http/HttpConnection.java @@ -82,6 +82,9 @@ public class HttpConnection implements Connection { "utf-8"); private static final ContentType CONTENT_TYPE_VPACK = ContentType.create("application/x-velocypack"); + // max safe UTF-8 json string length, so that it can be converted to byte array + private static final int MAX_JSON_LENGTH = (Integer.MAX_VALUE - 8) / 4; + public static class Builder { private String user; private String password; @@ -291,7 +294,12 @@ private HttpRequestBase requestWithBody(final HttpEntityEnclosingRequestBase htt Arrays.copyOfRange(body.getBuffer(), body.getStart(), body.getStart() + body.getByteSize()), CONTENT_TYPE_VPACK)); } else { - httpRequest.setEntity(new StringEntity(body.toString(), CONTENT_TYPE_APPLICATION_JSON_UTF8)); + String json = body.toString(); + if (json.length() > MAX_JSON_LENGTH) { + LOGGER.warn("Json string length is greater than safe threshold (" + MAX_JSON_LENGTH + "). " + + "This could cause memory allocation errors."); + } + httpRequest.setEntity(new StringEntity(json, CONTENT_TYPE_APPLICATION_JSON_UTF8)); } } return httpRequest;