From 89c8e25debeef4cded1857c88ab22774c5a60933 Mon Sep 17 00:00:00 2001 From: Sungkeun Cho Date: Fri, 29 Jan 2021 09:59:36 -0800 Subject: [PATCH] [frontend] Add tests of using single field struct Summary: When a single field struct is initialized with "type x{v}" form, the translated result is not straightforward. For example, ``` struct t { int val_; }; void foo(t x) { t y{x}; } ``` calls the copy constructor with `x`. This is good. ``` void foo(int n) { t y{n}; } ``` assigns the integer `n` to `y.val_`. This is good. ``` t get_v(); void foo() { t y{get_v()}; } ``` assigns return value of `get_v` to `y.val_`, rather than calling the copy constructor. This is not good, but doesn't matter for actual running; `&y.val_` is the same to `&y` and `t` value is the same to `int` value. Reviewed By: jvillard Differential Revision: D26146578 fbshipit-source-id: 8a81bb1db --- .../codetoanalyze/cpp/pulse/frontend.cpp | 38 +++++++++++++++++++ .../tests/codetoanalyze/cpp/pulse/issues.exp | 1 + 2 files changed, 39 insertions(+) diff --git a/infer/tests/codetoanalyze/cpp/pulse/frontend.cpp b/infer/tests/codetoanalyze/cpp/pulse/frontend.cpp index 7ef02bfe5..bae143d8b 100644 --- a/infer/tests/codetoanalyze/cpp/pulse/frontend.cpp +++ b/infer/tests/codetoanalyze/cpp/pulse/frontend.cpp @@ -203,4 +203,42 @@ void not_boolean_bad() { } } +struct double_fields_struct { + int v; + int a; +}; + +double_fields_struct get_double_fields_struct() { + double_fields_struct b; + b.v = 42; + b.a = 42; + return b; +} + +void init_double_fields_struct_ok() { + double_fields_struct y{get_double_fields_struct()}; + if (y.v != 42) { + int* p = nullptr; + *p = 42; + } +} + +struct single_field_struct { + int v; +}; + +single_field_struct get_single_field_struct() { + single_field_struct b; + b.v = 42; + return b; +} + +void FP_init_single_field_struct_ok() { + single_field_struct y{get_single_field_struct()}; + if (y.v != 42) { + int* p = nullptr; + *p = 42; + } +} + } // namespace frontend diff --git a/infer/tests/codetoanalyze/cpp/pulse/issues.exp b/infer/tests/codetoanalyze/cpp/pulse/issues.exp index d83d45b4b..e952a1d72 100644 --- a/infer/tests/codetoanalyze/cpp/pulse/issues.exp +++ b/infer/tests/codetoanalyze/cpp/pulse/issues.exp @@ -32,6 +32,7 @@ codetoanalyze/cpp/pulse/deduplication.cpp, deduplication::templated_function_bad codetoanalyze/cpp/pulse/deduplication.cpp, deduplication::templated_function_bad, 3, USE_AFTER_DELETE, no_bucket, ERROR, [invalidation part of the trace starts here,passed as argument to `new` (modelled),return from call to `new` (modelled),assigned,when calling `deduplication::templated_delete_function` here,parameter `a` of deduplication::templated_delete_function,was invalidated by `delete`,use-after-lifetime part of the trace starts here,passed as argument to `new` (modelled),return from call to `new` (modelled),assigned,when calling `deduplication::templated_access_function` here,parameter `a` of deduplication::templated_access_function,invalid access occurs here] codetoanalyze/cpp/pulse/exit_test.cpp, store_exit_null_bad, 0, NULLPTR_DEREFERENCE, no_bucket, ERROR, [invalidation part of the trace starts here,assigned,is the null pointer,use-after-lifetime part of the trace starts here,when calling `store_exit` here,parameter `x` of store_exit,invalid access occurs here] codetoanalyze/cpp/pulse/folly_DestructorGuard.cpp, UsingDelayedDestruction::double_delete_bad, 2, USE_AFTER_DELETE, no_bucket, ERROR, [invalidation part of the trace starts here,parameter `this` of UsingDelayedDestruction::double_delete_bad,was invalidated by `delete`,use-after-lifetime part of the trace starts here,parameter `this` of UsingDelayedDestruction::double_delete_bad,invalid access occurs here] +codetoanalyze/cpp/pulse/frontend.cpp, frontend::FP_init_single_field_struct_ok, 4, NULLPTR_DEREFERENCE, no_bucket, ERROR, [invalidation part of the trace starts here,assigned,is the null pointer,use-after-lifetime part of the trace starts here,assigned,invalid access occurs here] codetoanalyze/cpp/pulse/frontend.cpp, frontend::call_Frontend_constructor2_bad, 5, NULLPTR_DEREFERENCE, no_bucket, ERROR, [invalidation part of the trace starts here,assigned,is the null pointer,use-after-lifetime part of the trace starts here,assigned,invalid access occurs here] codetoanalyze/cpp/pulse/frontend.cpp, frontend::call_Frontend_constructor_bad, 4, NULLPTR_DEREFERENCE, no_bucket, ERROR, [invalidation part of the trace starts here,assigned,is the null pointer,use-after-lifetime part of the trace starts here,assigned,invalid access occurs here] codetoanalyze/cpp/pulse/frontend.cpp, frontend::call_set_field_via_local_bad, 5, NULLPTR_DEREFERENCE, no_bucket, ERROR, [invalidation part of the trace starts here,assigned,is the null pointer,use-after-lifetime part of the trace starts here,assigned,invalid access occurs here]