retry_realpath: check before possibly writing out of bounds

This commit is contained in:
Sigrid Solveig Haflínudóttir 2023-07-10 17:21:36 +00:00
parent 784467564c
commit dbcb8ed050
1 changed files with 6 additions and 1 deletions

View File

@ -126,10 +126,11 @@ static char *
retry_realpath(const char *file_name) retry_realpath(const char *file_name)
{ {
char *r, p[PATH_MAX] = {'\0'}, *x; char *r, p[PATH_MAX] = {'\0'}, *x;
int fnlen;
if(file_name == NULL) { if(file_name == NULL) {
errno = EINVAL; errno = EINVAL;
return NULL; return NULL;
} else if(strlen(file_name) >= PATH_MAX) { } else if((fnlen = strlen(file_name)) >= PATH_MAX) {
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
return NULL; return NULL;
} }
@ -137,6 +138,10 @@ retry_realpath(const char *file_name)
/* TODO: use a macro instead of '/' for absolute path first character so that other systems can work */ /* TODO: use a macro instead of '/' for absolute path first character so that other systems can work */
/* if a relative path, prepend cwd */ /* if a relative path, prepend cwd */
getcwd(p, sizeof(p)); getcwd(p, sizeof(p));
if(strlen(p) + strlen(DIR_SEP_STR) + fnlen >= PATH_MAX) {
errno = ENAMETOOLONG;
return NULL;
}
strcat(p, DIR_SEP_STR); /* TODO: use a macro instead of '/' for the path delimiter */ strcat(p, DIR_SEP_STR); /* TODO: use a macro instead of '/' for the path delimiter */
} }
strcat(p, file_name); strcat(p, file_name);