Sometimes - not always - unconscious units become overstunned upon applying handcuffs. Checking the save file, health on these units declines every turn. From what I can tell (by manually reducing health in the save), the unit eventually dies from overstun. There is no way to prevent this in-game, as the stun level can't be reduced and handcuffs can not be removed. This makes applying handcuffs a potential liability.
In examining this case, I concluded that the maximum safety stun level of the wounded creatures (before they begin to suffer damage from overstun) is lower than maxStunLevel. I therefore reduce maxStunLevel by the percentage reduction of the creature's health.
For example, there is a healthy creature with 100 HP and maxStunLevel 200, i.e. when it receives 201 stun level, it will receive damage from overstun.
We will deal 40 damage to this creature and its current health will be 60 HP. In this state the creature will take damage from overstun at (very approximately) 120 stun level.
I have implemented this algorithm in the current version of XCF at GitHub. But I need to run many different tests to make sure that this solution will always work fine.