cobs icon indicating copy to clipboard operation
cobs copied to clipboard

Fix undefined behaviour of char bit shifting when combining classic i…

Open Zhicheng-Liu opened this issue 3 years ago • 1 comments

…ndices

Previously when combining multiple classic indices into a single classic index, the contents of source indices are read in as char. During the interleaving process, depending on the current position of the destination index, both left and right shifts on the next char could be performed.

However, there are a few undefined behaviours that could affect the results depending on the platform:

  1. The signedness of a char is an undefined behaviour. Hence when bit shifting, the usual arithmetic conversion performed on the char is undefined. The char could be promoted to either signed int or unsigned int.
  2. If the char is treated as signed int, the bit shifting (both left and right) is also undefined in pre-c++20 standards. The behaviour is platform dependent.

This change fixes the issue by declare the contents read from source indices as unsigned char.

Zhicheng-Liu avatar Jun 23 '21 11:06 Zhicheng-Liu

@bingmann and @leoisl, could you please review this pull request?

Zhicheng-Liu avatar Jul 02 '21 10:07 Zhicheng-Liu