diff --git a/infer/tests/codetoanalyze/java/performance/ArrayCost.java b/infer/tests/codetoanalyze/java/performance/ArrayCost.java index 5f5383cfc..ce7894830 100644 --- a/infer/tests/codetoanalyze/java/performance/ArrayCost.java +++ b/infer/tests/codetoanalyze/java/performance/ArrayCost.java @@ -19,4 +19,21 @@ public class ArrayCost { } } + + private static boolean isPowOfTwo_FP(int value) { + int ones = 0; + int v = value; + + for (int shifts = 0; shifts < 31 && ones <= 1; shifts++) { + if ((v & 1) == 1) { + ones++; + } + + v >>= 1; + } + + return ones == 1; + } + + } diff --git a/infer/tests/codetoanalyze/java/performance/JsonArray.java b/infer/tests/codetoanalyze/java/performance/JsonArray.java new file mode 100644 index 000000000..7cb664302 --- /dev/null +++ b/infer/tests/codetoanalyze/java/performance/JsonArray.java @@ -0,0 +1,17 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +package libraries.marauder.analytics.utils.json; + +public class JsonArray implements JsonType { + + public StringBuilder array = new StringBuilder("["); + + + public void addStringEntry(String value) { + if (array.length() != 1) { + array.append(","); + } + JsonUtils.serialize(array, value); + } + +} diff --git a/infer/tests/codetoanalyze/java/performance/JsonMap.java b/infer/tests/codetoanalyze/java/performance/JsonMap.java new file mode 100644 index 000000000..d5cac4358 --- /dev/null +++ b/infer/tests/codetoanalyze/java/performance/JsonMap.java @@ -0,0 +1,52 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +package libraries.marauder.analytics.utils.json; + +public class JsonMap implements JsonType { + + public StringBuilder map = new StringBuilder("{"); + + public void addEntry(String key, JsonType value) { + if (!JsonUtils.isValidInputType(value)) { + //throw new IllegalStateException("illegal input type " + value); + } + + addKeyToMap(key); + map.append(value.toString()); + } + + public void addEntry(String key, String value) { + addKeyToMap(key); + JsonUtils.serialize(map, value); + } + + public void addEntry(String key, long value) { + addKeyToMap(key); + JsonUtils.serialize(map, value); + } + + public void addEntry(String key, double value) { + addKeyToMap(key); + JsonUtils.serialize(map, value); + } + + public void addEntry(String key, boolean value) { + addKeyToMap(key); + JsonUtils.serialize(map, value); + } + + public void addEntry(String key, Object value) { + addKeyToMap(key); + JsonUtils.serialize(map, value); + } + + + private void addKeyToMap(String key) { + if (map.length() != 1) { + map.append(","); + } + + JsonUtils.serialize(map, key); + map.append(":"); + } +} diff --git a/infer/tests/codetoanalyze/java/performance/JsonString.java b/infer/tests/codetoanalyze/java/performance/JsonString.java new file mode 100644 index 000000000..cb5dec6d3 --- /dev/null +++ b/infer/tests/codetoanalyze/java/performance/JsonString.java @@ -0,0 +1,29 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +package libraries.marauder.analytics.utils.json; + +public class JsonString implements JsonType { + + public String array; + + public JsonString(String input) { + array = JsonUtils.serialize(input).toString(); + } + + public JsonString(long input) { + array = JsonUtils.serialize(input); + } + + public JsonString(double input) { + array = JsonUtils.serialize(input); + } + + public JsonString(boolean input) { + array = JsonUtils.serialize(input); + } + + public String toString() { + return array; + } + +} diff --git a/infer/tests/codetoanalyze/java/performance/JsonType.java b/infer/tests/codetoanalyze/java/performance/JsonType.java new file mode 100644 index 000000000..869496828 --- /dev/null +++ b/infer/tests/codetoanalyze/java/performance/JsonType.java @@ -0,0 +1,7 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +package libraries.marauder.analytics.utils.json; + +public interface JsonType { + +} diff --git a/infer/tests/codetoanalyze/java/performance/JsonUtils.java b/infer/tests/codetoanalyze/java/performance/JsonUtils.java new file mode 100644 index 000000000..51ed4e228 --- /dev/null +++ b/infer/tests/codetoanalyze/java/performance/JsonUtils.java @@ -0,0 +1,85 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +package libraries.marauder.analytics.utils.json; + +public class JsonUtils { + + private static void escape(StringBuilder builder, String input) { + for (Character c : input.toCharArray()) { + if (Character.getType(c) == Character.CONTROL) { + builder.append(String.format("\\u%04x", (int)c)); + } else if (c == '\\' || c== '\"') { + builder.append('\\').append(c); + } else { + builder.append(c); + } + } + } + + public static StringBuilder serialize(String input) { + StringBuilder stringBuilder = new StringBuilder(); + serialize(stringBuilder, input); + return stringBuilder; + } + + public static String serialize(long input) { + return String.valueOf(input); + } + + public static String serialize(boolean input) { + return String.valueOf(input); + } + + public static String serialize(double input) { + return String.valueOf(input); + } + + public static String serialize(Object input) { + if (input instanceof Integer + || input instanceof Float + || input instanceof Long + || input instanceof Double + || input instanceof Boolean + || input instanceof Byte + || input instanceof Short) { + return "";//String.valueOf(input); + } else { + return "";// serialize(String.valueOf(input)).toString(); + } + } + + public static void serialize(StringBuilder out, String input) { + if (input == null) { + out.append("null"); + } else { + out.append("\""); + escape(out, input); + out.append("\""); + } + } + + public static void serialize(StringBuilder out, long input) { + out.append(serialize(input)); + } + + public static void serialize(StringBuilder out, boolean input) { + out.append(serialize(input)); + } + + public static void serialize(StringBuilder out, double input) { + out.append(serialize(input)); + } + + public static void serialize(StringBuilder out, Object input) { + out.append(serialize(input)); + } + + public static boolean isValidInputType(Object value) { + if (value instanceof JsonString || + value instanceof JsonArray || + value instanceof JsonMap) { + return true; + } + return false; + } +} diff --git a/infer/tests/codetoanalyze/java/performance/issues.exp b/infer/tests/codetoanalyze/java/performance/issues.exp index 7b6823da8..15b5625b2 100644 --- a/infer/tests/codetoanalyze/java/performance/issues.exp +++ b/infer/tests/codetoanalyze/java/performance/issues.exp @@ -1 +1,15 @@ +codetoanalyze/java/performance/ArrayCost.java, boolean ArrayCost.isPowOfTwo_FP(int), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] codetoanalyze/java/performance/ArrayCost.java, void ArrayCost.ArrayCost_FP(int,int[]), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonArray.java, void JsonArray.addStringEntry(String), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonMap.java, void JsonMap.addEntry(String,JsonType), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonMap.java, void JsonMap.addEntry(String,Object), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonMap.java, void JsonMap.addEntry(String,String), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonMap.java, void JsonMap.addEntry(String,boolean), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonMap.java, void JsonMap.addEntry(String,double), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonMap.java, void JsonMap.addEntry(String,long), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonMap.java, void JsonMap.addKeyToMap(String), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonString.java, JsonString.(String), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonUtils.java, StringBuilder JsonUtils.serialize(String), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonUtils.java, void JsonUtils.escape(StringBuilder,String), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, [] +codetoanalyze/java/performance/JsonUtils.java, void JsonUtils.escape(StringBuilder,String), 1, BUFFER_OVERRUN_U5, ERROR, [Unknown value from: char[] String.toCharArray(),Assignment,ArrayAccess: Offset: [-oo, +oo] Size: [0, +oo]] +codetoanalyze/java/performance/JsonUtils.java, void JsonUtils.serialize(StringBuilder,String), 0, INFINITE_EXECUTION_TIME_CALL, ERROR, []