abseil-cpp icon indicating copy to clipboard operation
abseil-cpp copied to clipboard

signed/unsigned compare in btree.h

Open faceprint opened this issue 5 years ago • 7 comments

Describe the bug

Since the enum containing kNodeValues was changed to be explicitly uint32_t, there is now a signed/unsigned comparison:

external/abseil/absl/container/internal/btree.h:2439:19: error: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Werror=sign-compare] 2439 | if (max_count < kNodeValues) { | ~~~~~~~~~~^~~~~~~~~~~~~

Steps to reproduce the bug

Compile revision c6b3f2cf583d8fec124f9d70a172b513363506fe with sign comparison warnings turned on

What version of Abseil are you using? HEAD (c6b3f2cf583d8fec124f9d70a172b513363506fe and later)

What operating system and version are you using Linux (RHEL 6)

What compiler and version are you using? gcc 9.1.0 clang 8.0.1 clang 9.0.0

What build system are you using? cmake 3.15.0

Additional context

faceprint avatar Aug 20 '20 13:08 faceprint

I also ran into this problem building Abseil from HEAD and using clang-12

asraa avatar Aug 21 '20 19:08 asraa

A fix for this should be exported to GitHub today.

derekmauro avatar Aug 27 '20 15:08 derekmauro

Unfortunately, there are more issues, my build system just stopped after the first failure:

external/abseil/absl/container/internal/btree.h:2252:45: fatal error: comparison of integers of different signs: 'int' and 
'absl::container_internal::btree<absl::container_internal::set_params<StringType, StringCmp, std::__1::allocator<StringType >, 256, false> >::(anonymous enum at <external/abseil/absl/container/internal/btree.h>:1052:3)' [-Wsign-compare]
                      (1 + (insert_position < kNodeValues));

I attempted to fix so I could provide all the spots that need fixing, but the int here is set from iterator->position, which is purposefully signed, as it uses -1 as a special value for invalid positions:

  1006   // The position within the node of the tree the iterator is pointing at.                                                                                                                                               
  1007   // NOTE: this is an int rather than a field_type because iterators can point                                                                                                                                           
  1008   // to invalid positions (such as -1) in certain circumstances.                                                                                                                                                         
  1009   int position;   

I'm not sure what your preferred solution here would be.

faceprint avatar Aug 28 '20 12:08 faceprint

Open

termizil avatar Aug 29 '20 20:08 termizil

Signed

termizil avatar Aug 29 '20 20:08 termizil

#https://github.com/abseil/abseil-cpp/issues/771#issue-682705311

termizil avatar Aug 29 '20 20:08 termizil

I'm seeing the same thing. It goes on to complain about similar things:

[...]abseil/absl/container/internal/btree.h:2292:37: error: comparison of integers of different signs: 'int' and 'absl::container_internal::btree<absl::container_internal::map_params<const MY_TYPE *, float, std::__1::less<const MY_TYPE *>, std::__1::allocator<std::__1::pair<const MY_TYPE *const, float> >, 256, false> >::(anonymous enum at [...]abseil/absl/container/internal/btree.h:1056:3)' [-Werror,-Wsign-compare]
            left->count() + to_move < kNodeValues) {
            ~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~

where kNodeValues is an enum, to_move is int and so an int. I think it should just be static_cast<int>(kNodeValues) in place of kNodeValues.

BenFrantzDale avatar Sep 25 '20 18:09 BenFrantzDale