Skip to content

Commit 084ffe0

Browse files
authored
Merge pull request #83 – Add valgrind target to check for memory leaks
#83
2 parents eb9dbc1 + c85f432 commit 084ffe0

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Infrastructure:
2626
* Remove distclean.sh in favor of make clean
2727
* Use `package`, `depends`, `install_requires` in setup.py
2828
* Add make target for scan-build (static analysis using clang)
29+
* Add make target and suppression file for Valgrind (memory checker)
2930

3031
Modules/
3132
* Remove unused LDAPberval helper functions

Doc/contributing.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,33 @@ We use several specialized tools for debugging and maintenance.
168168
Make targets
169169
------------
170170

171-
``make lcov-open``
171+
Make targets currently use the ``python3`` executable.
172+
Specify a different one using, for example::
173+
174+
make PYTHON=/usr/local/bin/python
175+
176+
Notable targets are:
177+
178+
``make lcov lcov-open``
172179
Generate and view test coverage for C code.
173-
Requires ``make`` and ``lcov``.
180+
Requires LCOV_.
174181

175182
``make scan-build``
176183
Run static analysis. Requires ``clang``.
177184

185+
``make valgrind``
186+
Run Valgrind_ to check for memory leaks. Requires ``valgrind`` and
187+
a Python suppression file, which you can specify as ``PYTHON_SUPP``, e.g.::
188+
189+
make valgrind PYTHON_SUPP=/your/path/to/valgrind-python.supp
190+
191+
The suppression file is ``Misc/valgrind-python.supp`` in the Python
192+
source distribution, and it's frequently packaged together with
193+
Python development headers.
194+
195+
.. _LCOV: https://github.com/linux-test-project/lcov
196+
.. _Valgrind: http://valgrind.org/
197+
178198

179199
Reference leak tests
180200
--------------------

Doc/spelling_wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,6 @@ userApplications
150150
userPassword
151151
usr
152152
uuids
153+
Valgrind
153154
whitespace
154155
workflow

Makefile

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ LCOV_REPORT=build/lcov_report
44
LCOV_REPORT_OPTIONS=--show-details -no-branch-coverage \
55
--title "python-ldap LCOV report"
66
SCAN_REPORT=build/scan_report
7+
PYTHON_SUPP=/usr/share/doc/python3-devel/valgrind-python.supp
78

89
.NOTPARALLEL:
910

@@ -18,6 +19,9 @@ clean:
1819
-delete
1920
find . -depth -name __pycache__ -exec rm -rf {} \;
2021

22+
build:
23+
mkdir -p build
24+
2125
# LCOV report (measuring test coverage for C code)
2226
.PHONY: lcov-clean lcov-coverage lcov-report lcov-open lcov
2327
lcov-clean:
@@ -27,7 +31,7 @@ lcov-clean:
2731
lcov-coverage:
2832
WITH_GCOV=1 tox -e py27,py36
2933

30-
$(LCOV_INFO):
34+
$(LCOV_INFO): build
3135
lcov --capture --directory build --output-file $(LCOV_INFO)
3236

3337
$(LCOV_REPORT): $(LCOV_INFO)
@@ -49,3 +53,19 @@ scan-build:
4953
scan-build -o $(SCAN_REPORT) --html-title="python-ldap scan report" \
5054
-analyze-headers --view \
5155
$(PYTHON) setup.py clean --all build
56+
57+
# valgrind memory checker
58+
.PHONY: valgrind
59+
$(PYTHON_SUPP):
60+
@ >&2 echo "valgrind-python.supp not found"
61+
@ >&2 echo "install Python development files and run:"
62+
@ >&2 echo " $(MAKE) valgrind PYTHON_SUPP=/your/path/to/valgrind-python.supp"
63+
exit 1;
64+
65+
valgrind: build $(PYTHON_SUPP)
66+
valgrind --leak-check=full \
67+
--suppressions=$(PYTHON_SUPP) \
68+
--suppressions=Misc/python-ldap.supp \
69+
--gen-suppressions=all \
70+
--log-file=build/valgrind.log \
71+
$(PYTHON) setup.py test

Misc/python-ldap.supp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Valgrind suppression file for Python 3.6.
2+
3+
{
4+
Ignore libldap memory leak, https://github.com/python-ldap/python-ldap/issues/82
5+
Memcheck:Leak
6+
match-leak-kinds: definite
7+
fun:malloc
8+
fun:ber_memalloc_x
9+
fun:ber_flatten
10+
fun:ldap_cancel
11+
fun:l_ldap_cancel
12+
...
13+
}
14+
15+
{
16+
Known leak in SASL interaction, https://github.com/python-ldap/python-ldap/issues/81
17+
Memcheck:Leak
18+
match-leak-kinds: definite
19+
fun:malloc
20+
fun:strdup
21+
fun:interaction
22+
fun:py_ldap_sasl_interaction
23+
fun:ldap_int_sasl_bind
24+
fun:ldap_sasl_interactive_bind
25+
fun:ldap_sasl_interactive_bind_s
26+
fun:l_ldap_sasl_interactive_bind_s
27+
...
28+
}
29+
30+
{
31+
Ignore possible leaks in exception initialization
32+
Memcheck:Leak
33+
match-leak-kinds: possible
34+
fun:malloc
35+
fun:PyObject_Malloc
36+
...
37+
fun:PyErr_NewException
38+
fun:LDAPinit_constants
39+
fun:init_ldap_module
40+
...
41+
}

0 commit comments

Comments
 (0)