Issue 26708: Constify C string pointers in the posix module (original) (raw)

Proposed patch adds the "const" qualifier to char and wchar_t pointers in the posix module to prevents possible bugs. These pointers point to internal data of PyBytes or PyUnicode objects or to C string literals, and unintentional changing the content is a hard bug. I expect that the patch can also eliminate some compiler warnings.

Since large part of the code is Windows specific, the patch needs to be tested on Windows.

I get the following warnings:

..\Modules\posixmodule.c(7422): warning C4090: 'function': different 'const' qualifiers [...] ..\Modules\posixmodule.c(7423): warning C4090: 'function': different 'const' qualifiers [...]

    target_is_directory |= _check_dirW(src->wide, dst->wide);
    result = Py_CreateSymbolicLinkW(dst->wide, src->wide,
                                    target_is_directory);

..\Modules\posixmodule.c(7429): warning C4090: 'function': different 'const' qualifiers [...]

    result = Py_CreateSymbolicLinkA(dst->narrow, src->narrow,
                                    target_is_directory);

You can change _check_dirW to use LPCWSTR parameters, or const wchar_t * to be consistent with _check_dirA. In this context I prefer the Windows typedefs:

_check_dirW(LPCWSTR src, LPCWSTR dest)
_check_dirA(LPCSTR src, LPCSTR dest)

Change Py_CreateSymbolicLink[W|A] to use LPC[W]STR, which is how it's declared in Winbase.h:

static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL;
static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPCSTR, LPCSTR, DWORD) = NULL;