Clang 16 will be available with Red Hat Enterprise Linux (RHEL) 8.9 and 9.3. Like Clang 15, it comes with some new warnings and errors enabled by default that more strictly enforce language standards and help prevent bugs.
Wimplicit-function-declaration
Starting with Clang 16, implicit function definitions will be considered an error instead of a warning. The C99 standard dropped support for implicit function definitions, but many compilers continued to accept them for backward compatibility.
Implicit function definitions are usually caused by a programmer forgetting to include a necessary header in a C file, or forgetting to add a function prototype when implementing a new function. They can be dangerous and lead to subtle bugs or even security issues, because without a prototype, the compiler will accept any number of arguments of any type passed to the function. Passing too many arguments or arguments that are too large can override the stack and cause the program to crash or give an attacker the opportunity to insert malicious code.
$ cat implicit-function.c int main(int argc, char **argv) { implicit_function(); } $ clang-15 -c implicit-function.c test.c:2:3: warning: call to undeclared function 'implicit_function'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] $ clang-16 -c implicit-function.c test.c:2:3: error: call to undeclared function 'implicit_function'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
Wimplicit-int
This is another warning that was upgraded to an error in Clang 16. Prior to C99, functions without a return type were assumed to return int. C99 dropped support for this feature, but just like with implicit function definitions, compilers continued to accept this as valid C code until now.
$ cat implicit-int.c main (int argc, char **argv) { return 0; } $ clang-15 -c implicit-int.c implicit-int.c:1:1: warning: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int] $ clang-16 -c implicit-int.c implicit-int.c:1:1: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
Wincompatible-function-pointer-types
This warning was also upgraded to an error by default in Clang 16. It is able to detect when you use the wrong function type when initializing a function variable or passing a function as an argument to another function.
$ cat ifpt.c int callback(int a, int b) { return a + b; } int do_something(int (*callback)(int)) { return callback(5); } int main (int argc, char **argv) { do_something(callback); } $ clang-15 -c ifpt.c ifpt.c:10:18: warning: incompatible function pointer types passing 'int (int, int)' to parameter of type 'int (*)(int)' [-Wincompatible-function-pointer-types] $ clang-16 -c ifpt.c ifpt.c:10:18: error: incompatible function pointer types passing 'int (int, int)' to parameter of type 'int (*)(int)' [-Wincompatible-function-pointer-types]
Wcast-function-type-strict and Wincompatible-function-pointer-types-strict
These warnings were added to Clang 16 and are intended to help avoid false positives when using Control Flow Integrity (CFI). CFI is a security feature that prevents certain types of exploits by adding some extra checks around function calls. For more details about how to use CFI, please refer to this blog post.
These warnings are "strict" versions of existing warnings that handle a few extra cases that are relevant to CFI. For example, -Wincompatible-function-pointer-types-strict
, will warn about enum and int mismatches even in cases where the enum is the same size as an int.
These warnings are not enabled by default, so if you want to use them, you need to explicitly pass these options to the compiler. It is also a good idea to promote them to errors using the -Werror= flag
to ensure that you don't introduce false positives into your program.
$ cat cfi.c enum cfi_enum { A = -1, B = 0 }; int callback(int a) { return a; } int do_something(int (*callback)(enum cfi_enum)) { return callback(5); } int main (int argc, char **argv) { do_something(callback); } $ clang -fsanitize=cfi -flto -fvisibility=hidden cfi.c -o cfi $ ./cfi Illegal instruction (core dumped)
Our application failed because we passed the wrong function type to do_something(). Notice that -Wincompatible-function-pointer-types
did not catch this case and we need to use the strict variant if we want to be compatible with CFI.
$ clang -Werror=incompatible-function-pointer-types-strict -fsanitize=cfi -flto -fvisibility=hidden cfi.c -o cfi cfi.c:15:18: error: incompatible function pointer types passing 'int (int)' to parameter of type 'int (*)(enum cfi_enum)' [-Werror,-Wincompatible-function-pointer-types-strict]
Disabling warnings
While these changes to warnings and errors are designed to help improve your application, you may encounter situations where it’s not possible or you don't have time to fix some of these new errors. In these cases, you can work around the problem by disabling or downgrading the error to a warning.
To downgrade an error to a warning, use the -Wno-error=
option like this:
$ clang -Wno-error=implicit-function-declaration -c implicit-function.c implicit-function.c:1:37: warning: call to undeclared function 'implicit_function'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] int main(int argc, char **argv) { implicit_function(); }
To disable an error or warning completely, use the -Wno option:
$ clang -Wno-implicit-function-declaration -c implicit-function.c
Try it out
You don't need to wait until the next RHEL release to try out these new compiler features. Clang-16 builds are available today in the CentOS Stream repositories. If you are a C developer, give Clang-16 a try with your applications. These new warnings and errors can help you catch bugs and ensure that your applications comply with the modern C standards.
저자 소개
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
오리지널 쇼
엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리
제품
- Red Hat Enterprise Linux
- Red Hat OpenShift Enterprise
- Red Hat Ansible Automation Platform
- 클라우드 서비스
- 모든 제품 보기
툴
체험, 구매 & 영업
커뮤니케이션
Red Hat 소개
Red Hat은 Linux, 클라우드, 컨테이너, 쿠버네티스 등을 포함한 글로벌 엔터프라이즈 오픈소스 솔루션 공급업체입니다. Red Hat은 코어 데이터센터에서 네트워크 엣지에 이르기까지 다양한 플랫폼과 환경에서 기업의 업무 편의성을 높여 주는 강화된 기능의 솔루션을 제공합니다.