diff options
author | Sergey Kandaurov <pluknet@nginx.com> | 2024-03-18 17:14:30 +0400 |
---|---|---|
committer | Sergey Kandaurov <pluknet@nginx.com> | 2024-03-18 17:14:30 +0400 |
commit | 3d5a356abb4f06b0f103290bd31a4c146233956b (patch) | |
tree | b380dca90f465f05a35f7483beeeacb5496cb54e /src/stream/ngx_stream_geo_module.c | |
parent | d3d64cacb3ce96477d354fe17d3b5c6e348f933a (diff) | |
download | nginx-3d5a356abb4f06b0f103290bd31a4c146233956b.tar.gz nginx-3d5a356abb4f06b0f103290bd31a4c146233956b.zip |
Fixed undefined behaviour with IPv4-mapped IPv6 addresses.
Previously, it could result when left-shifting signed integer due to implicit
integer promotion, such that the most significant bit appeared on the sign bit.
In practice, though, this results in the same left value as with an explicit
cast, at least on known compilers, such as GCC and Clang. The reason is that
in_addr_t, which is equivalent to uint32_t and same as "unsigned int" in ILP32
and LP64 data type models, has the same type width as the intermediate after
integer promotion, so there's no side effects such as sign-extension. This
explains why adding an explicit cast does not change object files in practice.
Found with UndefinedBehaviorSanitizer (shift).
Based on a patch by Piotr Sikora.
Diffstat (limited to 'src/stream/ngx_stream_geo_module.c')
-rw-r--r-- | src/stream/ngx_stream_geo_module.c | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/stream/ngx_stream_geo_module.c b/src/stream/ngx_stream_geo_module.c index a9e10100f..2324bef0d 100644 --- a/src/stream/ngx_stream_geo_module.c +++ b/src/stream/ngx_stream_geo_module.c @@ -190,7 +190,7 @@ ngx_stream_geo_cidr_variable(ngx_stream_session_t *s, p = inaddr6->s6_addr; if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { - inaddr = p[12] << 24; + inaddr = (in_addr_t) p[12] << 24; inaddr += p[13] << 16; inaddr += p[14] << 8; inaddr += p[15]; @@ -263,7 +263,7 @@ ngx_stream_geo_range_variable(ngx_stream_session_t *s, if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { p = inaddr6->s6_addr; - inaddr = p[12] << 24; + inaddr = (in_addr_t) p[12] << 24; inaddr += p[13] << 16; inaddr += p[14] << 8; inaddr += p[15]; |