Q027FreeFirmware
Intersect redundant-core fault lists
Question
Return the multiset intersection of two bounded fault-ID lists, preserving the first list's encounter order and consuming each match only once. Use C11 on the stated flat-address platform, where uintptr_t preserves byte addresses. Nonempty spans refer to live objects of the stated type and length. Null is allowed only for a zero-length input or zero-capacity output buffer; nout always identifies a writable size_t object. Validate arithmetic and alignment, then require the entire declared output buffer and nout to be disjoint from each other and from both active inputs. The two read-only inputs may overlap. Descriptor errors take priority over insufficient capacity.
Implementation scaffold
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
typedef enum { IX_OK, IX_EINVAL, IX_ENOSPC } ix_rc_t;
enum { IX_MAX = 32 };
// Supplied flat-address span helpers. Zero bytes require no object.
static bool valid_span(const void *ptr, size_t bytes, size_t alignment) {
if (bytes == 0) return true;
const uintptr_t base = (uintptr_t)ptr;
return ptr != NULL && base % alignment == 0 && bytes <= UINTPTR_MAX - base;
}
// Call only after validating both nonempty spans.
static bool overlap(const void *a, size_t an, const void *b, size_t bn) {
if (an == 0 || bn == 0) return false;
const uintptr_t ab = (uintptr_t)a, bb = (uintptr_t)b;
return ab < bb + bn && bb < ab + an;
}
ix_rc_t fault_intersection(const uint16_t *a, size_t na,
const uint16_t *b, size_t nb,
uint16_t *out, size_t cap, size_t *nout) {
// TODO: validate all spans, stage stable matches, then commit both outputs.
}
Trace one case
A=[5,2,5,7]; B=[5,5,8]intersection=[5,5]Both copies of 5 are consumed once in A encounter order; 2 and 7 have no available match.
Requirements
- Emit each identifier min(count_a, count_b) times in A's encounter order.
- Use fixed state for at most 32 entries and do not modify either input.
- Count the complete result before writing and return no-space without exposing a partial output.
- Validate lengths, pointers, byte-count/address arithmetic and alignment before reading inputs. Reject any overlap between the declared output buffer, nout and the active inputs; the inputs may overlap each other. Leave both outputs and both inputs unchanged on every error.
