diff --git a/infer/src/checkers/costModels.ml b/infer/src/checkers/costModels.ml index ffb26b4c1..1e13ce0a2 100644 --- a/infer/src/checkers/costModels.ml +++ b/infer/src/checkers/costModels.ml @@ -49,8 +49,11 @@ end module JavaString = struct let substring_aux ~begin_idx ~end_v {integer_type_widths; location} inferbo_mem = let begin_v = BufferOverrunSemantics.eval integer_type_widths begin_idx inferbo_mem in + let begin_itv = BufferOverrunDomain.Val.get_itv begin_v in + let end_itv = BufferOverrunDomain.Val.get_itv end_v in let itv = - Itv.minus (BufferOverrunDomain.Val.get_itv end_v) (BufferOverrunDomain.Val.get_itv begin_v) + if Boolean.is_true (Itv.le_sem begin_itv end_itv) then Itv.minus end_itv begin_itv + else end_itv in CostUtils.of_itv ~itv ~degree_kind:Polynomials.DegreeKind.Linear ~of_function:"String.substring" location diff --git a/infer/tests/codetoanalyze/java/performance/StringTest.java b/infer/tests/codetoanalyze/java/performance/StringTest.java index e41d5c660..c6654fd9e 100644 --- a/infer/tests/codetoanalyze/java/performance/StringTest.java +++ b/infer/tests/codetoanalyze/java/performance/StringTest.java @@ -29,4 +29,9 @@ class StringTest { int index = indexof_linear(mId); return mId.substring(0, index); } + + private String startsWith_constant() { + String s = ""; + return s.startsWith(",") ? s.substring(1) : s; + } }