Skip to content

Commit

Permalink
fix: issue#44 integer overflow in usage request
Browse files Browse the repository at this point in the history
 - `Usage.Detail::count` and `Usage.Detail::limit` were declared as `long` but parsed as `int`.
 - This seems to be caused by a176b6d, where the type was changed from `int` to `long` without adjusting the parsing.
 - This caused an integer overflow, resulting in a negative value when e.g. the character limit exceeded `Integer::MAX_VAlUE`.
  • Loading branch information
Lukas Bolz committed Aug 30, 2024
1 parent 6fb600c commit bec8d3d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions deepl-java/src/main/java/com/deepl/api/parsing/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public String parseErrorMessage(String json) {
return jsonObject.get(parameterName).getAsInt();
}

static @Nullable Long getAsLongOrNull(JsonObject jsonObject, String parameterName) {
if (!jsonObject.has(parameterName)) return null;
return jsonObject.get(parameterName).getAsLong();
}

static @Nullable String getAsStringOrNull(JsonObject jsonObject, String parameterName) {
if (!jsonObject.has(parameterName)) return null;
return jsonObject.get(parameterName).getAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public Usage deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont
}

public static @Nullable Usage.Detail createDetail(JsonObject jsonObject, String prefix) {
Integer count = Parser.getAsIntOrNull(jsonObject, prefix + "count");
Integer limit = Parser.getAsIntOrNull(jsonObject, prefix + "limit");
Long count = Parser.getAsLongOrNull(jsonObject, prefix + "count");
Long limit = Parser.getAsLongOrNull(jsonObject, prefix + "limit");
if (count == null || limit == null) return null;
return new Usage.Detail(count, limit);
}
Expand Down

0 comments on commit bec8d3d

Please sign in to comment.