mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-06 17:20:22 +00:00
Fix readdir_r() usage for Solaris
On Solaris, struct dirent is defined differently than Linux. The field containing the path name is of size 0, rather than NAME_MAX. So, we need to use a properly sized buffer on Solaris to avoid a stack overflow. Also fix some DIR* leaks on cleanup.
This commit is contained in:
parent
b1ec25facc
commit
6fb1c0b489
33
src/path.c
33
src/path.c
@ -494,7 +494,7 @@ int git_path_direach(
|
|||||||
{
|
{
|
||||||
ssize_t wd_len;
|
ssize_t wd_len;
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent de_buf, *de;
|
struct dirent *de, *de_buf;
|
||||||
|
|
||||||
if (git_path_to_dir(path) < 0)
|
if (git_path_to_dir(path) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
@ -506,14 +506,23 @@ int git_path_direach(
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (p_readdir_r(dir, &de_buf, &de) == 0 && de != NULL) {
|
#ifdef __sun
|
||||||
|
de_buf = git__malloc(sizeof(struct dirent) + FILENAME_MAX + 1);
|
||||||
|
#else
|
||||||
|
de_buf = git__malloc(sizeof(struct dirent));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
while (p_readdir_r(dir, de_buf, &de) == 0 && de != NULL) {
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
if (is_dot_or_dotdot(de->d_name))
|
if (is_dot_or_dotdot(de->d_name))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (git_buf_puts(path, de->d_name) < 0)
|
if (git_buf_puts(path, de->d_name) < 0) {
|
||||||
|
closedir(dir);
|
||||||
|
git__free(de_buf);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
result = fn(arg, path);
|
result = fn(arg, path);
|
||||||
|
|
||||||
@ -521,11 +530,13 @@ int git_path_direach(
|
|||||||
|
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
git__free(de_buf);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
git__free(de_buf);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -537,7 +548,7 @@ int git_path_dirload(
|
|||||||
{
|
{
|
||||||
int error, need_slash;
|
int error, need_slash;
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent de_buf, *de;
|
struct dirent *de, *de_buf;
|
||||||
size_t path_len;
|
size_t path_len;
|
||||||
|
|
||||||
assert(path != NULL && contents != NULL);
|
assert(path != NULL && contents != NULL);
|
||||||
@ -549,11 +560,17 @@ int git_path_dirload(
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef __sun
|
||||||
|
de_buf = git__malloc(sizeof(struct dirent) + FILENAME_MAX + 1);
|
||||||
|
#else
|
||||||
|
de_buf = git__malloc(sizeof(struct dirent));
|
||||||
|
#endif
|
||||||
|
|
||||||
path += prefix_len;
|
path += prefix_len;
|
||||||
path_len -= prefix_len;
|
path_len -= prefix_len;
|
||||||
need_slash = (path_len > 0 && path[path_len-1] != '/') ? 1 : 0;
|
need_slash = (path_len > 0 && path[path_len-1] != '/') ? 1 : 0;
|
||||||
|
|
||||||
while ((error = p_readdir_r(dir, &de_buf, &de)) == 0 && de != NULL) {
|
while ((error = p_readdir_r(dir, de_buf, &de)) == 0 && de != NULL) {
|
||||||
char *entry_path;
|
char *entry_path;
|
||||||
size_t entry_len;
|
size_t entry_len;
|
||||||
|
|
||||||
@ -573,11 +590,15 @@ int git_path_dirload(
|
|||||||
memcpy(&entry_path[path_len + need_slash], de->d_name, entry_len);
|
memcpy(&entry_path[path_len + need_slash], de->d_name, entry_len);
|
||||||
entry_path[path_len + need_slash + entry_len] = '\0';
|
entry_path[path_len + need_slash + entry_len] = '\0';
|
||||||
|
|
||||||
if (git_vector_insert(contents, entry_path) < 0)
|
if (git_vector_insert(contents, entry_path) < 0) {
|
||||||
|
closedir(dir);
|
||||||
|
git__free(de_buf);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
git__free(de_buf);
|
||||||
|
|
||||||
if (error != 0)
|
if (error != 0)
|
||||||
giterr_set(GITERR_OS, "Failed to process directory entry in '%s'", path);
|
giterr_set(GITERR_OS, "Failed to process directory entry in '%s'", path);
|
||||||
|
@ -14,6 +14,8 @@
|
|||||||
# include "compat/fnmatch.h"
|
# include "compat/fnmatch.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define p_lstat(p,b) lstat(p,b)
|
#define p_lstat(p,b) lstat(p,b)
|
||||||
#define p_readlink(a, b, c) readlink(a, b, c)
|
#define p_readlink(a, b, c) readlink(a, b, c)
|
||||||
#define p_link(o,n) link(o, n)
|
#define p_link(o,n) link(o, n)
|
||||||
|
Loading…
Reference in New Issue
Block a user