You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ASSERT_EQUALS("[test.c:5:1]: (information) --check-library: Function list_add_tail() should have <use>/<leak-ignore> configuration [checkLibraryUseIgnore]\n", errout_str());
1929
+
1930
+
check("struct S { int value; };\n"
1931
+
"void retain(void *);\n"
1932
+
"void f(int *state) {\n"
1933
+
" struct S *p = malloc(sizeof(*p));\n"
1934
+
" retain(&p->value);\n"
1935
+
" *state = 1;\n"
1936
+
"}\n");
1937
+
ASSERT_EQUALS("[test.c:7:1]: (information) --check-library: Function retain() should have <use>/<leak-ignore> configuration [checkLibraryUseIgnore]\n", errout_str());
1938
+
1939
+
check("struct S { int value; };\n"
1940
+
"void retain(void *);\n"
1941
+
"void f(int *state) {\n"
1942
+
" struct S *p = malloc(sizeof(*p));\n"
1943
+
" retain(&(p->value));\n"
1944
+
" *state = 1;\n"
1945
+
"}\n");
1946
+
ASSERT_EQUALS("[test.c:7:1]: (information) --check-library: Function retain() should have <use>/<leak-ignore> configuration [checkLibraryUseIgnore]\n", errout_str());
1947
+
1948
+
check("struct S { int value; };\n"
1949
+
"void retain(void *);\n"
1950
+
"void f(int *state) {\n"
1951
+
" struct S *p = malloc(sizeof(*p));\n"
1952
+
" retain((void *)&p->value);\n"
1953
+
" *state = 1;\n"
1954
+
"}\n");
1955
+
ASSERT_EQUALS("[test.c:7:1]: (information) --check-library: Function retain() should have <use>/<leak-ignore> configuration [checkLibraryUseIgnore]\n", errout_str());
1956
+
}
1957
+
1958
+
voidfunctioncallMemberAddressOwnership() {
1959
+
check("struct link { struct link *next; };\n"
1960
+
"struct item { int value; struct link entry; };\n"
1961
+
"struct link *head;\n"
1962
+
"void retain_entry(struct link *entry) {\n"
1963
+
" entry->next = head;\n"
1964
+
" head = entry;\n"
1965
+
"}\n"
1966
+
"void append_item(int value, int *state) {\n"
1967
+
" struct item *p = malloc(sizeof(*p));\n"
1968
+
" if (!p) return;\n"
1969
+
" p->value = value;\n"
1970
+
" retain_entry(&p->entry);\n"
1971
+
" *state = 1;\n"
1972
+
"}\n");
1973
+
ASSERT_EQUALS("", errout_str());
1974
+
}
1975
+
1976
+
voidfunctioncallMemberAddressDerived() {
1977
+
check("struct S { int value; };\n"
1978
+
"void observe(int);\n"
1979
+
"void f(int *saved, int *state) {\n"
1980
+
" struct S *p = malloc(sizeof(*p));\n"
1981
+
" observe(&p->value == saved);\n"
1982
+
" *state = 1;\n"
1983
+
"}\n");
1984
+
ASSERT_EQUALS("[test.c:7:1]: (error) Memory leak: p [memleak]\n", errout_str());
1985
+
1986
+
check("struct S { int value; };\n"
1987
+
"void observe(int);\n"
1988
+
"void f(int *state) {\n"
1989
+
" struct S *p = malloc(sizeof(*p));\n"
1990
+
" observe((&p->value, 1));\n"
1991
+
" *state = 1;\n"
1992
+
"}\n");
1993
+
ASSERT_EQUALS("[test.c:7:1]: (error) Memory leak: p [memleak]\n", errout_str());
1994
+
1995
+
check("struct S { int value; };\n"
1996
+
"void observe(long);\n"
1997
+
"void f(int *state) {\n"
1998
+
" struct S *p = malloc(sizeof(*p));\n"
1999
+
" observe((long)&p->value);\n"
2000
+
" *state = 1;\n"
2001
+
"}\n");
2002
+
ASSERT_EQUALS("[test.c:7:1]: (error) Memory leak: p [memleak]\n", errout_str());
2003
+
}
2004
+
2005
+
voidfunctioncallMemberAddressValue() {
2006
+
check("struct S { int value; };\n"
2007
+
"void observe(int);\n"
2008
+
"void f(int *state) {\n"
2009
+
" struct S *p = malloc(sizeof(*p));\n"
2010
+
" p->value = 1;\n"
2011
+
" observe(p->value);\n"
2012
+
" *state = 1;\n"
2013
+
"}\n");
2014
+
ASSERT_EQUALS("[test.c:8:1]: (error) Memory leak: p [memleak]\n", errout_str());
2015
+
}
2016
+
2017
+
voidfunctioncallMemberAddressIndirect() {
2018
+
check("struct S { int value; struct S *child; };\n"
2019
+
"void retain(int *);\n"
2020
+
"void f(struct S *child, int *state) {\n"
2021
+
" struct S *p = malloc(sizeof(*p));\n"
2022
+
" p->child = child;\n"
2023
+
" retain(&p->child->value);\n"
2024
+
" *state = 1;\n"
2025
+
"}\n");
2026
+
ASSERT_EQUALS("[test.c:8:1]: (error) Memory leak: p [memleak]\n", errout_str());
2027
+
2028
+
check("struct S { int *value; };\n"
2029
+
"void retain(int *);\n"
2030
+
"void f(int *value, int *state) {\n"
2031
+
" struct S *p = malloc(sizeof(*p));\n"
2032
+
" p->value = value;\n"
2033
+
" retain(p->value);\n"
2034
+
" *state = 1;\n"
2035
+
"}\n");
2036
+
ASSERT_EQUALS("[test.c:8:1]: (error) Memory leak: p [memleak]\n", errout_str());
2037
+
}
2038
+
2039
+
voidfunctioncallMemberAddressLeakIgnore() {
2040
+
check("struct S { int value; };\n"
2041
+
"void f(int *state) {\n"
2042
+
" struct S *p = malloc(sizeof(*p));\n"
2043
+
" memset(&p->value, 0, sizeof(p->value));\n"
2044
+
" *state = 1;\n"
2045
+
"}\n");
2046
+
ASSERT_EQUALS("[test.c:6:1]: (error) Memory leak: p [memleak]\n", errout_str());
ASSERT_EQUALS("[test.c:8:1]: (error) Memory leak: p [memleak]\n", errout_str());
2060
+
}
2061
+
2062
+
voidfunctioncallMemberAddressDoubleFree() {
2063
+
check("struct S { int value; };\n"
2064
+
"void retain(int *);\n"
2065
+
"void f() {\n"
2066
+
" struct S *p = malloc(sizeof(*p));\n"
2067
+
" retain(&p->value);\n"
2068
+
" free(p);\n"
2069
+
" free(p);\n"
2070
+
"}\n");
2071
+
ASSERT_EQUALS("[test.c:6:5] -> [test.c:7:5]: (error) Memory pointed to by 'p' is freed twice. [doubleFree]\n", errout_str());
2072
+
2073
+
check("struct S { int value; };\n"
2074
+
"void retain(int *);\n"
2075
+
"void f() {\n"
2076
+
" S *p = new S;\n"
2077
+
" retain(&p->value);\n"
2078
+
" free(p);\n"
2079
+
"}\n", dinit(CheckOptions, $.cpp = true));
2080
+
ASSERT_EQUALS("[test.cpp:4:12] -> [test.cpp:6:5]: (error) Mismatching allocation and deallocation: p [mismatchAllocDealloc]\n", errout_str());
2081
+
}
2082
+
2083
+
voidfunctioncallMemberAddressNested() {
2084
+
check("struct Entry { int value; };\n"
2085
+
"struct S { struct Entry embedded; };\n"
2086
+
"void retain(int *);\n"
2087
+
"void f(int *state) {\n"
2088
+
" struct S *p = malloc(sizeof(*p));\n"
2089
+
" retain(&p->embedded.value);\n"
2090
+
" *state = 1;\n"
2091
+
"}\n");
2092
+
ASSERT_EQUALS("[test.c:8:1]: (information) --check-library: Function retain() should have <use>/<leak-ignore> configuration [checkLibraryUseIgnore]\n", errout_str());
2093
+
}
2094
+
2095
+
voidfunctioncallMemberAddressCppStorage() {
2096
+
check("struct S { int value; };\n"
2097
+
"void retain(int *);\n"
2098
+
"void f(int *state) {\n"
2099
+
" S *p = new S;\n"
2100
+
" retain(&p->value);\n"
2101
+
" *state = 1;\n"
2102
+
"}\n", dinit(CheckOptions, $.cpp = true));
2103
+
ASSERT_EQUALS("[test.cpp:7:1]: (information) --check-library: Function retain() should have <use>/<leak-ignore> configuration [checkLibraryUseIgnore]\n", errout_str());
2104
+
2105
+
check("struct S { int *value; };\n"
2106
+
"void retain(int **);\n"
2107
+
"void f(int *state) {\n"
2108
+
" S *p = new S;\n"
2109
+
" retain(&p->value);\n"
2110
+
" *state = 1;\n"
2111
+
"}\n", dinit(CheckOptions, $.cpp = true));
2112
+
ASSERT_EQUALS("[test.cpp:7:1]: (information) --check-library: Function retain() should have <use>/<leak-ignore> configuration [checkLibraryUseIgnore]\n", errout_str());
2113
+
2114
+
check("struct S { static int value; };\n"
2115
+
"void retain(int *);\n"
2116
+
"void f(int *state) {\n"
2117
+
" S *p = new S;\n"
2118
+
" retain(&p->value);\n"
2119
+
" *state = 1;\n"
2120
+
"}\n", dinit(CheckOptions, $.cpp = true));
2121
+
ASSERT_EQUALS("[test.cpp:7:1]: (error) Memory leak: p [memleak]\n", errout_str());
2122
+
2123
+
check("struct S { int &value; };\n"
2124
+
"void retain(int *);\n"
2125
+
"void f(int &value, int *state) {\n"
2126
+
" S *p = new S{value};\n"
2127
+
" retain(&p->value);\n"
2128
+
" *state = 1;\n"
2129
+
"}\n", dinit(CheckOptions, $.cpp = true));
2130
+
ASSERT_EQUALS("[test.cpp:7:1]: (error) Memory leak: p [memleak]\n", errout_str());
2131
+
2132
+
check("struct Entry { int *operator&(); };\n"
2133
+
"struct S { Entry entry; };\n"
2134
+
"void retain(int *);\n"
2135
+
"void f(int *state) {\n"
2136
+
" S *p = (S *)malloc(sizeof(S));\n"
2137
+
" retain(&p->entry);\n"
2138
+
" *state = 1;\n"
2139
+
"}\n", dinit(CheckOptions, $.cpp = true));
2140
+
ASSERT_EQUALS("[test.cpp:8:1]: (error) Memory leak: p [memleak]\n", errout_str());
2141
+
2142
+
check("struct Entry { int value; };\n"
2143
+
"struct Link { Entry *operator->(); };\n"
2144
+
"struct S { Link link; };\n"
2145
+
"void retain(int *);\n"
2146
+
"void f(int *state) {\n"
2147
+
" S *p = (S *)malloc(sizeof(S));\n"
2148
+
" retain(&p->link->value);\n"
2149
+
" *state = 1;\n"
2150
+
"}\n", dinit(CheckOptions, $.cpp = true));
2151
+
ASSERT_EQUALS("[test.cpp:9:1]: (error) Memory leak: p [memleak]\n", errout_str());
0 commit comments