-
Notifications
You must be signed in to change notification settings - Fork 509
Description
I have a "generic" (in terms of queries it accepts) function that does something like
// std::string query = "UPDATE ... (without returning anything)" in this particular case
soci::row row;
soci::statement st = (sql.prepare << query, soci::into(row));
st.execute(false);
while (st.fetch()) {
// do something with the row - the code is "generic" and should work with queries that return data too
}in this case, I get a lot of iterations in the while loop which I think are due to multiple variables in the PSQL statement backend being in an undefined state. I stepped through the execution in a debugger and found that:
- during
st.execute(false),process_resultis false here:
soci/src/backends/postgresql/statement.cpp
Lines 521 to 526 in b2e4cf3
if (process_result) { currentRow_ = 0; rowsToConsume_ = 0; numberOfRows_ = PQntuples(result_);
which causes it to immediately jump to
soci/src/backends/postgresql/statement.cpp
Lines 545 to 548 in b2e4cf3
else { return ef_no_data; }
- which makes sense since no data is indeed produced by an update query. It is, however, skipping the initialization ofcurrentRow_,rowsToConsume_andnumberOfRows_ - then, backend calls from
st.fetch()go into
soci/src/backends/postgresql/statement.cpp
Line 552 in b2e4cf3
postgresql_statement_backend::fetch(int number)
which uses these uninitialized members in its body, e.g.
soci/src/backends/postgresql/statement.cpp
Lines 568 to 570 in b2e4cf3
currentRow_ += rowsToConsume_; if (currentRow_ >= numberOfRows_)
Documentation does not suggest the return value of execute() to be checked before following it up with fetch(), so I suppose it's not intended behavior and fetch() in my case should've returned false immediately?