enthought / enthought/Python-2.7.3
Suspicious memory leaks by reviewing the code
- 主要语言
- Python
- 星标
- 3
- 派生
- 12
- PR 合并指标
- 30 天内没有已合并 PR
描述
It seems that when coming into the exception branch, perhaps memory leak will happen.
1.
```
static BOOL __stdcall
win32_wchdir(LPCWSTR path)
{
wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
int result;
wchar_t env[4] = L"=x:";
if(!SetCurrentDirectoryW(path))
return FALSE;
result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
if (!result)
return FALSE;
if (result > MAX_PATH+1) {
new_path = malloc(result * sizeof(wchar_t));
if (!new_path) {
SetLastError(ERROR_OUTOFMEMORY);
return FALSE;
}
result = GetCurrentDirectoryW(result, new_path);
if (!result) {
free(new_path);
return FALSE;
}
}
if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
wcsncmp(new_path, L"//", 2) == 0)
/* UNC path, nothing to do. */
return TRUE; ################### if new_path calls malloc, it nerver calls free
env[1] = new_path[0];
result = SetEnvironmentVariableW(env, new_path);
if (new_path != _new_path)
free(new_path);
return result;
}
```
2.
```
static PyObject *
posix_fdopen(PyObject *self, PyObject *args)
{
int fd;
char *orgmode = "r";
int bufsize = -1;
FILE *fp;
PyObject *f;
char *mode;
if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
return NULL;
/* Sanitize mode. See fileobject.c */
mode = PyMem_MALLOC(strlen(orgmode)+3);
if (!mode) {
PyErr_NoMemory();
return NULL;
}
strcpy(mode, orgmode);
if (_PyFile_SanitizeMode(mode)) {
PyMem_FREE(mode);
return NULL;
}
if (!_PyVerify_fd(fd))
return posix_error(); ################### mode nerver calls PyMem_FREE
Py_BEGIN_ALLOW_THREADS
#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
if (mode[0] == 'a') {
/* try to make sure the O_APPEND flag is set */
int flags;
flags = fcntl(fd, F_GETFL);
if (flags != -1)
fcntl(fd, F_SETFL, flags | O_APPEND);
fp = fdopen(fd, mode);
if (fp == NULL && flags != -1)
/* restore old mode if fdopen failed */
fcntl(fd, F_SETFL, flags);
} else {
fp = fdopen(fd, mode);
}
#else
fp = fdopen(fd, mode);
#endif
Py_END_ALLOW_THREADS
PyMem_FREE(mode);
if (fp == NULL)
return posix_error();
/* The dummy filename used here must be kept in sync with the value
tested against in gzip.GzipFile.__init__() - see issue #13781. */
f = PyFile_FromFile(fp, "", orgmode, fclose);
if (f != NULL)
PyFile_SetBufSize(f, bufsize);
return f;
}
```
贡献指南
这个仓库没有索引到贡献指南
调研方向
首先阅读 issue 中展示的入口点 win32_wchdir 和 posix_fdopen,跟踪每次分配之后的所有提前返回。确认每个已分配的缓冲区都在所有路径上得到释放,然后使用 repository 中相关的 platform 和 File-I/O 测试,在不改变成功行为的情况下验证泄漏修复。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, python
- 领域
- operating-systems
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100