From 7f3b333815cf1d515e7d77a1561d83890182d2a4 Mon Sep 17 00:00:00 2001 From: MarkLee131 Date: Tue, 7 Apr 2026 00:39:16 +0800 Subject: [PATCH] fix GetColumnNames() undefined behavior on empty label row GetColumnNames() did .begin() + (mRowNameIdx + 1) on the label row without checking that the row has enough elements. When the label row is empty (e.g. after InsertRow on an empty document with LabelParams(0,0)), .begin() wraps a null pointer and adding to it is undefined behavior. Add a bounds check before the iterator arithmetic so it returns an empty vector instead of hitting UB. --- src/rapidcsv.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/rapidcsv.h b/src/rapidcsv.h index bc83e0e..53e1f58 100644 --- a/src/rapidcsv.h +++ b/src/rapidcsv.h @@ -1473,9 +1473,13 @@ namespace rapidcsv { if (mLabelParams.mColumnNameIdx >= 0) { - return std::vector(mData.at(static_cast(mLabelParams.mColumnNameIdx)).begin() + - (mLabelParams.mRowNameIdx + 1), - mData.at(static_cast(mLabelParams.mColumnNameIdx)).end()); + const auto& labelRow = mData.at(static_cast(mLabelParams.mColumnNameIdx)); + const size_t offset = static_cast(mLabelParams.mRowNameIdx + 1); + if (offset <= labelRow.size()) + { + return std::vector(labelRow.begin() + static_cast(offset), + labelRow.end()); + } } return std::vector();