@@ -2366,6 +2366,9 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_0(
23662366 wolfsentry_route_flags_t current_rule_route_flags;
23672367 wolfsentry_errcode_t ret;
23682368 wolfsentry_time_t now;
2369+ int penalty_triggered = 0;
2370+ wolfsentry_hitcount_t derog_snap;
2371+ wolfsentry_hitcount_t commend_snap;
23692372
23702373 if (target_route == NULL)
23712374 WOLFSENTRY_ERROR_RETURN(INVALID_ARG);
@@ -2553,20 +2556,25 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_0(
25532556 }
25542557 }
25552558
2559+ /* Snapshot atomic counts once so the guard and arithmetic operate on the
2560+ * same values (avoid TOCTOU between successive loads). */
2561+ derog_snap = WOLFSENTRY_ATOMIC_LOAD(rule_route->meta.derogatory_count);
2562+ commend_snap = WOLFSENTRY_ATOMIC_LOAD(rule_route->meta.commendable_count);
2563+ if (config->config.derogatory_threshold_for_penaltybox > 0) {
2564+ if (config->config.flags & WOLFSENTRY_EVENTCONFIG_FLAG_DEROGATORY_THRESHOLD_IGNORE_COMMENDABLE) {
2565+ penalty_triggered = (derog_snap >= config->config.derogatory_threshold_for_penaltybox);
2566+ } else {
2567+ penalty_triggered = (derog_snap >= commend_snap)
2568+ && ((derog_snap - commend_snap)
2569+ >= config->config.derogatory_threshold_for_penaltybox);
2570+ }
2571+ }
2572+
25562573 if (current_rule_route_flags & WOLFSENTRY_ROUTE_FLAG_PENALTYBOXED) {
25572574 *action_results |= WOLFSENTRY_ACTION_RES_REJECT;
25582575 ret = WOLFSENTRY_ERROR_ENCODE(OK);
25592576 goto done;
2560- } else if ((config -> config .derogatory_threshold_for_penaltybox > 0 )
2561- && ((config -> config .flags & WOLFSENTRY_EVENTCONFIG_FLAG_DEROGATORY_THRESHOLD_IGNORE_COMMENDABLE ) ?
2562- (WOLFSENTRY_ATOMIC_LOAD (rule_route -> meta .derogatory_count )
2563- >= config -> config .derogatory_threshold_for_penaltybox )
2564- :
2565- ((WOLFSENTRY_ATOMIC_LOAD (rule_route -> meta .derogatory_count )
2566- >= WOLFSENTRY_ATOMIC_LOAD (rule_route -> meta .commendable_count ))
2567- && ((wolfsentry_hitcount_t )(WOLFSENTRY_ATOMIC_LOAD (rule_route -> meta .derogatory_count )
2568- - WOLFSENTRY_ATOMIC_LOAD (rule_route -> meta .commendable_count ))
2569- >= config -> config .derogatory_threshold_for_penaltybox ))))
2577+ } else if (penalty_triggered)
25702578 {
25712579 wolfsentry_route_flags_t flags_before;
25722580 WOLFSENTRY_WARN_ON_FAILURE(
0 commit comments