Skip to content

Commit aded64e

Browse files
committed
client: fix assertion triggered on data decoding failure
After decoding response data we check if the decoder advanced the iterator to the end of the raw data. However, that's not true when someone tries to decode data with non-matching format. The commit fixes the assertion - we should check iterator only if data was successfully decoded. Closes #99
1 parent b582e57 commit aded64e

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Client/ResponseReader.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ struct Data {
106106
{
107107
it_t itr = iters.first;
108108
bool ok = mpp::decode(itr, tuples);
109-
assert(itr == iters.second);
109+
assert(!ok || itr == iters.second);
110110
return ok;
111111
}
112112

test/ClientTest.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,42 @@ test_dead_connection_wait(Connector<BUFFER, NetProvider> &client)
10931093
#endif
10941094
}
10951095

1096+
/**
1097+
* Test for miscellaneous issues related to response decoding.
1098+
*/
1099+
template <class BUFFER, class NetProvider>
1100+
void
1101+
response_decoding(Connector<BUFFER, NetProvider> &client)
1102+
{
1103+
TEST_INIT(0);
1104+
1105+
Connection<Buf_t, NetProvider> conn(client);
1106+
int rc = test_connect(client, conn, localhost, port);
1107+
fail_unless(rc == 0);
1108+
1109+
TEST_CASE("decode data with non-matching format");
1110+
rid_t f = conn.call("remote_uint", std::make_tuple());
1111+
client.wait(conn, f, WAIT_TIMEOUT);
1112+
fail_unless(conn.futureIsReady(f));
1113+
std::optional<Response<Buf_t>> response = conn.getResponse(f);
1114+
1115+
fail_unless(response.has_value());
1116+
fail_unless(response->body.data.has_value());
1117+
1118+
std::string str;
1119+
unsigned num;
1120+
std::tuple<std::string> arr_of_str;
1121+
std::tuple<unsigned> arr_of_num;
1122+
/* Try to decode data with non-matching format. */
1123+
fail_if(response->body.data->decode(str));
1124+
fail_if(response->body.data->decode(num));
1125+
fail_if(response->body.data->decode(arr_of_str));
1126+
/* We should successfully decode data after all. */
1127+
fail_unless(response->body.data->decode(arr_of_num));
1128+
1129+
client.close(conn);
1130+
}
1131+
10961132
int main()
10971133
{
10981134
#ifdef TNTCXX_ENABLE_SSL
@@ -1141,5 +1177,6 @@ int main()
11411177
::test_sigpipe(client);
11421178
#endif
11431179
::test_dead_connection_wait(client);
1180+
response_decoding(client);
11441181
return 0;
11451182
}

0 commit comments

Comments
 (0)