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

Commit f117250

Browse files
Merge pull request #1114 from igchor/ubsan_1.8
[stable-1.8] Ubsan fixes
2 parents 197dcda + 44bab51 commit f117250

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

include/libpmemobj++/container/vector.hpp

Lines changed: 24 additions & 13 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;
@@ -2362,7 +2365,8 @@ vector<T>::internal_insert(size_type idx, InputIt first, InputIt last)
23622365
* Private helper function. Must be called during transaction. Allocates new
23632366
* memory for capacity_new number of elements and copies or moves old elements
23642367
* to new memory area. If the current size is greater than capacity_new, the
2365-
* container is reduced to its first capacity_new elements.
2368+
* container is reduced to its first capacity_new elements. If was never
2369+
* allocated behaves as an alloc call.
23662370
*
23672371
* param[in] capacity_new new capacity.
23682372
*
@@ -2381,6 +2385,13 @@ vector<T>::realloc(size_type capacity_new)
23812385
{
23822386
assert(pmemobj_tx_stage() == TX_STAGE_WORK);
23832387

2388+
/*
2389+
* If _data == nullptr this object has never allocated any memory
2390+
* so we need to behave as alloc instead.
2391+
*/
2392+
if (_data == nullptr)
2393+
return alloc(capacity_new);
2394+
23842395
/*
23852396
* XXX: future optimization: we don't have to snapshot data
23862397
* which we will not overwrite
@@ -2389,7 +2400,7 @@ vector<T>::realloc(size_type capacity_new)
23892400

23902401
auto old_data = _data;
23912402
auto old_size = _size;
2392-
pointer old_begin = &_data[0];
2403+
pointer old_begin = _data.get();
23932404
pointer old_end = capacity_new < _size
23942405
? &_data[static_cast<difference_type>(capacity_new)]
23952406
: &_data[static_cast<difference_type>(size())];

0 commit comments

Comments
 (0)