[LLVMbugs] [Bug 20174] New: Unsafe perfect forwarding

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Tue Jul 1 01:17:51 PDT 2014


            Bug ID: 20174
           Summary: Unsafe perfect forwarding
           Product: clang
           Version: 3.4
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: akim.demaille at gmail.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified


The following program is wrong, but it is accepted by clang.  It's an attempt
to reproduce at small scale a problem I have with clang++ on a much larger
program, but the fixed version of the following program unfortunately (for me)
does not show the failure I was trying to reproduce.

There are three classes.  The base class roughly represents some parametric
container that will be parameterized by a tuple<char, char>.  The middle class
aggregates an instance of the base class, and only forwards the calls to this
base class.

The derived class specializes the decorator by looking at the first item of the
tuple only.

This program is invalid, because the definition of word as
"std::tuple_element<Tape, Word>" is missing the "::type" bit.  Yet clang
accepts this program, and incorrectly calls a routine which requires a
tuple<char, char> with an effective argument which is just a char.

$ cat /tmp/bar.cc
#include <string>
#include <iostream>
#include <tuple>

template <typename Word>
struct base
  using word = std::tuple<char, char>;
  int out(int s)
    return s;

  int out(int s, word w)
    std::cerr << "Tuple: " << std::get<0>(w) << std::get<1>(w) << '\n';
    return s;

template <typename Aut>
struct decorator
  decorator(Aut a)
    : a_(a)

  Aut a_;

  template <typename... Args>
  out(Args&&... args)
    -> decltype(a_.out(args...))
    return a_.out(args...);

template <size_t Tape, typename Word>
struct derived : decorator<base<Word>>
  using super_t = decorator<base<Word>>;
  using word = std::tuple_element<Tape, Word>;
  using super_t::out;
  using super_t::super_t;

  int out(int s, word w)
    std::cerr << "char: " << w << '\n';
    return s;

int main()
  using word = std::tuple<char, char>;
  base<word> b;
  derived<0, word> d(b);
  d.out(12, 'a');

$ clang++-mp-3.5 --version
clang version 3.5.0 (trunk 210448)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
$ clang++-mp-3.5 -std=c++11 bar.cc
$ ./a.out
Tuple: a

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140701/2ad2af55/attachment.html>

More information about the llvm-bugs mailing list