estraier / tkrzw

a set of implementations of DBM
Apache License 2.0
164 stars 20 forks source link

Fix UB: int64_t >> 64 #39

Open iacore opened 1 year ago

iacore commented 1 year ago

In https://en.cppreference.com/w/cpp/language/operator_arithmetic, section "Bitwise shift operators"

In any case, if the value of the right operand is negative or is greater or equal to the number of bits in the promoted left operand, the behavior is undefined.

I handled the specific UB case.

estraier commented 1 year ago

0 is not negative. I'll modify the assertion to be "width >= 0 && width < sizeof(int64_t)".

iacore commented 1 year ago

when width is 0, ((sizeof(num) - width) * 8) is 64, which is UB

estraier commented 1 year ago

Good point. Then, I'll put assert(width > 0).