feat(raft-library): reject even n_peers in raft_init; add test_raft_init_even_peers_returns_einval

This commit is contained in:
Rongsong Shen 2026-05-31 08:48:30 +00:00
parent fd9933c9d0
commit becf6ee765
2 changed files with 48 additions and 6 deletions

View file

@ -15,6 +15,11 @@ int raft_init(raft_state_t *s, const raft_config_t *cfg)
/* Validate required callbacks and n_peers before touching *s */
if (cfg->n_peers < 1)
return -EINVAL;
/* Even cluster sizes (>1) have no fault-tolerance advantage over the next
* smaller odd size and can mislead operators into thinking split-brain is
* impossible. Reject to prevent misconfiguration. */
if (cfg->n_peers > 1 && cfg->n_peers % 2 == 0)
return -EINVAL;
if (!cfg->cbs)
return -EINVAL;
if (!cfg->cbs->on_send_vote_request)