Skip to content

Commit c1843f2

Browse files
authored
Fix C++ client tablet bounds and session close semantics (#18005)
Add boundary checks for Tablet addTimestamp/addValue and validate ts_tablet_set_row_count to prevent rowSize from exceeding maxRowNumber. Align Session::close with Java by closing connections and routing all operations through getDefaultSessionConnection(). Map C API out_of_range to TS_ERR_INVALID_PARAM and add regression tests.
1 parent b727c66 commit c1843f2

8 files changed

Lines changed: 189 additions & 54 deletions

File tree

iotdb-client/client-cpp/src/include/Session.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,14 @@ class Tablet {
232232
}
233233

234234
void addTimestamp(size_t rowIndex, int64_t timestamp) {
235+
if (rowIndex >= static_cast<size_t>(maxRowNumber)) {
236+
char tmpStr[100];
237+
snprintf(tmpStr, sizeof(tmpStr),
238+
"Tablet::addTimestamp(), rowIndex >= maxRowNumber. rowIndex=%ld, "
239+
"maxRowNumber=%ld.",
240+
(long)rowIndex, (long)maxRowNumber);
241+
throw std::out_of_range(tmpStr);
242+
}
235243
timestamps[rowIndex] = timestamp;
236244
rowSize = max(rowSize, rowIndex + 1);
237245
}
@@ -248,10 +256,18 @@ class Tablet {
248256
throw std::out_of_range(tmpStr);
249257
}
250258

251-
if (rowIndex >= rowSize) {
259+
if (rowIndex >= static_cast<size_t>(maxRowNumber)) {
260+
char tmpStr[100];
261+
snprintf(tmpStr, sizeof(tmpStr),
262+
"Tablet::addValue(), rowIndex >= maxRowNumber. rowIndex=%ld, maxRowNumber=%ld.",
263+
(long)rowIndex, (long)maxRowNumber);
264+
throw std::out_of_range(tmpStr);
265+
}
266+
267+
if (rowIndex >= static_cast<size_t>(rowSize)) {
252268
char tmpStr[100];
253269
snprintf(tmpStr, sizeof(tmpStr),
254-
"Tablet::addValue(), rowIndex >= rowSize. rowIndex=%ld, rowSize.size()=%ld.",
270+
"Tablet::addValue(), rowIndex >= rowSize. rowIndex=%ld, rowSize=%ld.",
255271
(long)rowIndex, (long)rowSize);
256272
throw std::out_of_range(tmpStr);
257273
}

iotdb-client/client-cpp/src/rpc/SessionImpl.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ class Session::Impl {
6060
std::shared_ptr<INodesSupplier> nodesSupplier_;
6161
std::shared_ptr<SessionConnection> defaultSessionConnection_;
6262

63+
std::shared_ptr<SessionConnection> getDefaultSessionConnection() {
64+
if (isClosed_ || !defaultSessionConnection_) {
65+
throw IoTDBConnectionException("Session is not open, please invoke Session.open() first");
66+
}
67+
return defaultSessionConnection_;
68+
}
69+
6370
TEndPoint defaultEndPoint_;
6471

6572
struct TEndPointHash {
@@ -168,7 +175,7 @@ void Session::Impl::insertByGroup(
168175
if (endPointToSessionConnection.size() > 1) {
169176
removeBrokenSessionConnection(connection);
170177
try {
171-
insertConsumer(defaultSessionConnection_, req);
178+
insertConsumer(getDefaultSessionConnection(), req);
172179
} catch (const RedirectException&) {
173180
}
174181
} else {
@@ -216,7 +223,7 @@ void Session::Impl::insertOnce(
216223
if (endPointToSessionConnection.size() > 1) {
217224
removeBrokenSessionConnection(connection);
218225
try {
219-
insertConsumer(defaultSessionConnection_, req);
226+
insertConsumer(getDefaultSessionConnection(), req);
220227
} catch (const RedirectException&) {
221228
}
222229
} else {

0 commit comments

Comments
 (0)