## Summary

- Fix swapped comparison values in `atomic_stdatomic.h` `dec_and_test` and 
`inc_and_test` functions
- `atomic_fetch_sub_explicit()` returns the **pre-decrement** value, so testing 
"result == 0" requires comparing the old value against `1`, not `-1`
- The symmetric bug exists in `inc_and_test` (compares `== 1` instead of `== 
-1`)
- All 8 `_and_test` functions are fixed (int/long × relaxed/seq_cst)

### Impact

This header is only used on aarch64 (ARM64). The bug causes 
`atomic_dec_and_test` to always return false, so any refcount-guarded `free()` 
never fires. Affected subsystems include TM transactions, DNS cache, TCP 
connections, config framework, and WebSocket connections — all leak shared 
memory monotonically on aarch64.

### Reproduction

Reproduced on Apple Silicon (aarch64) using Docker with @whosgonna's 
[reproduction project](https://github.com/whosgonna/6.1.1_memleak). Unpatched 
6.1.1 leaks ~175 KB/30s (linear, never returns to baseline). Patched 6.1.1 is 
stable with zero sustained growth.

Fixes: #4626

## Test plan

- [x] Reproduced leak on unpatched 6.1.1 aarch64 (20 samples, linear growth)
- [x] Verified patched 6.1.1 aarch64 has zero sustained memory growth (14 
samples, returns to baseline)
- [x] Verified x86 semantics match: `decl` + `setz` tests post-decrement ZF 
(result == 0)
- [x] Verified `atomic_unknown.h` semantics match: `ret = --(*var); return (ret 
== 0)`
- [x] Confirmed all 8 _and_test functions have the same bug pattern
You can view, comment on, or merge this pull request online at:

  https://github.com/kamailio/kamailio/pull/4638

-- Commit Summary --

  * core: fix swapped comparison in atomic_stdatomic.h dec/inc_and_test

-- File Changes --

    M src/core/atomic/atomic_stdatomic.h (16)

-- Patch Links --

https://github.com/kamailio/kamailio/pull/4638.patch
https://github.com/kamailio/kamailio/pull/4638.diff

-- 
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/pull/4638
You are receiving this because you are subscribed to this thread.

Message ID: <kamailio/kamailio/pull/[email protected]>
_______________________________________________
Kamailio - Development Mailing List -- [email protected]
To unsubscribe send an email to [email protected]
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to