[MDEV-30401] Bitmap type - Shifting 64-bit value by 64 bits is undefined (cppcheck) Created: 2023-01-13  Updated: 2023-01-16  Resolved: 2023-01-13

Status: Closed
Project: MariaDB Server
Component/s: Server
Affects Version/s: 10.5.18
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Daniel Black Assignee: Vladislav Vaintroub
Resolution: Not a Bug Votes: 0
Labels: None

Issue Links:
Problem/Incident
is caused by MDEV-19702 Refactor Bitmap<N> to be based on ulo... Closed

 Description   

This is a cppcheck found item. Seems to be valid. Not 100% sure.

10.11

Checking storage/perfschema/pfs_global.cc: O_NONBLOCK=1;INT_MAX=0x7FFFFFFF;MAP_ANONYMOUS=1;__GNUC__=1;HAVE_CONFIG_H=1;MYSQL_SERVER=1;_FILE_OFFSET_BITS=64;NDEBUG=1;_FORTIFY_SOURCE=2;DBUG_OFF=1;HAVE_OPENSSL=1;OPENSSL_API_COMPAT=0x10100000L...
mariadb-server-10.11/sql/sql_bitmap.h:78:39: error: Shifting 64-bit value by 64 bits is undefined behaviour [shiftTooManyBits]
    return ARRAY_ELEMENTS == 1 ? 1ULL << n : 1ULL << (n % BITS_PER_ELEMENT);
                                      ^
sql/sql_bitmap.h:190:53: note: Calling function 'last_element_mask', 1st argument '64' value is 64
      buffer[ARRAY_ELEMENTS - 1]= last_element_mask(width);
                                                    ^
mariadb-server-10.11/sql/sql_bitmap.h:83:21: note: Calling function 'bit_mask', 1st argument 'n' value is 64
    return bit_mask(n) - 1;
                    ^
mariadb-server-10.11/sql/sql_bitmap.h:78:39: note: Shift
    return ARRAY_ELEMENTS == 1 ? 1ULL << n : 1ULL << (n % BITS_PER_ELEMENT);
                                      ^

What this doesn't show is sql_bitmask.h:77

DBUG_ASSERT(n < width);

So last_element_mask(width) - results in bit_mask(width) which misses the DBUG_ASSERT(n < width) in production builds.

This would get called in:

storage/maria/ha_maria.cc:ha_maria::info    share->keys_in_use.intersect_extended(maria_info.key_map);

(and myisam in same way).



 Comments   
Comment by Vladislav Vaintroub [ 2023-01-13 ]

Nope, not valid.

mariadb-server-10.11/sql/sql_bitmap.h:78:39: error: Shifting 64-bit value by 64 bits is undefined behaviour [shiftTooManyBits]
return ARRAY_ELEMENTS == 1 ? 1ULL << n : 1ULL << (n % BITS_PER_ELEMENT);

Alleged error happens if ARRAY_ELEMENTS == 1, only but the caller
sql/sql_bitmap.h:190:53: note: Calling function 'last_element_mask' ...

has the condition if (ARRAY_ELEMENTS > 1) on previous line 189

Comment by Daniel Black [ 2023-01-16 ]

Thanks wlad, I was hoping I missed something obvious. Raised with cppcheck.

Generated at Thu Feb 08 10:15:58 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.