diff options
| author | Natanael Copa | 2015-05-07 14:50:03 +0200 |
|---|---|---|
| committer | sin | 2015-05-07 15:02:11 +0100 |
| commit | c2bb9e106a32619726cfc5052a1509e96b90de9f (patch) | |
| tree | d6dfbd159f911fcfe2cd9f0f41f6d40c107fb792 | |
| parent | c7e82d4863992c2f3bbb6f5a31fa8e5fd0e1643f (diff) | |
fix realpath when stdlib.h is included before limits.h
If program includes stdlib.h before limits.h without _XOPEN_SOURCE,
_GNU_SOURCE or _BSD_SOURCE explicitly set, then will it always trigger
the trap with musl libc.
This is becase stdlib.h will pull in features.h which will set
_GNU_SOURCE. This means that the fortify stdlib.h will not include
limits.h but it will still trigger the fortified realpath(), but without
PATH_MAX set.
We fix this by including system stdlib.h before testing if limits.h
should be included.
Since PATH_MAX is known at compile time we can also error at compile
time, instead of compiling a broken realpath().
| -rw-r--r-- | include/stdlib.h | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/include/stdlib.h b/include/stdlib.h index 22f1f2b..6629652 100644 --- a/include/stdlib.h +++ b/include/stdlib.h | |||
| @@ -1,12 +1,12 @@ | |||
| 1 | #ifndef _FORTIFY_STDLIB_H | 1 | #ifndef _FORTIFY_STDLIB_H |
| 2 | #define _FORTIFY_STDLIB_H | 2 | #define _FORTIFY_STDLIB_H |
| 3 | 3 | ||
| 4 | #include_next <stdlib.h> | ||
| 5 | |||
| 4 | #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) | 6 | #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) |
| 5 | #include_next <limits.h> | 7 | #include_next <limits.h> |
| 6 | #endif | 8 | #endif |
| 7 | 9 | ||
| 8 | #include_next <stdlib.h> | ||
| 9 | |||
| 10 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 | 10 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 |
| 11 | 11 | ||
| 12 | #ifdef __cplusplus | 12 | #ifdef __cplusplus |
| @@ -23,7 +23,7 @@ char *realpath(const char *path, char *resolved) | |||
| 23 | 23 | ||
| 24 | if (resolved) { | 24 | if (resolved) { |
| 25 | #ifndef PATH_MAX | 25 | #ifndef PATH_MAX |
| 26 | __builtin_trap(); | 26 | # error PATH_MAX unset. A fortified realpath will not work. |
| 27 | #else | 27 | #else |
| 28 | bos = __builtin_object_size(resolved, 0); | 28 | bos = __builtin_object_size(resolved, 0); |
| 29 | if (PATH_MAX > bos) | 29 | if (PATH_MAX > bos) |
