Use of the offsetof macro

ID

c.maintainability.avoid_offsetof

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Type Safety

Language

C / C++

Description

Reports a call to the offsetof macro from <stddef.h>/<cstddef>.

Rationale

offsetof is only guaranteed to give a meaningful result for a standard-layout type; on a type with virtual functions, virtual bases, multiple access specifiers, or non-trivial members, the call is conditionally-supported and its behavior is implementation-defined or undefined by both the C and C++ standards. It also silently breaks if the member it names is ever moved behind an access-specifier boundary or into a base class, with no compiler warning at the call site.

The following code illustrates the pattern detected by this rule:

struct Plain {
  int a;
  int b;
};

void f1(void) {
  // FLAGGED: Use of the offsetof macro
  size_t off = offsetof(struct Plain, b);
}

Remediation

Avoid computing member offsets by hand. Prefer accessing the member directly, or if the byte layout genuinely must be queried (e.g. for serialization), restrict the type to a verified standard-layout struct and document that constraint next to the definition.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved