Skip to content

Commit

Permalink
[annot] Mention constructors explicitly
Browse files Browse the repository at this point in the history
Summary: In the error message, be more specific. If a method is a constructor, then call it a constructor.

Reviewed By: ngorogiannis

Differential Revision:
D68017290

Privacy Context Container: L1208441

fbshipit-source-id: 3a7638ffc6b63765621e8e90b4f53310af316f51
  • Loading branch information
hajduakos authored and facebook-github-bot committed Jan 10, 2025
1 parent 6388ae8 commit 007abe2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
14 changes: 8 additions & 6 deletions infer/src/checkers/annotationReachability.ml
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,18 @@ let report_src_to_snk_path {InterproceduralAnalysis.proc_desc; tenv; err_log} sr
let spec_name = append_if_not_empty spec.name ". " in
(* A direct call has a trace of length 3: source def + callsite + sink def *)
let transitive = if List.length trace > 3 then "transitively " else "" in
let method_or_constr = if Procname.is_constructor src_pname then "Constructor" else "Method" in
let description =
if is_dummy_constructor snk then
let constr_str = str_of_pname ~withclass:true snk_pname in
Format.asprintf "%sMethod %a annotated with %a allocates %a via %a%s" spec_name
Format.asprintf "%s%s %a annotated with %a allocates %a via %a%s" spec_name method_or_constr
MF.pp_monospaced (str_of_pname src_pname) MF.pp_monospaced ("@" ^ src_annot_str)
MF.pp_monospaced constr_str MF.pp_monospaced ("new " ^ constr_str) spec_description
else
Format.asprintf "%sMethod %a (%s %a%s%s) %s%s %a (%s %a%s%s)%s" spec_name MF.pp_monospaced
(str_of_pname src_pname) (get_kind src src_pname) MF.pp_monospaced ("@" ^ src_annot_str)
(get_details src src_pname) (get_class_details src src_pname) transitive access_or_call
MF.pp_monospaced
Format.asprintf "%s%s %a (%s %a%s%s) %s%s %a (%s %a%s%s)%s" spec_name method_or_constr
MF.pp_monospaced (str_of_pname src_pname) (get_kind src src_pname) MF.pp_monospaced
("@" ^ src_annot_str) (get_details src src_pname) (get_class_details src src_pname)
transitive access_or_call MF.pp_monospaced
(str_of_pname ~withclass:true snk_pname)
(get_kind snk snk_pname) MF.pp_monospaced ("@" ^ snk_annot_str) (get_details snk snk_pname)
(get_class_details snk snk_pname) spec_description
Expand All @@ -242,7 +243,8 @@ let report_src_to_snk_path {InterproceduralAnalysis.proc_desc; tenv; err_log} sr

let start_trace proc_desc annot =
let description =
"Method "
( if Procname.is_constructor (Procdesc.get_proc_name proc_desc) then "Constructor "
else "Method " )
^ str_of_pname (Procdesc.get_proc_name proc_desc)
^ ", marked as source @" ^ annot.Annot.class_name
in
Expand Down
13 changes: 13 additions & 0 deletions infer/tests/codetoanalyze/java/annotreach/CustomAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,17 @@ public void source(Dummy d) {
@UserDefinedSink1
public void sink() {}
}

// Testing implicit constructors
class BaseConstructorCallsSink {
public BaseConstructorCallsSink() {
sink();
}

@UserDefinedSink1
public void sink() {}
}

@UserDefinedSource1
class DerivedWithSourceConstructor extends BaseConstructorCallsSink {}
}
1 change: 1 addition & 0 deletions infer/tests/codetoanalyze/java/annotreach/issues.exp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ codetoanalyze/java/annotreach/CustomAnnotations.java, codetoanalyze.java.checker
codetoanalyze/java/annotreach/CustomAnnotations.java, codetoanalyze.java.checkers.CustomAnnotations.accessesDeprecatedFieldBad():int, 1, CHECKERS_ANNOTATION_REACHABILITY_ERROR, no_bucket, ERROR, [Method accessesDeprecatedFieldBad(), marked as source @AnySource,accesses deprecatedField]
codetoanalyze/java/annotreach/CustomAnnotations.java, codetoanalyze.java.checkers.CustomAnnotations.callsDeprecatedMethodBad():void, 1, CHECKERS_ANNOTATION_REACHABILITY_ERROR, no_bucket, ERROR, [Method callsDeprecatedMethodBad(), marked as source @AnySource,calls deprecatedMethod(),deprecatedMethod() defined here, marked as sink @Deprecated]
codetoanalyze/java/annotreach/CustomAnnotations.java, codetoanalyze.java.checkers.CustomAnnotations$GenericDerived.source(java.lang.Object):void, 8, CHECKERS_ANNOTATION_REACHABILITY_ERROR, no_bucket, ERROR, [Method source(...), marked as source @UserDefinedSource1,calls source(...),source(...) defined here,calls sink(),sink() defined here, marked as sink @UserDefinedSink1]
codetoanalyze/java/annotreach/CustomAnnotations.java, codetoanalyze.java.checkers.CustomAnnotations$DerivedWithSourceConstructor.<init>(codetoanalyze.java.checkers.CustomAnnotations), 0, CHECKERS_ANNOTATION_REACHABILITY_ERROR, no_bucket, ERROR, [Constructor CustomAnnotations$DerivedWithSourceConstructor(...), marked as source @UserDefinedSource1,calls CustomAnnotations$BaseConstructorCallsSink(...),CustomAnnotations$BaseConstructorCallsSink(...) defined here,calls sink(),sink() defined here, marked as sink @UserDefinedSink1]
codetoanalyze/java/annotreach/ExpensiveCallExample.java, codetoanalyze.java.checkers.PerformanceCriticalClass.performanceCriticalMethod1(codetoanalyze.java.checkers.ExpensiveClass):void, 1, CHECKERS_CALLS_EXPENSIVE_METHOD, no_bucket, ERROR, [Method performanceCriticalMethod1(...), marked as source @PerformanceCritical,calls anExpensiveMethod(),anExpensiveMethod() defined here, marked as sink @Expensive]
codetoanalyze/java/annotreach/ExpensiveCallExample.java, codetoanalyze.java.checkers.PerformanceCriticalClass.performanceCriticalMethod2(codetoanalyze.java.checkers.Other):void, 1, CHECKERS_CALLS_EXPENSIVE_METHOD, no_bucket, ERROR, [Method performanceCriticalMethod2(...), marked as source @PerformanceCritical,calls expensive(),expensive() defined here, marked as sink @Expensive]
codetoanalyze/java/annotreach/ExpensiveCallExample.java, codetoanalyze.java.checkers.PerformanceCriticalClass.performanceCriticalMethod3(codetoanalyze.java.checkers.Other):void, 1, CHECKERS_CALLS_EXPENSIVE_METHOD, no_bucket, ERROR, [Method performanceCriticalMethod3(...), marked as source @PerformanceCritical,calls callsExpensive1(),callsExpensive1() defined here,calls expensive(),expensive() defined here, marked as sink @Expensive]
Expand Down

0 comments on commit 007abe2

Please sign in to comment.