Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 44bab51

Browse files
committed
vector: fix referencing empty array
This fixes following tests under UBSAN: vector_modifiers_type_requirements_0_none vector_parameters_0_none They failed with following error message: "runtime error: reference binding to null pointer of type" This fix is continuation of 41ddc88
1 parent 41ddc88 commit 44bab51

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

include/libpmemobj++/container/vector.hpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019, Intel Corporation
2+
* Copyright 2018-2021, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -668,7 +668,7 @@ vector<T>::assign(size_type count, const_reference value)
668668
add_data_to_tx(0, size_old);
669669

670670
std::fill_n(
671-
&_data[0],
671+
_data.get(),
672672
(std::min)(count,
673673
static_cast<size_type>(size_old)),
674674
value);
@@ -1600,7 +1600,7 @@ vector<T>::insert(const_iterator pos, size_type count, const value_type &value)
16001600
single_element_iterator<value_type>(&value, count));
16011601
});
16021602

1603-
return iterator(&_data[static_cast<difference_type>(idx)]);
1603+
return iterator(_data.get() + static_cast<difference_type>(idx));
16041604
}
16051605

16061606
/**
@@ -1843,7 +1843,7 @@ typename vector<T>::iterator
18431843
vector<T>::erase(const_iterator first, const_iterator last)
18441844
{
18451845
size_type idx = static_cast<size_type>(
1846-
std::distance(const_iterator(&_data[0]), first));
1846+
std::distance(const_iterator(_data.get()), first));
18471847
size_type count = static_cast<size_type>(std::distance(first, last));
18481848

18491849
if (count == 0)
@@ -2306,10 +2306,11 @@ vector<T>::internal_insert(size_type idx, InputIt first, InputIt last)
23062306
auto count = static_cast<size_type>(std::distance(first, last));
23072307

23082308
if (_capacity >= size() + count) {
2309-
pointer dest =
2310-
&_data[static_cast<difference_type>(size() + count)];
2311-
pointer begin = &_data[static_cast<difference_type>(idx)];
2312-
pointer end = &_data[static_cast<difference_type>(size())];
2309+
pointer dest = _data.get() +
2310+
static_cast<difference_type>(size() + count);
2311+
pointer begin = _data.get() + static_cast<difference_type>(idx);
2312+
pointer end =
2313+
_data.get() + static_cast<difference_type>(size());
23132314

23142315
add_data_to_tx(idx, size() - idx + count);
23152316

@@ -2327,9 +2328,11 @@ vector<T>::internal_insert(size_type idx, InputIt first, InputIt last)
23272328

23282329
auto old_data = _data;
23292330
auto old_size = _size;
2330-
pointer old_begin = &_data[0];
2331-
pointer old_mid = &_data[static_cast<difference_type>(idx)];
2332-
pointer old_end = &_data[static_cast<difference_type>(size())];
2331+
pointer old_begin = _data.get();
2332+
pointer old_mid =
2333+
_data.get() + static_cast<difference_type>(idx);
2334+
pointer old_end =
2335+
_data.get() + static_cast<difference_type>(size());
23332336

23342337
_data = nullptr;
23352338
_size = _capacity = 0;
@@ -2397,7 +2400,7 @@ vector<T>::realloc(size_type capacity_new)
23972400

23982401
auto old_data = _data;
23992402
auto old_size = _size;
2400-
pointer old_begin = &_data[0];
2403+
pointer old_begin = _data.get();
24012404
pointer old_end = capacity_new < _size
24022405
? &_data[static_cast<difference_type>(capacity_new)]
24032406
: &_data[static_cast<difference_type>(size())];

0 commit comments

Comments
 (0)