From 25627fd4d920f3328b13d9ebac2044a1eae9a124 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Tue, 19 Jun 2018 05:21:29 -0700 Subject: [PATCH] [c] add test for cleanup attribute Summary: Record that it is not supported: https://github.com/facebook/infer/issues/8 Reviewed By: mbouaziz, dulmarod Differential Revision: D8442762 fbshipit-source-id: fa271cb --- infer/tests/codetoanalyze/c/errors/issues.exp | 2 ++ .../c/errors/memory_leaks/cleanup_attribute.c | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 infer/tests/codetoanalyze/c/errors/memory_leaks/cleanup_attribute.c diff --git a/infer/tests/codetoanalyze/c/errors/issues.exp b/infer/tests/codetoanalyze/c/errors/issues.exp index f980798dc..6b13f594b 100644 --- a/infer/tests/codetoanalyze/c/errors/issues.exp +++ b/infer/tests/codetoanalyze/c/errors/issues.exp @@ -26,6 +26,8 @@ codetoanalyze/c/errors/local_vars/local_vars.c, m1, 6, DIVIDE_BY_ZERO, no_bucket codetoanalyze/c/errors/local_vars/local_vars.c, m2, 9, DIVIDE_BY_ZERO, no_bucket codetoanalyze/c/errors/local_vars/local_vars.c, mm, 6, DIVIDE_BY_ZERO, no_bucket codetoanalyze/c/errors/local_vars/local_vars.c, t, 8, DIVIDE_BY_ZERO, no_bucket +codetoanalyze/c/errors/memory_leaks/cleanup_attribute.c, FP_cleanup_malloc_good, 4, MEMORY_LEAK, no_bucket +codetoanalyze/c/errors/memory_leaks/cleanup_attribute.c, FP_cleanup_string_good, 2, MEMORY_LEAK, no_bucket codetoanalyze/c/errors/memory_leaks/test.c, common_realloc_leak, 3, MEMORY_LEAK, no_bucket codetoanalyze/c/errors/memory_leaks/test.c, common_realloc_leak, 5, NULL_TEST_AFTER_DEREFERENCE, no_bucket codetoanalyze/c/errors/memory_leaks/test.c, conditional_last_instruction, 2, MEMORY_LEAK, no_bucket diff --git a/infer/tests/codetoanalyze/c/errors/memory_leaks/cleanup_attribute.c b/infer/tests/codetoanalyze/c/errors/memory_leaks/cleanup_attribute.c new file mode 100644 index 000000000..ef01ee257 --- /dev/null +++ b/infer/tests/codetoanalyze/c/errors/memory_leaks/cleanup_attribute.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#include +#include + +void cleanup_char(char** x) { free(*x); } + +void cleanup_int(int** x) { free(*x); } + +// related to https://github.com/facebook/infer/issues/8 +void FP_cleanup_malloc_good() { + __attribute__((cleanup(cleanup_int))) int* x; + x = malloc(sizeof(int)); + if (x != NULL) { + *x = 10; + } + /* s goes out of scope. Cleanup function called - no leak */ +} + +// related to https://github.com/facebook/infer/issues/8 +void FP_cleanup_string_good() { + __attribute__((cleanup(cleanup_char))) char* s; + s = strdup("demo string"); + /* s goes out of scope. Cleanup function called - no leak */ +}