This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyImport_AppendInittab stores pointer to parameter
Type: Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, coder_5, georg.brandl, loewis
Priority: normal Keywords:

Created on 2006-01-31 03:19 by coder_5, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg27388 - (view) Author: coder_5 (coder_5) Date: 2006-01-31 03:19
signature is:

int PyImport_AppendInittab(char *name, void
(*initfunc)(void))

if the 'name' pointer is freed or overwritten directly
after the call to PyImport_AppendInittab, this call
returns true but fails making the module known, and the
interpreter crashes on PyFinalize();
this suggests that PyImport_AppendInittab stores the
name pointer and uses it later, after leaving this
function. this is undocumented and leads to crashes if
name is freed in the meantime. (a typical c problem)

this function is important to boost::python library to
extend python with c++.

workaround for c/c++ users:
-malloc a char* for the name,
-copy the modulename to name
-call PyImport_AppendInittab with this name
-DONT free name. (leaving a memory-leak)


btw, 'char *' should be 'const char*', but this applies
to most other Python API functions.
msg27389 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-02-17 18:47
Logged In: YES 
user_id=1188172

Should AppendInittab() itself malloc a char buffer?
msg27390 - (view) Author: coder_5 (coder_5) Date: 2006-02-19 17:42
Logged In: YES 
user_id=1440178

I see 3 sollutions.

1) (bad)
leave as is but document it. (user may free pointer to name
AFTER PyFinalize() only)

2) (ok)
let AppendInittab() malloc a buffer to store the name for
later use there. (dont know about pythons mem alloc policy
though)

3) (good)
make it: int PyImport_AppendInittab(const char *name, void
(*initfunc)(void)) AND realize 2) (malloc buffer).
i would prefer to have the interface cleaned in a way that
pointers to const* are declared as const. using const is
good codingstyle and might even help the compiler on
optimization.
msg85153 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2009-04-02 03:42
I made the char * a const char *. Committed in r71031 & r71033 for 2.x,
r71034 for 3.x.
History
Date User Action Args
2022-04-11 14:56:15adminsetgithub: 42850
2009-04-02 03:42:10brett.cannonsetstatus: open -> closed
resolution: fixed
messages: + msg85153
2009-02-11 02:59:14ajaksu2setassignee: loewis -> brett.cannon
versions: + Python 2.7, - Python 2.4
nosy: + brett.cannon
2006-01-31 03:19:28coder_5create