This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ interop issue due to non-null pointers


In GCC 4.9, we have optimizations that make use of non-null annotations, at least for removing null pointer checks. Some libc functions are annotated with it, such as qsort, memcpy, memset, memcmp.

On the other hand, it is unspecified if the data() member of std::vector returns null pointer if empty() returns true.

As a result, code like this is invalid if the functions are ever called with empty vectors:

  void clear(std::vector<char> &vec)
  {
    memset(vec.data(), '\0', vec.size());
  }

  int comparefn(void *, void *);

  void sort(std::vector<T> &vec)
  {
    qsort(vec.data(), vec.size(), sizeof(T), comparefn);
  }

I think this is quite surprising.  What can we do about it?

--
Florian Weimer / Red Hat Product Security Team


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]