summaryrefslogtreecommitdiff
path: root/include/stdio.h
diff options
context:
space:
mode:
authorjvoisin2024-05-20 14:48:35 +0200
committerJulien Voisin2024-05-26 20:19:27 +0000
commit1becad43298e74ba73bc66f9d44523e5d121c667 (patch)
tree009d0b1431d11ceb9d3ae1d33d1fd35638365ee9 /include/stdio.h
parent92c611ad8abb146ed548301de8bc011c2b17bccd (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/stdio.h')
-rw-r--r--include/stdio.h42
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