DynamoRIO / DynamoRIO/dynamorio
Avoid using PATH_MAX
- Dominant language
- C
- Stars
- 3.2k
- Forks
- 629
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 31
Description
PATH_MAX is used in a couple of places in `dr_frontend_unix.c`, but it is best avoided. Not all systems define it, and even if it is defined it could have an unhelpful value such as INT_MAX.
Modern implementations of realpath() allow the second argument to be NULL. If it is necessary to cope with ancient implementations a wrapper like this could be used:
```
/* Handle the (unlikely) case of us having an ancient realpath() that does
* not accept a second argument that is NULL.
*/
static char *
realpath_wrapper(const char *path)
{
char *result = realpath(path, NULL);
#if defined(PATH_MAX) && PATH_MAX > 0
if (result == NULL && path != NULL && errno == EINVAL) {
/* Try again with a fixed-length buffer. */
char *buf = malloc(PATH_MAX);
if (buf != NULL) {
result = realpath(path, buf);
if (result != NULL) {
/* Copy result into malloc-ed memory, if possible. */
char *tmp = malloc(strlen(result) + 1);
if (tmp != NULL)
strcpy(tmp, result);
result = tmp;
}
free(buf);
}
}
#endif
return result;
}
```
Contributor guide
Assessment
This issue has not been assessed yet.