Skip to content

Commit a7632ca

Browse files
committed
fix: guard flatten_il against empty ranges to silence gcc -Wnonnull
gcc-13/-14 inlines std::copy on a 0-length range to __builtin_memmove(out, in, 0) and trips -Wnonnull when both pointers are null (e.g. std::array<T, 0>::begin()), which is what the empty_vector unit test exercises. The std::copy itself is a well- defined no-op on empty ranges, but the optimizer can't see past the inlined memmove's nonnull attribute. Short-circuit empty ranges so std::copy is not called at all in that case.
1 parent 11dc950 commit a7632ca

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/TiledArray/util/initializer_list.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,15 @@ auto flatten_il(T&& il, OutputItr out_itr) {
212212
++out_itr;
213213
}
214214
// We were given a vector or we have recursed to the most nested
215-
// initializer_list, either way copy the contents to the buffer
215+
// initializer_list, either way copy the contents to the buffer.
216+
// Guard against empty ranges: std::copy on a 0-length range with a
217+
// possibly-null output iterator (e.g. std::array<T,0>::begin()) inlines
218+
// to __builtin_memmove(null, null, 0), tripping gcc's -Wnonnull even
219+
// though the copy itself is a no-op.
216220
else if constexpr (ranks_left == 1) {
217-
out_itr = std::copy(il.begin(), il.end(), out_itr);
221+
if (il.size() != 0) {
222+
out_itr = std::copy(il.begin(), il.end(), out_itr);
223+
}
218224
}
219225
// The initializer list is at least a matrix, so recurse over sub-lists
220226
else {

0 commit comments

Comments
 (0)