Skip to content

Clean up memory handling in attrs_from_List() #105

@tiran

Description

@tiran

The function

attrs_from_List( PyObject *attrlist, char***attrsp, PyObject** seq) {
has a rather peculiar way to handle memory. It takes a sequence of Python bytes / text and converts it to a NULL terminated array of char *. It allocates memory for the array itself but uses PyUnicode_AsUTF8 or PyBytes_AsString for the elements inside the array. Both functions do not return a copy of the string but rather a reference to an internal buffer. The internal buffer is shared between callers and must be treated as const, immutable block. In fact in Python 3.7+, the return value of PyUnicode_AsUTF8 has been changed to const char*, https://docs.python.org/3.7/c-api/unicode.html#c.PyUnicode_AsUTF8AndSize . However ldap_search_ext does not take a const char*[] but a char *attrs[]. This breaks the const promise starting with Python 3.7.

Even more peculiar is the seq argument. It is used to pass back a reference to the Python sequence that holds the PyObject *. This trick is used to keep the Python sequence and all its members alive, so the elements in the array of char * do not get deallocated by Python.

I suggest to change the function to copy the strings to allocated buffers instead of (ab)using Python objects to provide the buffer. The performance impact of additional PyMem_NEW and memcpy is negligible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy