Commit 39dde7eb authored by Kirill Smelkov's avatar Kirill Smelkov

*: Replace usage of &vector[0] with vector.data()

We use &vector[0] in several places to get the pointer to vector data,
but std::vector[i] states invariant that i must be < vector.size(). If
the vector is not empty - then &vector[0] works ok. However if the
vector is empty - then the invariant is broken.

Normally it should not be a problem, since with empty vector we usually
use (&vector[0], vector.size()) pair, and for empty vector its size is
zero. In other words even if &vector[0] returns some not good pointer
with zero size that data should never be accessed. But a build of
libstdc++ could have asserts turned on and verify that i is actually <
size. For example with default install on Fedora the following program
with plain select()

    from golang import select
    select()

crashes with assert triggered on the breakage of that invariant

    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib64/libthread_db.so.1".
    /usr/include/c++/10/bits/stl_vector.h:1042: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = golang::_selcase; _Alloc = std::allocator<golang::_selcase>; std::vector<_Tp, _Alloc>::reference = golang::_selcase&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]: Assertion '__builtin_expect(__n < this->size(), true)' failed.

    Program received signal SIGABRT, Aborted.
    0x00007ffff7e22a25 in raise () from /lib64/libc.so.6
    (gdb) bt
    #0  0x00007ffff7e22a25 in raise () from /lib64/libc.so.6
    #1  0x00007ffff7e0b895 in abort () from /lib64/libc.so.6
    #2  0x00007fffea638d6d in std::__replacement_assert (__condition=0x7fffea63ea88 "__builtin_expect(__n < this->size(), true)",
        __function=0x7fffea63eab8 "std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = golang::_selcase; _Alloc = std::allocator<golang::_selcase>; std::vector<_Tp, "..., __line=1042, __file=0x7fffea63ebe8 "/usr/include/c++/10/bits/stl_vector.h")
        at /usr/include/c++/10/x86_64-redhat-linux/bits/c++config.h:2552
    #3  std::vector<golang::_selcase, std::allocator<golang::_selcase> >::operator[] (this=<synthetic pointer>, __n=0)
        at /usr/include/c++/10/bits/stl_vector.h:1042
    #4  std::vector<golang::_selcase, std::allocator<golang::_selcase> >::operator[] (__n=0, this=<synthetic pointer>)
        at /usr/include/c++/10/bits/stl_vector.h:1040
    #5  __pyx_pf_6golang_7_golang_6pyselect (__pyx_self=<optimized out>, __pyx_v_pycasev=<optimized out>) at golang/_golang.cpp:7930
    #6  __pyx_pw_6golang_7_golang_7pyselect (__pyx_self=<optimized out>, __pyx_args=<optimized out>, __pyx_kwds=<optimized out>) at golang/_golang.cpp:7284
    #7  0x00007ffff7bceeab in cfunction_call_varargs (func=<built-in function pyselect>, args=(), kwargs=0x0)
        at /usr/src/debug/python3-3.8.10-1.fc32.x86_64/Objects/call.c:743
    ...

-> Fix it all via replacing &vector[0] with vector.data() everywhere.

No need to add new test since select() is used by test_blockforever()
and running that test on Fedora was crashing as well.

/reviewed-by @jerome
/reviewed-at nexedi/pygolang!22
parent ffb40903
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# distutils: language = c++ # distutils: language = c++
# distutils: depends = libgolang.h os/signal.h _golang_str.pyx # distutils: depends = libgolang.h os/signal.h _golang_str.pyx
# #
# Copyright (C) 2018-2022 Nexedi SA and Contributors. # Copyright (C) 2018-2023 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com> # Kirill Smelkov <kirr@nexedi.com>
# #
# This program is free software: you can Use, Study, Modify and Redistribute # This program is free software: you can Use, Study, Modify and Redistribute
...@@ -469,7 +469,7 @@ def pyselect(*pycasev): ...@@ -469,7 +469,7 @@ def pyselect(*pycasev):
casev[i].user = pych.dtype casev[i].user = pych.dtype
with nogil: with nogil:
selected = _chanselect_pyexc(&casev[0], casev.size()) selected = _chanselect_pyexc(casev.data(), casev.size())
finally: finally:
# decref not sent tx (see ^^^ send prepare) # decref not sent tx (see ^^^ send prepare)
......
#ifndef _NXD_LIBGOLANG_H #ifndef _NXD_LIBGOLANG_H
#define _NXD_LIBGOLANG_H #define _NXD_LIBGOLANG_H
// Copyright (C) 2018-2022 Nexedi SA and Contributors. // Copyright (C) 2018-2023 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -577,7 +577,7 @@ int select(const _selcase (&casev)[N]) { ...@@ -577,7 +577,7 @@ int select(const _selcase (&casev)[N]) {
static inline // select(vector<casev>) static inline // select(vector<casev>)
int select(const std::vector<_selcase> &casev) { int select(const std::vector<_selcase> &casev) {
return _chanselect(&casev[0], casev.size()); return _chanselect(casev.data(), casev.size());
} }
// defer(f) mimics `defer f()` from golang. // defer(f) mimics `defer f()` from golang.
......
// Copyright (C) 2019-2022 Nexedi SA and Contributors. // Copyright (C) 2019-2023 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -226,8 +226,8 @@ tuple<string, error> ReadFile(const string& path) { ...@@ -226,8 +226,8 @@ tuple<string, error> ReadFile(const string& path) {
while (1) { while (1) {
int n; int n;
tie(n, err) = f->Read(&buf[0], buf.size()); tie(n, err) = f->Read(buf.data(), buf.size());
data.append(&buf[0], n); data.append(buf.data(), n);
if (err != nil) { if (err != nil) {
if (err == io::EOF_) if (err == io::EOF_)
err = nil; err = nil;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment