| name | platform-portability-checker |
| description | Verify C/C++ code is platform-independent and portable across embedded platforms. Use when reviewing code for cross-platform deployment or preparing for new hardware targets. |
Platform Portability Checker
Purpose
Ensure C/C++ code is portable across different embedded platforms, architectures, and operating systems without modification.
When to Use
- Reviewing new code before merge
- Porting to new hardware platform
- Preparing release for multiple architectures
- Investigating platform-specific bugs
- Refactoring legacy platform-specific code
Portability Checklist
1. Integer Types
Check for: Use of int, long, short without fixed sizes
int counter;
long timestamp;
short flag;
#include <stdint.h>
uint32_t counter;
uint64_t timestamp;
uint16_t flag;
size_t length;
ssize_t result;
2. Pointer Assumptions
Check for: Pointer arithmetic, casting, size assumptions
long ptr_value = (long)ptr;
#include <stdint.h>
uintptr_t ptr_value = (uintptr_t)ptr;
if (ptr & 0x1) { ... }
if ((uintptr_t)ptr & 0x1) { ... }
3. Endianness
Check for: Multi-byte values sent over network or stored to disk
uint32_t value = 0x12345678;
fwrite(&value, 4, 1, file);
#include <arpa/inet.h>
uint32_t host_value = 0x12345678;
uint32_t network_value = htonl(host_value);
fwrite(&network_value, 4, 1, file);
uint32_t network_value;
fread(&network_value, 4, 1, file);
uint32_t host_value = ntohl(network_value);
4. Structure Packing
Check for: Structures sent over network or saved to disk
struct {
uint8_t type;
uint32_t value;
uint16_t flags;
} data;
struct __attribute__((packed)) {
uint8_t type;
uint32_t value;
uint16_t flags;
} data;
struct {
uint8_t type;
uint8_t padding[3];
uint32_t value;
uint16_t flags;
uint16_t padding2;
} data;
5. Boolean Type
Check for: Using int/char for boolean
int flag;
char enabled;
#include <stdbool.h>
bool flag;
bool enabled;
if (flag) { ... }
6. Character Sets
Check for: Assumptions about ASCII or character encoding
if (ch >= 'A' && ch <= 'Z') {
ch = ch + 32;
}
#include <ctype.h>
if (isupper(ch)) {
ch = tolower(ch);
}
7. File Paths
Check for: Hard-coded path separators
const char* path = "/tmp/telemetry/data.log";
#ifdef _WIN32
#define PATH_SEP "\\"
const char* tmp_dir = getenv("TEMP");
#else
#define PATH_SEP "/"
const char* tmp_dir = "/tmp";
#endif
char path[256];
snprintf(path, sizeof(path), "%s%stelemetry%sdata.log",
tmp_dir, PATH_SEP, PATH_SEP);
8. System Calls
Check for: Platform-specific syscalls
#include <sys/epoll.h>
int fd = epoll_create(10);
#if defined(__linux__)
#include "platform_linux.h"
#elif defined(__APPLE__)
#include "platform_darwin.h"
#else
#error "Unsupported platform"
#endif
event_loop_t* create_event_loop(void);
9. Compiler Extensions
Check for: GCC/Clang specific features
typeof(x) y = x;
int array[0];
__auto_type y = x;
10. Include Paths
Check for: Platform-specific headers
#include <linux/limits.h>
#ifdef HAVE_LINUX_LIMITS_H
#include <linux/limits.h>
#else
#include <limits.h>
#endif
Build System Integration
configure.ac checks
# Check for required features
AC_C_BIGENDIAN
AC_CHECK_SIZEOF([int])
AC_CHECK_SIZEOF([long])
AC_CHECK_SIZEOF([void *])
# Check for headers
AC_CHECK_HEADERS([stdint.h stdbool.h endian.h])
# Check for functions
AC_CHECK_FUNCS([htonl ntohl])
# Platform-specific code
case "$host" in
*-linux*)
AC_DEFINE([PLATFORM_LINUX], [1])
;;
arm*|*-arm*)
AC_DEFINE([PLATFORM_ARM], [1])
;;
esac
Testing
Cross-Compilation Test
./configure --host=arm-linux-gnueabihf
make clean && make
./configure --host=x86_64-linux-gnu
make clean && make
./configure --host=mips-linux-gnu
make clean && make
Endianness Test
uint32_t value = 0x12345678;
uint32_t network = htonl(value);
uint32_t restored = ntohl(network);
assert(value == restored);
assert(sizeof(packed_struct_t) == EXPECTED_SIZE);
Output Format
## Platform Portability Analysis
### Critical Issues
1. [file.c:123] Using `long` for timestamp - not fixed width
2. [file.c:456] Writing struct directly to network - endianness issue
3. [file.c:789] Assuming 32-bit pointers
### Warnings
1. [file.c:234] Using int for boolean - prefer stdbool.h
2. [file.c:567] Hard-coded Unix path separator
### Recommendations
1. Add configure checks for required headers
2. Create platform abstraction layer
3. Test build on multiple architectures
### Suggested Fixes
[Specific code changes for each issue]
Verification
- Code compiles on target platforms
- Tests pass on all platforms
- Static analysis clean
- No endianness issues
- No alignment issues
- Structure sizes verified