5

I found a bug in my code that only happens when I enable compiler optimizations -O1 or greater. I traced the bug and it seems that I can't use the boost type_erased adaptor on a boost transformed range when optimizations are enabled. I wrote this c++ program to reproduce it:

#include <iostream>
#include <vector>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/adaptor/type_erased.hpp>

using namespace boost::adaptors;
using namespace std;

int addOne(int b) {
  return b + 1;
}

int main(int, char**) {
  vector<int> nums{ 1, 2, 3 };

  auto result1 = nums | transformed(addOne) | type_erased<int, boost::forward_traversal_tag>();
  auto result2 = nums | transformed(addOne);
  auto result3 = nums | type_erased<int, boost::forward_traversal_tag>();

  for (auto n : result1)
    cout << n << " ";
  cout << endl;

  for (auto n : result2)
    cout << n << " ";
  cout << endl;

  for (auto n : result3)
    cout << n << " ";
  cout << endl;
}

When I run this program without any optimizations, I get the following output:

2 3 4
2 3 4
1 2 3

When I run it with the -O1 flag, I get the following:

1 1 1
2 3 4
1 2 3

I am using clang++ to compile it. The version of clang that I am using is:

Apple LLVM version 8.0.0 (clang-800.0.38)

I don't know if I am doing something wrong, or if it is a boost/clang bug.

edit:

Changed it to

type_erased<int, boost::forward_traversal_tag, const int>()

and it works now. The third template argument is the reference type, setting the reference to const prolongs the timespan of the temporary created by the transformed.

4
  • Removing type_erased from the computation of result1 gives correct result even with optimization.
    – Arunmu
    Nov 8, 2016 at 5:39
  • Yeah I know that, that would be equivalent to result2 which is correct. But I need the type of result to be type erased. I used result2 and result3 to demonstrate that both adaptors work if not used together. Nov 8, 2016 at 5:51
  • I just commented my observation. I have zero knowledge on the range stuff :(. How about using boost::any_iterator ?
    – Arunmu
    Nov 8, 2016 at 6:12
  • 1
    That's what I was trying to do and that's how I got to the type_erased adaptor. You can't iterate on one iterator, you need two of them, a begin and end. A range is a pair of iterators (begin and end). The type erasure adaptor (type_erased) should return a boost::any_range, which is a pair of boost::any_iterator. Nov 8, 2016 at 6:18

2 Answers 2

9

EDIT In fact there's more to this than meets the eye. There is another usability issue, which does address the problem. See OP's self-answer


You're falling into the number 1 pitfall with Boost Range v2 (and Boost Proto etc.).

nums | transformed(addOne) is a temporary. The type_erased adaptor stores a reference to that.

After assigning the type-erased adaptor to the resultN variable, the temporary is destructed.

What you have is a dangling reference :(

This is a highly unintuitive effect, and the number 1 reason why I limit the use of Range V2 in my codebase: I've been there all too often.

Here is a workaround:

auto tmp = nums | transformed(addOne);
auto result = tmp | type_erased<int, boost::forward_traversal_tag>();

-fsanitize=address,undefined confirms that the UB is gone when using the named temporary.

6
  • auto tmp = nums | transformed(addOne); auto result1 = tmp | type_erased<int, boost::forward_traversal_tag>(); This still outputs 1 1 1. Nov 8, 2016 at 16:19
  • What compiler, platform and boost version is that? (Is there a SSCCE to check for ... subtle differences)
    – sehe
    Nov 8, 2016 at 16:30
  • Setting the type_erased reference type to const fixed the issue (see my edit on the question). I am using Apple LLVM version 8.0.0 (clang-800.0.38), on Mac OS Sierra. Boost version 1.62. Nov 8, 2016 at 17:00
  • Wow. You can add this as an answer. Somehow I think I now half-remember @_jv finding this earlier and I completely forgot about it. Also don't know why anymore. Did you file an issue with the boost devs?
    – sehe
    Nov 8, 2016 at 17:03
  • @sehe clang 3.8 C++14 and C++1z, Boost 1.62.0 failed with this test
    – Danh
    Nov 8, 2016 at 17:05
2

Using

type_erased<int, boost::forward_traversal_tag, const int>()

works. The third template argument is the reference type, setting the reference to const prolongs the timespan of the temporary created by the transformed.

1
  • I have just verified that, indeed, this also silences ubsan/asan as well as valgrind on my system (where indeed using the temp also removed the symptoms). Not tried with clang though.
    – sehe
    Nov 8, 2016 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.