openresty / lua-resty-balancer

A generic consistent hash implementation for OpenResty/Lua
322 stars 77 forks source link

Make explicitly type conversion on _gcd() parameter to prevent infinite loop #45

Closed shihpeng closed 2 years ago

shihpeng commented 2 years ago

Reference Issues

The current implementation may cause _gcd() into an infinite loop if the parameter b is a unexpected value like string '0'. Which caused the video platform Bilibili its massive outage on 2021/7/13.

Reference: https://mp.weixin.qq.com/s/zCOnUEp25xYqx7BMyrLIGQ

The change

To prevent the condition makes the infinite loop, we have to make an explicit type conversion so that the b == 0 condition can be met when there's any undesired value.

jizhuozhi commented 2 years ago

This is not caused by gcd function, and this type safe issue has already been fixed https://github.com/openresty/lua-resty-balancer/pull/40