Bug Summary

File:deps/lua/src/lua_bit.c
Warning:line 117, column 1
The result of the right shift is undefined due to shifting by '32', which is greater or equal to the width of type 'UBits'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name lua_bit.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D LUA_ANSI -D ENABLE_CJSON_GLOBAL -D REDIS_STATIC= -D LUA_USE_MKSTEMP -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir /home/netto/Desktop/redis-6.2.1/deps/lua/src -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-03-14-133648-8817-1 -x c lua_bit.c
1/*
2** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
3** http://bitop.luajit.org/
4**
5** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
6**
7** Permission is hereby granted, free of charge, to any person obtaining
8** a copy of this software and associated documentation files (the
9** "Software"), to deal in the Software without restriction, including
10** without limitation the rights to use, copy, modify, merge, publish,
11** distribute, sublicense, and/or sell copies of the Software, and to
12** permit persons to whom the Software is furnished to do so, subject to
13** the following conditions:
14**
15** The above copyright notice and this permission notice shall be
16** included in all copies or substantial portions of the Software.
17**
18** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25**
26** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
27*/
28
29#define LUA_BITOP_VERSION"1.0.2" "1.0.2"
30
31#define LUA_LIB
32#include "lua.h"
33#include "lauxlib.h"
34
35#ifdef _MSC_VER
36/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
37typedef __int32 int32_t;
38typedef unsigned __int32 uint32_t;
39typedef unsigned __int64 uint64_t;
40#else
41#include <stdint.h>
42#endif
43
44typedef int32_t SBits;
45typedef uint32_t UBits;
46
47typedef union {
48 lua_Number n;
49#ifdef LUA_NUMBER_DOUBLE
50 uint64_t b;
51#else
52 UBits b;
53#endif
54} BitNum;
55
56/* Convert argument to bit type. */
57static UBits barg(lua_State *L, int idx)
58{
59 BitNum bn;
60 UBits b;
61#if LUA_VERSION_NUM501 < 502
62 bn.n = lua_tonumber(L, idx);
63#else
64 bn.n = luaL_checknumber(L, idx);
65#endif
66#if defined(LUA_NUMBER_DOUBLE)
67 bn.n += 6755399441055744.0; /* 2^52+2^51 */
68#ifdef SWAPPED_DOUBLE
69 b = (UBits)(bn.b >> 32);
70#else
71 b = (UBits)bn.b;
72#endif
73#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
74 defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
75 defined(LUA_NUMBER_LLONG)
76 if (sizeof(UBits) == sizeof(lua_Number))
77 b = bn.b;
78 else
79 b = (UBits)(SBits)bn.n;
80#elif defined(LUA_NUMBER_FLOAT)
81#error "A 'float' lua_Number type is incompatible with this library"
82#else
83#error "Unknown number type, check LUA_NUMBER_* in luaconf.h"
84#endif
85#if LUA_VERSION_NUM501 < 502
86 if (b == 0 && !lua_isnumber(L, idx)) {
87 luaL_typerror(L, idx, "number");
88 }
89#endif
90 return b;
91}
92
93/* Return bit type. */
94#define BRET(b)lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1; lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
95
96static int bit_tobit(lua_State *L) { BRET(barg(L, 1))lua_pushnumber(L, (lua_Number)(SBits)(barg(L, 1))); return 1; }
97static int bit_bnot(lua_State *L) { BRET(~barg(L, 1))lua_pushnumber(L, (lua_Number)(SBits)(~barg(L, 1))); return 1
;
}
98
99#define BIT_OP(func, opr)static int func(lua_State *L) { int i; UBits b = barg(L, 1); for
(i = lua_gettop(L); i > 1; i--) b opr barg(L, i); lua_pushnumber
(L, (lua_Number)(SBits)(b)); return 1; }
\
100 static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
101 for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b)lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1; }
102BIT_OP(bit_band, &=)static int bit_band(lua_State *L) { int i; UBits b = barg(L, 1
); for (i = lua_gettop(L); i > 1; i--) b &= barg(L, i)
; lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1; }
103BIT_OP(bit_bor, |=)static int bit_bor(lua_State *L) { int i; UBits b = barg(L, 1
); for (i = lua_gettop(L); i > 1; i--) b |= barg(L, i); lua_pushnumber
(L, (lua_Number)(SBits)(b)); return 1; }
104BIT_OP(bit_bxor, ^=)static int bit_bxor(lua_State *L) { int i; UBits b = barg(L, 1
); for (i = lua_gettop(L); i > 1; i--) b ^= barg(L, i); lua_pushnumber
(L, (lua_Number)(SBits)(b)); return 1; }
105
106#define bshl(b, n)(b << n) (b << n)
107#define bshr(b, n)(b >> n) (b >> n)
108#define bsar(b, n)((SBits)b >> n) ((SBits)b >> n)
109#define brol(b, n)((b << n) | (b >> (32-n))) ((b << n) | (b >> (32-n)))
110#define bror(b, n)((b << (32-n)) | (b >> n)) ((b << (32-n)) | (b >> n))
111#define BIT_SH(func, fn)static int func(lua_State *L) { UBits b = barg(L, 1); UBits n
= barg(L, 2) & 31; lua_pushnumber(L, (lua_Number)(SBits)
(fn(b, n))); return 1; }
\
112 static int func(lua_State *L) { \
113 UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n))lua_pushnumber(L, (lua_Number)(SBits)(fn(b, n))); return 1; }
114BIT_SH(bit_lshift, bshl)static int bit_lshift(lua_State *L) { UBits b = barg(L, 1); UBits
n = barg(L, 2) & 31; lua_pushnumber(L, (lua_Number)(SBits
)((b << n))); return 1; }
115BIT_SH(bit_rshift, bshr)static int bit_rshift(lua_State *L) { UBits b = barg(L, 1); UBits
n = barg(L, 2) & 31; lua_pushnumber(L, (lua_Number)(SBits
)((b >> n))); return 1; }
116BIT_SH(bit_arshift, bsar)static int bit_arshift(lua_State *L) { UBits b = barg(L, 1); UBits
n = barg(L, 2) & 31; lua_pushnumber(L, (lua_Number)(SBits
)(((SBits)b >> n))); return 1; }
117BIT_SH(bit_rol, brol)static int bit_rol(lua_State *L) { UBits b = barg(L, 1); UBits
n = barg(L, 2) & 31; lua_pushnumber(L, (lua_Number)(SBits
)(((b << n) | (b >> (32-n))))); return 1; }
The result of the right shift is undefined due to shifting by '32', which is greater or equal to the width of type 'UBits'
118BIT_SH(bit_ror, bror)static int bit_ror(lua_State *L) { UBits b = barg(L, 1); UBits
n = barg(L, 2) & 31; lua_pushnumber(L, (lua_Number)(SBits
)(((b << (32-n)) | (b >> n)))); return 1; }
119
120static int bit_bswap(lua_State *L)
121{
122 UBits b = barg(L, 1);
123 b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
124 BRET(b)lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
125}
126
127static int bit_tohex(lua_State *L)
128{
129 UBits b = barg(L, 1);
130 SBits n = lua_isnone(L, 2)(lua_type(L, (2)) == (-1)) ? 8 : (SBits)barg(L, 2);
131 const char *hexdigits = "0123456789abcdef";
132 char buf[8];
133 int i;
134 if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
135 if (n > 8) n = 8;
136 for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
137 lua_pushlstring(L, buf, (size_t)n);
138 return 1;
139}
140
141static const struct luaL_Reg bit_funcs[] = {
142 { "tobit", bit_tobit },
143 { "bnot", bit_bnot },
144 { "band", bit_band },
145 { "bor", bit_bor },
146 { "bxor", bit_bxor },
147 { "lshift", bit_lshift },
148 { "rshift", bit_rshift },
149 { "arshift", bit_arshift },
150 { "rol", bit_rol },
151 { "ror", bit_ror },
152 { "bswap", bit_bswap },
153 { "tohex", bit_tohex },
154 { NULL((void*)0), NULL((void*)0) }
155};
156
157/* Signed right-shifts are implementation-defined per C89/C99.
158** But the de facto standard are arithmetic right-shifts on two's
159** complement CPUs. This behaviour is required here, so test for it.
160*/
161#define BAD_SAR(((SBits)-8 >> 2) != (SBits)-2) (bsar(-8, 2)((SBits)-8 >> 2) != (SBits)-2)
162
163LUALIB_APIextern int luaopen_bit(lua_State *L)
164{
165 UBits b;
166 lua_pushnumber(L, (lua_Number)1437217655L);
167 b = barg(L, -1);
168 if (b != (UBits)1437217655L || BAD_SAR(((SBits)-8 >> 2) != (SBits)-2)) { /* Perform a simple self-test. */
169 const char *msg = "compiled with incompatible luaconf.h";
170#ifdef LUA_NUMBER_DOUBLE
171#ifdef _WIN32
172 if (b == (UBits)1610612736L)
173 msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
174#endif
175 if (b == (UBits)1127743488L)
176 msg = "not compiled with SWAPPED_DOUBLE";
177#endif
178 if (BAD_SAR(((SBits)-8 >> 2) != (SBits)-2))
179 msg = "arithmetic right-shift broken";
180 luaL_error(L, "bit library self-test failed (%s)", msg);
181 }
182#if LUA_VERSION_NUM501 < 502
183 luaL_register(L, "bit", bit_funcs);
184#else
185 luaL_newlib(L, bit_funcs);
186#endif
187 return 1;
188}
189