[sr-dev] git:3.2: app_python: fixing clone string for dirname and basename
Daniel-Constantin Mierla
miconda at gmail.com
Tue Nov 1 23:40:22 CET 2011
Module: sip-router
Branch: 3.2
Commit: 395b4707025241f8de07a679c04c385a9bfc5937
URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=395b4707025241f8de07a679c04c385a9bfc5937
Author: Michal Karas <largon at largon.net>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: Mon Oct 17 11:18:51 2011 +0200
app_python: fixing clone string for dirname and basename
- variable tname replaced by dname_src and bname_src, because both
dirname() and basename() requires it's argument not to change: "These
functions may return pointers to statically allocated memory which may
be overwritten by subsequent calls. Alternatively, they may return a
pointer to some part of path, so that the string referred to by path
should not be modified or freed until the pointer returned by the
function is no longer required."
- related to FS#137
Signed-off-by: Daniel-Constantin Mierla <miconda at gmail.com>
(cherry picked from commit d05616d81960e6b1647981d8d8b5e0dbe04a3dcb)
---
modules/app_python/python_mod.c | 25 ++++++++++++++-----------
1 files changed, 14 insertions(+), 11 deletions(-)
diff --git a/modules/app_python/python_mod.c b/modules/app_python/python_mod.c
index c89c928..c178551 100644
--- a/modules/app_python/python_mod.c
+++ b/modules/app_python/python_mod.c
@@ -85,7 +85,7 @@ struct module_exports exports = {
static int
mod_init(void)
{
- char *dname, *bname, *tname;
+ char *dname, *bname, *dname_src, *bname_src;
int i;
PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
PyThreadState *mainThreadState;
@@ -100,19 +100,19 @@ mod_init(void)
child_init_mname.len = strlen(child_init_mname.s);
}
- tname = as_asciiz(&script_name);
- if(tname==NULL)
- {
- LM_ERR("no more pkg memory\n");
- return -1;
- }
- dname = dirname(tname);
+ dname_src = as_asciiz(&script_name);
+ bname_src = as_asciiz(&script_name);
+ if(dname_src==NULL || bname_src==NULL)
+ {
+ LM_ERR("no more pkg memory\n");
+ return -1;
+ }
+
+ dname = dirname(dname_src);
if (strlen(dname) == 0)
dname = ".";
- memcpy(tname, script_name.s, script_name.len);
- bname = basename(tname);
+ bname = basename(bname_src);
i = strlen(bname);
- pkg_free(tname);
if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
i -= 1;
if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
@@ -159,6 +159,9 @@ mod_init(void)
return -1;
}
+ pkg_free(dname_src);
+ pkg_free(bname_src);
+
pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
Py_DECREF(pModule);
/* pFunc is a new reference */
More information about the sr-dev
mailing list