realpath: guard slow/trap path with PATH_MAX

This allows the compiler to optimize out the slow/trap path at all
for the typical correct code:

char buf[PATH_MAX];
r = realpath(path, buf);

The change keeps the "unknown object size" case handling intact.
This commit is contained in:
info@mobile-stream.com 2019-03-06 16:28:48 +03:00 committed by sin
parent 1435d8186b
commit ff82ffbc74

View File

@ -39,12 +39,10 @@ extern "C" {
#undef realpath
_FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r)
{
size_t __b = __builtin_object_size(__r, 0);
if (__r) {
#ifndef PATH_MAX
#error PATH_MAX unset. A fortified realpath will not work.
#else
if (__r && PATH_MAX > __builtin_object_size(__r, 2)) {
char __buf[PATH_MAX], *__ret;
size_t __l;
@ -52,13 +50,13 @@ _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r)
if (!__ret)
return NULL;
__l = __builtin_strlen(__ret) + 1;
if (__l > __b)
if (__l > __builtin_object_size(__r, 0))
__builtin_trap();
__builtin_memcpy(__r, __ret, __l);
return __r;
#endif
}
return __orig_realpath(__p, __r);
#endif
}
#endif