I’m currently writing a test that uses a RPC framework based on RDMA, the github repository is here:https://github.com/erpc-io/eRPC.git
And something strange occurred, segmentation fault happened when the function access a inner data structure of this framework.
I used GDB to see the trace, and it turned out that a size()
function that access a size_t
type variable free_index_
, and that’s where the segfault happened.
I think the implement of my test doesn’t matter a lot in this question, I just wonder why a size_t
can cause segfault.
Here’s the source code in this RPC framework(called eRPC), the free_index_
variable declaration is in the class FixedVector
:
template <typename T, size_t N>
class FixedVector {
public:
FixedVector() {}
~FixedVector() {}
inline void push_back(T t) {
assert(free_index_ < N); // p free_index can be up to N - 1
arr_[free_index_] = t;
free_index_++;
}
inline T pop_back() {
assert(free_index_ > 0); // If free_index is 0, there is nothing to pop
T t = arr_[free_index_ - 1]; // The slot at free_index - 1 is occupied
free_index_--;
return t;
}
/// Return the number of elements currently in the vector
// Here!!!!!!!!!!!!
inline size_t size() {
return free_index_;
}
/// Return the maximum capacity of the FixedVector
inline size_t capacity() { return N; }
inline T operator[](size_t i) {
assert(i < free_index_); // There is no element at p free_index
return arr_[i];
}
T arr_[N]; // N is the capacity of the vector
size_t free_index_ = 0; // Index of the first free slot
};
} // namespace erpc
In my understanding, only array out-of-bounds access or accessing the properties of a null pointer would cause a segmentation fault. And I have checked that the capacity()
of the accessed object is normal, but I can’t even output its free_index_
variable, which causes segfault.
Can anyone tell me why? Thanks!!!
Or do I provide enough information for the question?