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

[DE-214] warn on json string too big #434

Merged
merged 1 commit into from
May 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/com/arangodb/internal/http/HttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down