From f46714c2f9eb13c12c8218f1b7c045182041fdc9 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 26 Oct 2022 00:30:00 +0200 Subject: add initial clang support Co-Authored-By: jvoisin --- include/stdio.h | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) (limited to 'include/stdio.h') diff --git a/include/stdio.h b/include/stdio.h index e060f64..a75c9ea 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2015-2016 Dimitris Papastamos + * Copyright (C) 2022 q66 * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted. @@ -36,7 +37,7 @@ extern "C" { #undef snprintf #undef sprintf -_FORTIFY_FN(fgets) char *fgets(char *__s, int __n, FILE *__f) +_FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f) { size_t __b = __bos(__s, 0); @@ -45,7 +46,8 @@ _FORTIFY_FN(fgets) char *fgets(char *__s, int __n, FILE *__f) return __orig_fgets(__s, __n, __f); } -_FORTIFY_FN(fread) size_t fread(void *__d, size_t __n, size_t __m, FILE *__f) +_FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n, + size_t __m, FILE *__f) { size_t __b = __bos(__d, 0); @@ -56,7 +58,8 @@ _FORTIFY_FN(fread) size_t fread(void *__d, size_t __n, size_t __m, FILE *__f) return __orig_fread(__d, __n, __m, __f); } -_FORTIFY_FN(fwrite) size_t fwrite(const void *__d, size_t __n, size_t __m, FILE *__f) +_FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n, + size_t __m, FILE *__f) { size_t __b = __bos(__d, 0); @@ -67,8 +70,8 @@ _FORTIFY_FN(fwrite) size_t fwrite(const void *__d, size_t __n, size_t __m, FILE return __orig_fwrite(__d, __n, __m, __f); } -_FORTIFY_FN(vsnprintf) int vsnprintf(char *__s, size_t __n, const char *__f, - __builtin_va_list __v) +_FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n, + const char *__f, __builtin_va_list __v) { size_t __b = __bos(__s, 0); @@ -77,7 +80,8 @@ _FORTIFY_FN(vsnprintf) int vsnprintf(char *__s, size_t __n, const char *__f, return __orig_vsnprintf(__s, __n, __f, __v); } -_FORTIFY_FN(vsprintf) int vsprintf(char *__s, const char *__f, __builtin_va_list __v) +_FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f, + __builtin_va_list __v) { size_t __b = __bos(__s, 0); int __r; @@ -92,7 +96,23 @@ _FORTIFY_FN(vsprintf) int vsprintf(char *__s, const char *__f, __builtin_va_list return __r; } -_FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, const char *__f, ...) +#if defined(__has_builtin) +#if __has_builtin(__builtin_va_arg_pack) + +/* clang is missing __builtin_va_arg_pack, so we cannot use these impls + * outside of gcc; we then have a few options: + * + * 1) using va_start/end and implementing these functions as static inline, + * with inlining never happening; that means extra symbols with internal + * linkage, which is not ideal + * 2) using macros; this is incompatible with c++ and since musl does not + * have the __chk variants, we'd need to implement a body with intermediate + * variables within the macro, which means more non-portable mess + * 3) not implementing these under clang, which is what we do for now + */ + +_FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, + const char *__f, ...) { size_t __b = __bos(__s, 0); @@ -116,6 +136,9 @@ _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...) return __r; } +#endif /* __has_builtin(__builtin_va_arg_pack) */ +#endif /* defined(__has_builtin) */ + #ifdef __cplusplus } #endif -- cgit v1.3