Include of a platform-specific system header

ID

c.portability.restrict_system_includes

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Portability

Language

C / C++

Description

This includes a platform-specific system header, which ties the code to a single operating system and hurts portability. Guard the include behind a platform macro (e.g. #ifdef _WIN32) and provide a portable abstraction, or replace it with a cross-platform alternative.

Rationale

This includes a platform-specific system header, which ties the code to a single operating system and hurts portability. Guard the include behind a platform macro (e.g. #ifdef _WIN32) and provide a portable abstraction, or replace it with a cross-platform alternative.

The following code illustrates the pattern detected by this rule:

// ok: restrict-system-includes
#include <stdio.h>
// ok: restrict-system-includes
#include <string.h>
// FLAGGED: Include of a platform-specific system header
#include <unistd.h>
// FLAGGED: Include of a platform-specific system header
#include <windows.h>
// FLAGGED: Include of a platform-specific system header
#include <sys/socket.h>
// FLAGGED: Include of a platform-specific system header
#include <pthread.h>
// FLAGGED: Include of a platform-specific system header
#include <io.h>

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.