mirror of
https://github.com/jarun/nnn.git
synced 2025-01-15 21:36:42 +00:00
Fix memory leak
This commit is contained in:
parent
1a04e813be
commit
cf46da60d9
19
src/nnn.c
19
src/nnn.c
|
@ -1167,15 +1167,21 @@ static char *common_prefix(const char *path, char *prefix)
|
||||||
* If there's a symlink in file list we want to show the symlink not what it's points to.
|
* If there's a symlink in file list we want to show the symlink not what it's points to.
|
||||||
* Resolves ./../~ in path
|
* Resolves ./../~ in path
|
||||||
*/
|
*/
|
||||||
static char *abspath(const char *path, const char *cwd, char *buf)
|
static char *abspath(const char *path, char *cwd, char *buf)
|
||||||
{
|
{
|
||||||
|
bool allocated = FALSE;
|
||||||
|
|
||||||
if (!path)
|
if (!path)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (path[0] == '~')
|
if (path[0] == '~')
|
||||||
cwd = home;
|
cwd = home;
|
||||||
else if ((path[0] != '/') && !cwd)
|
else if ((path[0] != '/') && !cwd) {
|
||||||
cwd = getcwd(NULL, 0);
|
cwd = getcwd(NULL, 0);
|
||||||
|
if (!cwd)
|
||||||
|
return NULL;
|
||||||
|
allocated = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
size_t dst_size = 0, src_size = xstrlen(path), cwd_size = cwd ? xstrlen(cwd) : 0;
|
size_t dst_size = 0, src_size = xstrlen(path), cwd_size = cwd ? xstrlen(cwd) : 0;
|
||||||
size_t len = src_size;
|
size_t len = src_size;
|
||||||
|
@ -1188,8 +1194,11 @@ static char *abspath(const char *path, const char *cwd, char *buf)
|
||||||
*/
|
*/
|
||||||
char *resolved_path = buf ? buf : malloc(src_size + cwd_size + 2);
|
char *resolved_path = buf ? buf : malloc(src_size + cwd_size + 2);
|
||||||
|
|
||||||
if (!resolved_path)
|
if (!resolved_path) {
|
||||||
|
if (allocated)
|
||||||
|
free(cwd);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* Turn relative paths into absolute */
|
/* Turn relative paths into absolute */
|
||||||
if (path[0] != '/') {
|
if (path[0] != '/') {
|
||||||
|
@ -1199,6 +1208,8 @@ static char *abspath(const char *path, const char *cwd, char *buf)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
dst_size = xstrsncpy(resolved_path, cwd, cwd_size + 1) - 1;
|
dst_size = xstrsncpy(resolved_path, cwd, cwd_size + 1) - 1;
|
||||||
|
if (allocated)
|
||||||
|
free(cwd);
|
||||||
} else
|
} else
|
||||||
resolved_path[0] = '\0';
|
resolved_path[0] = '\0';
|
||||||
|
|
||||||
|
@ -7548,7 +7559,7 @@ nochange:
|
||||||
tmp = xreadline(tmp, messages[MSG_NEW_PATH]);
|
tmp = xreadline(tmp, messages[MSG_NEW_PATH]);
|
||||||
else if (r == 's' || r == 'h')
|
else if (r == 's' || r == 'h')
|
||||||
tmp = xreadline(nselected == 1 ? xbasename(pselbuf) : NULL,
|
tmp = xreadline(nselected == 1 ? xbasename(pselbuf) : NULL,
|
||||||
messages[nselected <= 1?MSG_NEW_PATH:MSG_LINK_PREFIX]);
|
messages[nselected <= 1 ? MSG_NEW_PATH : MSG_LINK_PREFIX]);
|
||||||
else
|
else
|
||||||
tmp = NULL;
|
tmp = NULL;
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue