Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistent implementations of realpath in klee-uclibc #24

Open
wants to merge 1 commit into
base: klee_0_9_29
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions libc/stdlib/realpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ char got_path[];
/* use user supplied buffer directly - reduces stack usage */
/* char got_path[PATH_MAX]; */
char *max_path;
char *new_path;
char *new_path, *allocated_path;
size_t path_len;
int readlinks = 0;
#ifdef S_IFLNK
Expand All @@ -78,12 +78,13 @@ char got_path[];
/* Copy so that path is at the end of copy_path[] */
strcpy(copy_path + (PATH_MAX-1) - path_len, path);
path = copy_path + (PATH_MAX-1) - path_len;
allocated_path = got_path ? NULL : (got_path = malloc(PATH_MAX));
max_path = got_path + PATH_MAX - 2; /* points to last non-NUL char */
new_path = got_path;
if (*path != '/') {
/* If it's a relative pathname use getcwd for starters. */
if (!getcwd(new_path, PATH_MAX - 1))
return NULL;
goto err;
new_path += strlen(new_path);
if (new_path[-1] != '/')
*new_path++ = '/';
Expand Down Expand Up @@ -120,6 +121,8 @@ char got_path[];
while (*path != '\0' && *path != '/') {
if (new_path > max_path) {
__set_errno(ENAMETOOLONG);
err:
free(allocated_path);
return NULL;
}
*new_path++ = *path++;
Expand All @@ -128,22 +131,23 @@ char got_path[];
/* Protect against infinite loops. */
if (readlinks++ > MAX_READLINKS) {
__set_errno(ELOOP);
return NULL;
goto err;
}
path_len = strlen(path);
/* See if last (so far) pathname component is a symlink. */
*new_path = '\0';
int sv_errno = errno;
link_len = readlink(got_path, copy_path, PATH_MAX - 1);
if (link_len < 0) {
/* EINVAL means the file exists but isn't a symlink. */
if (errno != EINVAL) {
return NULL;
goto err;
}
} else {
/* Safe sex check. */
if (path_len + link_len >= PATH_MAX - 2) {
__set_errno(ENAMETOOLONG);
return NULL;
goto err;
}
/* Note: readlink doesn't add the null byte. */
/* copy_path[link_len] = '\0'; - we don't need it too */
Expand All @@ -157,6 +161,7 @@ char got_path[];
memmove(copy_path + (PATH_MAX-1) - link_len - path_len, copy_path, link_len);
path = copy_path + (PATH_MAX-1) - link_len - path_len;
}
__set_errno(sv_errno);
#endif /* S_IFLNK */
*new_path++ = '/';
}
Expand Down
11 changes: 11 additions & 0 deletions libc/string/strdup.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ libc_hidden_proto(strlen)
#endif
libc_hidden_proto(memcpy)

char *__strdup(const char* s1) {
register char *s;
register size_t l = (strlen(s1) + 1) * sizeof(char);

if ((s = malloc(l)) != NULL) {
memcpy(s, s1, l);
}

return s;
}

Wchar *Wstrdup(register const Wchar *s1)
{
register Wchar *s;
Expand Down