Skip to content

Add error handling to Python API calls in LDAPraise_for_message #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

encukou
Copy link
Member

@encukou encukou commented Jun 5, 2020

Add CPython style error handling to LDAPraise_for_message: ensure pointers are set to NULL when freed, and free all non-NULL pointers at the end. Use goto to jump to the cleanup. (The label is usually called error, which here clashes with a variable name).

Note that Python API sets the current exception on failure. If something else than C API fails, PyErr_NoMemory or PyErr_SetObject is called before returning NULL.

I recommend reviewing the diff with whitespace changes hidden (-w in Git CLI, ?w=1 in GitHub URLs).

@codecov
Copy link

codecov bot commented Jun 5, 2020

Codecov Report

Merging #341 (7067217) into master (39ea8e5) will decrease coverage by 3.01%.
The diff coverage is 50.41%.

❗ Current head 7067217 differs from pull request most recent head 43e8e75. Consider uploading reports for the commit 43e8e75 to get more accurate results

@@            Coverage Diff             @@
##           master     #341      +/-   ##
==========================================
- Coverage   71.30%   68.29%   -3.02%     
==========================================
  Files          50       49       -1     
  Lines        4796     4861      +65     
  Branches      802      820      +18     
==========================================
- Hits         3420     3320     -100     
- Misses       1045     1187     +142     
- Partials      331      354      +23     
Impacted Files Coverage Δ
Modules/constants.c 47.71% <50.41%> (-5.80%) ⬇️
Lib/ldap/compat.py 18.86% <0.00%> (-48.41%) ⬇️
Lib/ldap/cidict.py 69.23% <0.00%> (-27.27%) ⬇️
Lib/ldap/functions.py 80.00% <0.00%> (-17.50%) ⬇️
Lib/ldap/ldapobject.py 64.29% <0.00%> (-15.94%) ⬇️
Lib/ldap/__init__.py 76.92% <0.00%> (-13.47%) ⬇️
Lib/ldap/extop/__init__.py 55.55% <0.00%> (-12.87%) ⬇️
Lib/ldap/sasl.py 75.00% <0.00%> (-11.12%) ⬇️
Modules/ldapmodule.c 59.09% <0.00%> (-4.91%) ⬇️
Lib/ldap/dn.py 94.23% <0.00%> (-3.85%) ⬇️
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d9ded15...43e8e75. Read the comment docs.

@tiran tiran self-assigned this Jun 5, 2020
@tiran
Copy link
Member

tiran commented Jun 5, 2020

It'll review the PR next week or the week after. Too much C code for a Friday afternoon!

@encukou encukou added this to the 3.3 milestone Jun 5, 2020
@tiran tiran modified the milestones: 3.3, future Jun 8, 2020
@encukou encukou removed this from the 3.3 milestone Jun 8, 2020
PyErr_SetObject(errobj, info);
Py_DECREF(info);
ldap_memvfree((void **)refs);
PyErr_SetObject(errobj, info);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get a segfault here:

test105_reconnect_restore (Tests.t_ldapobject.Test01_ReconnectLDAPObject) ... make: *** [Makefile:72: valgrind] Segmentation fault (core dumped)

…_message

- Ensure pointers are set to NULL when freed.
- Free all non-NULL pointers at the end.
- Use goto to jump to the cleanup.

Note that Python API sets the current exception on failure. If
something else than C API fails, PyErr_NoMemory or PyErr_SetObject
is called before returning NULL.

I recommend reviewing the diff with whitespace changes hidden
(`-w` in Git CLI).
if (m != NULL) {
msgid = ldap_msgid(m);
msgtype = ldap_msgtype(m);
ldap_parse_result(l, m, &errnum, &matched, &error, &refs,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function call is missing an error check:

Upon success LDAP_SUCCESS is returned. Otherwise the values of the result parameters are undefined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean they should not be freed, like this?

            opt_errnum = ldap_parse_result(l, m, &errnum, &matched, &error,
                                           &refs, &serverctrls, 1);
            if (opt_errnum != LDAP_SUCCESS) {
                // values of the result parameters are undefined; zero them
                matched = NULL;
                error = NULL;
                refs = NULL;
                serverctrls = NULL;
                errnum = opt_errnum;
            }

@encukou encukou mentioned this pull request Sep 17, 2021
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants