Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ jobs:
# ---------------------------------------------------------------------------
# Build the library and run the full test suite under ASan + UBSan.
# Runs on both Linux and macOS to catch platform-specific issues.
# float-cast-overflow is added explicitly: GCC does not include it in
# -fsanitize=undefined (clang does), and it is the check that catches
# undefined float-to-integer conversions (e.g. NaN) in the scaled paths.
# ---------------------------------------------------------------------------
test:
name: "Test (${{ matrix.os }})"
Expand Down Expand Up @@ -44,6 +47,8 @@ jobs:
--buildtype=debug
-Dbuild_tests=true
-Db_sanitize=address,undefined
-Dc_args=-fsanitize=float-cast-overflow
-Dc_link_args=-fsanitize=float-cast-overflow

- name: Build
run: meson compile -C build
Expand Down Expand Up @@ -88,7 +93,8 @@ jobs:
--buildtype=debug
-Dbuild_tests=true
-Db_sanitize=address,undefined
-Dc_args=-DPPACK_PAYLOAD_BITS=512
"-Dc_args=-DPPACK_PAYLOAD_BITS=512 -fsanitize=float-cast-overflow"
-Dc_link_args=-fsanitize=float-cast-overflow

- name: Build
run: meson compile -C build
Expand Down Expand Up @@ -137,7 +143,8 @@ jobs:
# ---------------------------------------------------------------------------
# Code coverage gate. Builds with gcov instrumentation, runs the full
# test suite, and fails the job if line or branch coverage drops below
# the threshold. Threshold is currently 80% line / 70% branch as a pragmatic baseline.
# the threshold. The suite achieves 100% line and branch coverage; the
# gate is set there to keep it that way.
# ---------------------------------------------------------------------------
coverage:
name: "Coverage gate"
Expand Down Expand Up @@ -180,8 +187,8 @@ jobs:
--html-details coverage-report/index.html \
--txt coverage-report/summary.txt \
--print-summary \
--fail-under-line 80 \
--fail-under-branch 70
--fail-under-line 100 \
--fail-under-branch 100

- name: Upload coverage report
if: always()
Expand Down
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [2.4.0] - 2026-07-03

### Fixed

- Rejected NaN on scaled pack paths with `-PPACK_ERR_INVALARG`. Previously a NaN source value (or a NaN scaled result from the scale/offset arithmetic) reached a float-to-integer cast, which is undefined behaviour; ±infinity continues to saturate to the clamp bounds. Locked down with dedicated non-finite tests for every scaled type.

### Changed

- Resolved the required MISRA C:2023 violations for Rules 10.4, 10.7 and 10.8 through unsigned suffixes and temporary variables for composite expressions.
- Centralised the type-erased struct-member copies in internal `ppack_member_write` / `ppack_member_read` helpers; Rule 21.15 is now deviated at exactly three documented sites (the two helpers and the deliberate F32 `float`/`uint32_t` pun) rather than silenced per call site.
- Resolved advisory Rule 12.1 findings by making operator precedence explicit with parentheses.
- Moved `PPACK_PAYLOAD_UNITS` from `ppack_platform.h` to `ppack.h` to explicitly surface it as part of the public API. Include `ppack.h` (the documented entry point) rather than `ppack_platform.h` directly.
- Updated `README.md` and `CONTRIBUTING.md` to describe the tool-driven MISRA workflow.

### Removed

- Removed unused `PPACK_WORD_MASK` macro from `ppack_platform.h`.

### Added

- Adopted `misch` (cppcheck-backed MISRA C:2023 analysis) via `misra.toml`. The audit is clean; all deviations (advisory Rules 2.5, 8.7, 11.5, 15.5, 18.4 and required Rule 21.15) are justified at point of use with `cppcheck-suppress` `@deviation` comments or project-wide in `misra-deviations.txt`.
- Added a compile-time guard that `float` is exactly 32 bits (`PPACK_TYPE_F32` wire-format prerequisite).
- Added `-fsanitize=float-cast-overflow` to the CI sanitizer jobs: GCC does not include the float-to-integer conversion check in `-fsanitize=undefined`, leaving the Linux jobs blind to that UB class.
- Raised the CI coverage gate from 80% line / 70% branch to 100% / 100%, matching what the suite achieves.
- Documented error-path semantics for `ppack_pack` / `ppack_unpack` (destination contents are unspecified after a non-zero return) and clarified the 16-bit MAU `PPACK_TYPE_UINT8` storage contract (unpack zeroes the upper 8 bits of the member's storage unit).

## [2.3.0] - 2026-06-19

### Changed
Expand Down
16 changes: 6 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,15 @@ gcovr --root . --filter 'src/' --filter 'include/' --print-summary
- Public functions return `int` (negated `PPACK_ERR_*` on failure). No
`errno`, no exceptions.

## MISRA C:2012
## MISRA C:2023

The library is reviewed against MISRA C:2012. The full deviation record
lives in the file header of `src/ppack.c`. If your change introduces a
new deviation:
The library is analysed with `misch` (cppcheck-backed MISRA C:2023 analysis), configured by `misra.toml`. The audit is clean: `misch run` must report zero findings. If your change introduces a new finding:

1. Add an entry to the file-header deviation record (rule, sites,
justification, mitigation).
2. Add an inline `/* MISRA <rule> */` marker at the deviation site.
3. Justify the deviation in the PR description.
1. Prefer restructuring the code so the rule is satisfied. Deviate only when compliance would make the code genuinely worse, and never restructure purely to hide a violation from the checker; an honest deviation beats evasion.
2. Suppress at point of use with `/* cppcheck-suppress misra-c2012-<rule> ; @deviation <rationale> */`, or add a justified project-wide entry to `misra-deviations.txt` for house-style rules.
3. Verify with `misch run` and `misch deviations` (every suppression must carry a rationale), and justify the deviation in the PR description.

`required` rule deviations face a higher bar than `advisory`. We
currently have zero required-rule deviations and want to keep it that way.
`required` rule deviations face a higher bar than `advisory`. We currently carry exactly three, all Rule 21.15: the type-erased struct-member copies centralised in `ppack_member_write` / `ppack_member_read`, and the deliberate F32 `float`/`uint32_t` pun. Do not add required-rule deviations without strong justification; route new member copies through the existing helpers.

## Tests and coverage

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ For `PPACK_TYPE_UINT32` and `PPACK_TYPE_INT32` scaled fields, the float clamp us

For safety-critical applications where an out-of-range physical value MUST be detected, validate the input at the call site before calling `ppack_pack`.

### Non-finite values

Pack rejects a scaled field whose scaled result is NaN (a NaN source value, or a NaN produced by the scale/offset arithmetic) with `-PPACK_ERR_INVALARG`; NaN has no defined integer representation. Positive and negative infinity are treated like any other out-of-range value and saturate to the clamp bounds above. `PPACK_TYPE_F32` fields are a raw bit-copy and carry NaN/infinity payloads unchanged.

### Quantization

The float-to-integer cast in pack truncates toward zero (C standard cast semantics). The maximum round-trip error is one LSB, equal to `scale`. For round-to-nearest behaviour, pre-add `0.5f * scale * sign(physical)` at the call site before pack.
Expand Down Expand Up @@ -365,7 +369,7 @@ ppack is a serialisation primitive only. The following are explicitly out of sco
| Topic | Note |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Memory** | All operations use stack memory; no dynamic allocation |
| **MISRA C:2012 principles** | Designed with MISRA C:2012 principles: no dynamic allocation, no UB shifts, explicit error codes, `memcpy`-based type punning. A deviation record is maintained in the file header of `src/ppack.c` and at each deviation site (search for `MISRA`). Full tool-driven compliance requires running a certified static analyser against the source. |
| **MISRA C:2023** | Audited with `misch` (cppcheck-backed MISRA C:2023 analysis; configuration in `misra.toml`); the audit reports zero findings. Deviations are justified at point of use with `cppcheck-suppress ... @deviation` comments, or project-wide in `misra-deviations.txt`. The only required-rule deviations are three Rule 21.15 sites: the type-erased struct-member copies centralised in two internal helpers, and the deliberate F32 `float`/`uint32_t` pun. Design follows MISRA principles throughout: no dynamic allocation, no UB shifts, explicit error codes, `memcpy`-based type punning. |
| **Payload size** | Caller-supplied via `payload_bits` (multiple of 8, between 8 and 512). 64 matches CAN classic; 512 matches full CAN-FD. |
| **Bit ordering** | LSB-first within each byte; multi-byte fields little-endian; fields may span byte boundaries |
| **Field size** | 1-32 bits per field |
Expand Down
55 changes: 46 additions & 9 deletions include/ppack.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ extern "C" {
* without needing a separate include.
*/

/**
* @brief Number of addressable units occupied by a payload of
* @c PPACK_PAYLOAD_BITS bits.
*
* On 16-bit MAU platforms, ppack allocates one addressable unit per
* logical octet. This ensures compatibility with other primitives.
*
* Use this to declare a portable payload buffer:
* @code
* ppack_byte_t payload[PPACK_PAYLOAD_UNITS];
* @endcode
*/
/* cppcheck-suppress misra-c2012-2.5 ; @deviation Public API macro for caller buffer sizing. */
#define PPACK_PAYLOAD_UNITS (PPACK_PAYLOAD_BITS / PPACK_ADDR_UNIT_BITS)

/**
* @defgroup ppack_api Ppack Library
*
Expand Down Expand Up @@ -108,6 +123,14 @@ extern "C" {
* @c 2147483520 respectively (the next representable float
* below the type's true maximum).
*
* @b Non-finite values: pack rejects a SCALED field whose scaled
* result is NaN (a NaN source value, or a NaN produced by the
* scale/offset arithmetic) with @c -PPACK_ERR_INVALARG; NaN has no
* defined integer representation. Positive/negative infinity is
* treated like any other out-of-range value and saturates to the
* destination type's clamp bounds. @c PPACK_TYPE_F32 fields are a
* raw bit-copy and carry NaN/infinity payloads unchanged.
*
* ## 16-bit MAU Platform Notes
*
* On platforms with a 16-bit minimum addressable unit (MAU)
Expand All @@ -118,17 +141,19 @@ extern "C" {
* ensure interoperability with other primitives. Two contract points
* specific to this platform:
*
* @li A @c PPACK_TYPE_UINT8 field reads and writes only the LOW 8
* bits of its struct member's storage. On 16-bit MAU platforms
* the member's underlying 16-bit storage can technically hold 0..65535,
* but only the low 8 bits round-trip through the wire.
* @li A @c PPACK_TYPE_UINT8 field carries only the low 8 bits of its
* struct member through the wire. On pack, the upper 8 bits of
* the member's 16-bit storage are read but masked out. On
* unpack, the member's FULL storage unit is written: the low 8
* bits receive the wire value and the upper 8 bits are zeroed.
* Do not keep sentinel data in the upper bits of a
* @c PPACK_TYPE_UINT8 member; it will not survive an unpack.
*
* @li The @c ptr_offset value comes from @c offsetof(), which
* returns @c char-units. On 16-bit MAU platforms a @c char is 16
* bits, so the value is naturally in 16-bit units; the library
* does not need to translate it. Always use @c offsetof() rather
* than computing offsets by hand.

*
* @{
*/
Expand Down Expand Up @@ -251,8 +276,9 @@ struct ppack_field {
* @return -PPACK_ERR_INVALARG if @c field_count is 0, @c fields is NULL,
* @c payload_bits is 0, not a multiple of
* @c PPACK_ADDR_UNIT_BITS, or > 512;
* @c bit_length is 0 or > 32; or scaling is
* requested for @c PPACK_TYPE_UINT8
* @c bit_length is 0 or > 32; scaling is
* requested for @c PPACK_TYPE_UINT8; or a
* SCALED field's scaled result is NaN
* @return -PPACK_ERR_OVERFLOW if @c start_bit + bit_length exceeds
* @c payload_bits, or @c scale is 0.0 on a
* SCALED field
Expand All @@ -262,8 +288,13 @@ struct ppack_field {
* across threads or ISRs must provide their own mutual exclusion.
*
* @note Out-of-range scaled values clamp silently to the destination
* type's representable range. See "Scaled-field semantics" in
* the file-level docs.
* type's representable range; NaN is rejected. See "Scaled-field
* semantics" in the file-level docs.
*
* @note Not atomic on failure: the payload buffer is zeroed before the
* field descriptors are validated, so on an error return its
* contents are unspecified (zeroed and possibly partially
* written). Do not transmit a payload after a non-zero return.
*/
int ppack_pack(const void *base_ptr, void *payload, size_t payload_bits,
const struct ppack_field *fields, size_t field_count);
Expand Down Expand Up @@ -302,6 +333,12 @@ int ppack_pack(const void *base_ptr, void *payload, size_t payload_bits,
*
* @note Not thread-safe. Callers sharing @c base_ptr or @c payload
* across threads or ISRs must provide their own mutual exclusion.
*
* @note Not atomic on failure: fields are decoded in descriptor order,
* so on an error return the struct members of every field before
* the offending descriptor have already been overwritten. Treat
* the destination structure as unspecified after a non-zero
* return.
*/
int ppack_unpack(void *base_ptr, const void *payload, size_t payload_bits,
const struct ppack_field *fields, size_t field_count);
Expand Down
39 changes: 13 additions & 26 deletions include/ppack_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
* platforms), @c ppack_byte_t is @c uint16_t. Regardless of MAU size,
* ppack treats the payload as a sequence of 8-bit logical units to
* ensure interoperability with other primitives (like @c ucrc).

*
* @c PPACK_PAYLOAD_BITS is a user-overridable convenience macro that
* sizes @c PPACK_PAYLOAD_UNITS for stack buffer declarations. Define
* it before including the header (e.g. @c -DPPACK_PAYLOAD_BITS=128
* or @c -DPPACK_PAYLOAD_BITS=512) to change the default. The runtime
* sizes the @c PPACK_PAYLOAD_UNITS buffer-sizing macro (defined in
* @c ppack.h, the library's public entry point). Define it before
* including the header (e.g. @c -DPPACK_PAYLOAD_BITS=128 or
* @c -DPPACK_PAYLOAD_BITS=512) to change the default. The runtime
* @c ppack_pack / @c ppack_unpack API takes the payload size as an
* explicit argument and does not depend on this macro.
*
Expand Down Expand Up @@ -57,6 +57,7 @@
/** @brief Storage unit used for the payload buffer. */
typedef uint16_t ppack_byte_t;
#else
/* cppcheck-suppress misra-c2012-2.5 ; @deviation Public macro for external platform detection. */
#define PPACK_IS_16BIT_MAU 0
/** @brief Logical bits per payload unit. Always 8 for interoperability. */
#define PPACK_ADDR_UNIT_BITS 8u
Expand Down Expand Up @@ -84,7 +85,7 @@ typedef ppack_byte_t ppack_u8_t;

/**
* @brief Default payload size in bits for the @c PPACK_PAYLOAD_UNITS
* convenience macro.
* convenience macro (defined in @c ppack.h).
*
* Override at the toolchain level (e.g. @c -DPPACK_PAYLOAD_BITS=128)
* to size stack buffer declarations to a non-default payload. Must
Expand All @@ -98,24 +99,6 @@ typedef ppack_byte_t ppack_u8_t;
#define PPACK_PAYLOAD_BITS 64u
#endif

/**
* @brief Number of addressable units occupied by a payload of
* @c PPACK_PAYLOAD_BITS bits.
*
* On 16-bit MAU platforms, ppack allocates one addressable unit per
* logical octet. This ensures compatibility with other primitives.

*
* Use this to declare a portable payload buffer:
* @code
* ppack_byte_t payload[PPACK_PAYLOAD_UNITS];
* @endcode
*/
#define PPACK_PAYLOAD_UNITS (PPACK_PAYLOAD_BITS / PPACK_ADDR_UNIT_BITS)

/** @brief Bitmask covering one addressable unit. */
#define PPACK_WORD_MASK (((uint32_t)1u << PPACK_ADDR_UNIT_BITS) - 1u)

/**
* @brief Convert a payload bit index to its addressable-unit index.
*
Expand All @@ -142,16 +125,20 @@ ppack_bit_to_shift(uint16_t bit)
}

/* Catch unsupported configurations at compile time. */
_Static_assert(PPACK_ADDR_UNIT_BITS == 8 || PPACK_ADDR_UNIT_BITS == 16,
_Static_assert((PPACK_ADDR_UNIT_BITS == 8u) || (PPACK_ADDR_UNIT_BITS == 16u),
"ppack only supports 8-bit or 16-bit addressable units");

_Static_assert(PPACK_PAYLOAD_BITS > 0u
&& PPACK_PAYLOAD_BITS % PPACK_ADDR_UNIT_BITS == 0u,
_Static_assert((PPACK_PAYLOAD_BITS > 0u)
&& ((PPACK_PAYLOAD_BITS % PPACK_ADDR_UNIT_BITS) == 0u),
"PPACK_PAYLOAD_BITS must be a positive multiple of "
"PPACK_ADDR_UNIT_BITS");

_Static_assert(PPACK_PAYLOAD_BITS <= 512u,
"PPACK_PAYLOAD_BITS must be at most 512 "
"(CAN-FD frame data field ceiling)");

_Static_assert(sizeof(float) == sizeof(uint32_t),
"ppack requires a 32-bit float "
"(PPACK_TYPE_F32 wire-format prerequisite)");

#endif /* PPACK_PLATFORM_H_ */
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(
'ppack',
'c',
version: '2.3.0',
version: '2.4.0',
default_options: [
'c_std=c11',
'warning_level=3',
Expand Down
14 changes: 14 additions & 0 deletions misra-deviations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Project-level MISRA deviations for ppack. Each entry is preceded by the
# rationale that misch harvests into the deviation record. A bare rule id
# deviates that rule across every in-scope file; add :file or :file:line to
# narrow the scope.

# Rule 15.5 (a function should have a single point of exit) is deviated
# project-wide as a deliberate house style. Precondition guard clauses that
# return early on invalid input and the branch-per-outcome returns in the
# pack/unpack type-dispatch switches are clearer and less error-prone than
# threading a result variable through nested if/else. None of these
# functions acquire a resource, take a lock, or have any teardown, so there
# is no shared cleanup that a single exit point would protect. Advisory;
# deviation accepted.
misra-c2012-15.5
Loading
Loading