ClickHouse / libhdfs3

HDFS file read access for ClickHouse
Apache License 2.0
36 stars 56 forks source link

A lot of undefined behaviors due to code pattern &vector[idx] #7

Closed amosbird closed 4 years ago

amosbird commented 4 years ago

This breaks libcxx debug build.

alexey-milovidov commented 4 years ago

Let's replace to vector.data() + idx. C++ permits to take an address of one element past the array bound.

amosbird commented 4 years ago

C++ permits to take an address of one element past the array bound.

Huh? I don't find that in the standard... It sounds to me that &vector[0] will always be valid, but it's not for the current libcxx implementation at least.

alexey-milovidov commented 4 years ago

Using vector subscript operator for vec[size] or even &vec[size] is illegal. Taking the address ptr + size or &ptr[size] (pointing to one element after the end of the allocated range) is legal.

The difference is that in first case, operator[] is called. The second case is pure pointer arithmetic. And C++ standard has the description of what addresses you can safely compare. It specifically includes one element after the end.

amosbird commented 4 years ago

https://github.com/ClickHouse-Extras/libhdfs3/pull/8