Skip to content

Commit

Permalink
Fixes UB in pcl::poisson::OctNode
Browse files Browse the repository at this point in the history
Example

```cpp
/// Must be compiled with -fsanitze=undefined
#include <iostream>
#include <stdio.h>

long long _InterleaveBits( int p[3] )
{
  long long key = 0;
  for( int i=0 ; i<32 ; i++ ) 
  {
    key |= ( ( p[0] & (1<<i) )<<(2*i) ) | ( ( p[1] & (1<<i) )<<(2*i+1) ) | ( ( p[2] & (1<<i) )<<(2*i+2) );
  }
  return key;
}

long long _FixedInterleaveBits( int _p[3] )
{
  long long key = 0;
  long long p[3] = {_p[0],_p[1],_p[2]};
  for( int i=0 ; i<31 ; i++ ) 
  {
    key |= ( ( p[0] & (1ull<<i) )<<(2*i) ) | ( ( p[1] & (1ull<<i) )<<(2*i+1) ) | ( ( p[2] & (1ull<<i) )<<(2*i+2) );
  }
  return key;
}

int main() {
  int p[3] = {0,255,128};
  std::cout << _InterleaveBits(p) << std::endl;
  std::cout << _FixedInterleaveBits(p) << std::endl;
}
```
  • Loading branch information
RLThomaz committed Jan 16, 2020
1 parent f9c0154 commit 38cccab
Showing 1 changed file with 2 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,8 @@ namespace pcl
long long _InterleaveBits( int p[3] )
{
long long key = 0;
for( int i=0 ; i<32 ; i++ ) key |= ( ( p[0] & (1<<i) )<<(2*i) ) | ( ( p[1] & (1<<i) )<<(2*i+1) ) | ( ( p[2] & (1<<i) )<<(2*i+2) );
long long _p[3] = {p[0],p[1],p[2]};
for( int i=0 ; i<31 ; i++ ) key |= ( ( _p[0] & (1ull<<i) )<<(2*i) ) | ( ( _p[1] & (1ull<<i) )<<(2*i+1) ) | ( ( _p[2] & (1ull<<i) )<<(2*i+2) );
return key;
}
template <class NodeData,class Real>
Expand Down

0 comments on commit 38cccab

Please sign in to comment.