Skip to content

Commit 7fdaf29

Browse files
committed
- CS104_Connection: fixed possible application crash when application is trying to send a message after connection is closed (LIB8705-127)
1 parent 7e061ee commit 7fdaf29

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

lib60870-C/src/iec60870/cs104/cs104_connection.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ writeToSocket(CS104_Connection self, uint8_t* buf, int size)
166166
if (self->rawMessageHandler)
167167
self->rawMessageHandler(self->rawMessageHandlerParameter, buf, size, true);
168168

169+
if (self->socket == NULL || self->conState == STATE_IDLE)
170+
return 0;
171+
169172
#if (CONFIG_CS104_SUPPORT_TLS == 1)
170173
if (self->tlsSocket)
171174
return TLSSocket_write(self->tlsSocket, buf, size);

lib60870-C/tests/all_tests.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6577,6 +6577,40 @@ test_ScaledNormalizedConversion()
65776577
TEST_ASSERT_EQUAL_FLOAT(0.0f, NormalizedValue_fromScaled(0));
65786578
}
65796579

6580+
void
6581+
test_CS104Connection_cannotWriteToSocketWhenNotConnected()
6582+
{
6583+
/* test that the client does not crash when it tries to send a message after the connection is closed */
6584+
6585+
CS104_Slave slave = CS104_Slave_create(10, 10);
6586+
6587+
CS104_Slave_setServerMode(slave, CS104_MODE_SINGLE_REDUNDANCY_GROUP);
6588+
CS104_Slave_setLocalPort(slave, 20004);
6589+
6590+
CS104_Slave_start(slave);
6591+
6592+
CS101_AppLayerParameters alParams = CS104_Slave_getAppLayerParameters(slave);
6593+
6594+
CS104_Connection con = CS104_Connection_create("127.0.0.1", 20004);
6595+
6596+
bool result = CS104_Connection_connect(con);
6597+
TEST_ASSERT_TRUE(result);
6598+
6599+
CS104_Connection_sendStartDT(con);
6600+
6601+
CS104_Slave_stop(slave);
6602+
6603+
Thread_sleep(500);
6604+
6605+
CS104_Connection_sendStopDT(con);
6606+
6607+
CS104_Connection_close(con);
6608+
6609+
CS104_Connection_destroy(con);
6610+
6611+
CS104_Slave_destroy(slave);
6612+
}
6613+
65806614
int
65816615
main(int argc, char** argv)
65826616
{
@@ -6703,5 +6737,7 @@ main(int argc, char** argv)
67036737

67046738
RUN_TEST(test_ScaledNormalizedConversion);
67056739

6740+
RUN_TEST(test_CS104Connection_cannotWriteToSocketWhenNotConnected);
6741+
67066742
return UNITY_END();
67076743
}

0 commit comments

Comments
 (0)