diff options
| author | info@mobile-stream.com | 2019-03-06 16:28:48 +0300 |
|---|---|---|
| committer | sin | 2019-03-07 00:05:30 +0000 |
| commit | ff82ffbc74d82091527449e31fe351d15830f716 (patch) | |
| tree | 0ccbcea941cf0febdc42fd043a0ced50082b2eae /include/stdlib.h | |
| parent | 1435d8186b1954de640ec79717c5e564243bd350 (diff) | |
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.
Diffstat (limited to '')
| -rw-r--r-- | include/stdlib.h | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/include/stdlib.h b/include/stdlib.h index ef70995..11155cf 100644 --- a/include/stdlib.h +++ b/include/stdlib.h | |||
| @@ -39,12 +39,10 @@ extern "C" { | |||
| 39 | #undef realpath | 39 | #undef realpath |
| 40 | _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) | 40 | _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) |
| 41 | { | 41 | { |
| 42 | size_t __b = __builtin_object_size(__r, 0); | ||
| 43 | |||
| 44 | if (__r) { | ||
| 45 | #ifndef PATH_MAX | 42 | #ifndef PATH_MAX |
| 46 | #error PATH_MAX unset. A fortified realpath will not work. | 43 | #error PATH_MAX unset. A fortified realpath will not work. |
| 47 | #else | 44 | #else |
| 45 | if (__r && PATH_MAX > __builtin_object_size(__r, 2)) { | ||
| 48 | char __buf[PATH_MAX], *__ret; | 46 | char __buf[PATH_MAX], *__ret; |
| 49 | size_t __l; | 47 | size_t __l; |
| 50 | 48 | ||
| @@ -52,13 +50,13 @@ _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) | |||
| 52 | if (!__ret) | 50 | if (!__ret) |
| 53 | return NULL; | 51 | return NULL; |
| 54 | __l = __builtin_strlen(__ret) + 1; | 52 | __l = __builtin_strlen(__ret) + 1; |
| 55 | if (__l > __b) | 53 | if (__l > __builtin_object_size(__r, 0)) |
| 56 | __builtin_trap(); | 54 | __builtin_trap(); |
| 57 | __builtin_memcpy(__r, __ret, __l); | 55 | __builtin_memcpy(__r, __ret, __l); |
| 58 | return __r; | 56 | return __r; |
| 59 | #endif | ||
| 60 | } | 57 | } |
| 61 | return __orig_realpath(__p, __r); | 58 | return __orig_realpath(__p, __r); |
| 59 | #endif | ||
| 62 | } | 60 | } |
| 63 | #endif | 61 | #endif |
| 64 | 62 | ||
