diff options
| author | jvoisin | 2024-05-20 14:48:35 +0200 |
|---|---|---|
| committer | Julien Voisin | 2024-05-26 20:19:27 +0000 |
| commit | 1becad43298e74ba73bc66f9d44523e5d121c667 (patch) | |
| tree | 009d0b1431d11ceb9d3ae1d33d1fd35638365ee9 /include | |
| parent | 92c611ad8abb146ed548301de8bc011c2b17bccd (diff) | |
Add vasprintf/asprintf2.3
The only hardening being done here is to set the char** parameter to thos
functions to NULL in case of an error, to prevent it from being used should
people forget to check return values. This is already done on some BSD, as well
as in Rocky Linux.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stdio.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/include/stdio.h b/include/stdio.h index c6817fa..abdef6c 100644 --- a/include/stdio.h +++ b/include/stdio.h | |||
| @@ -220,6 +220,26 @@ _FORTIFY_FN(vprintf) int vprintf(const char *__f, __builtin_va_list __v) | |||
| 220 | return __orig_vprintf(__f, __v); | 220 | return __orig_vprintf(__f, __v); |
| 221 | #endif | 221 | #endif |
| 222 | } | 222 | } |
| 223 | |||
| 224 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) | ||
| 225 | #undef vasprintf | ||
| 226 | #if __has_builtin(__builtin_vasprintf) | ||
| 227 | __diagnose_as_builtin(__builtin_vasprintf, 1, 2, 3) | ||
| 228 | #endif | ||
| 229 | _FORTIFY_FN(vasprintf) int vasprintf(char **restrict strp, const char *restrict fmt, __builtin_va_list ap) | ||
| 230 | { | ||
| 231 | #if __has_builtin(__builtin___vasprintf_chk) && USE_NATIVE_CHK | ||
| 232 | return __builtin___vasprintf_chk(_FORTIFY_SOURCE, strp, fmt, ap); | ||
| 233 | #else | ||
| 234 | int ret = __orig_vasprintf(strp, fmt, ap); | ||
| 235 | if (ret < 0) | ||
| 236 | *strp = NULL; | ||
| 237 | return ret; | ||
| 238 | #endif | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | #endif // defined(_GNU_SOURCE) || defined(_BSD_SOURCE) | ||
| 223 | #endif // __clang__ | 243 | #endif // __clang__ |
| 224 | 244 | ||
| 225 | 245 | ||
| @@ -312,6 +332,28 @@ _FORTIFY_FN(fprintf) int fprintf(FILE *__s, const char *__f, ...) | |||
| 312 | #endif | 332 | #endif |
| 313 | } | 333 | } |
| 314 | 334 | ||
| 335 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) | ||
| 336 | #ifndef __clang__ | ||
| 337 | #undef asprintf | ||
| 338 | __fh_access(read_only, 2) | ||
| 339 | __fh_format(printf, 2, 0) | ||
| 340 | #if __has_builtin(__builtin_asprintf) | ||
| 341 | __diagnose_as_builtin(__builtin_asprintf, 2, 3) | ||
| 342 | #endif | ||
| 343 | _FORTIFY_FN(asprintf) int asprintf(char **restrict strp, const char *restrict fmt, ...) | ||
| 344 | { | ||
| 345 | #if __has_builtin(__builtin___asprintf_chk) && USE_NATIVE_CHK | ||
| 346 | return __builtin___asprintf_chk(_FORTIFY_SOURCE, strp, fmt, __builtin_va_arg_pack()); | ||
| 347 | #else | ||
| 348 | int ret = __orig_asprintf(strp, fmt, __builtin_va_arg_pack()); | ||
| 349 | if (ret<0) | ||
| 350 | *strp = NULL; | ||
| 351 | return ret; | ||
| 352 | #endif | ||
| 353 | } | ||
| 354 | #endif // __clang__ | ||
| 355 | #endif | ||
| 356 | |||
| 315 | #pragma GCC diagnostic pop | 357 | #pragma GCC diagnostic pop |
| 316 | #endif /* __has_builtin(__builtin_va_arg_pack) */ | 358 | #endif /* __has_builtin(__builtin_va_arg_pack) */ |
| 317 | 359 | ||
