When implementing commands with numeric failure modes (overflow, out-of-range, NaN/Inf) or multi-option parsing, define an explicit error contract and enforce it end-to-end:
1) Establish FAIL vs CLAMP semantics
2) Validate inputs and option combinations early
3) Propagate errors immediately
4) Respect fatal subsystem state
Example (numeric failure contract sketch):
bool clamp_mode = (flags & OBJ_ONBOUND_CLAMP);
// Validate/parse first; no state mutation before checks.
if (parse_value_or_reply(c, &value) != C_OK) return;
if (parse_bounds_or_reply(c, &lb, &ub) != C_OK) return;
value2 = value + incr;
if (isnan(value2) || isinf(value2)) {
addReplyError(c, "increment would produce NaN/Infinity");
return; // FAIL always
}
if (value2 < lb || value2 > ub) {
if (!clamp_mode) {
addReplyError(c, "value is out of bounds");
return; // do not modify key
}
value2 = (value2 < lb) ? lb : ub; // CLAMP
}
// Only now: perform the key update + reply.
set_key_value(c, value2);
Adopting this standard prevents inconsistent edge-case behavior, avoids partial updates on failure, and makes error handling predictable for both client-facing semantics and internal defensive safety.
Enter the URL of a public GitHub repository