Conversation
Added a test function to verify receiving data into a string with spare capacity.
| // null bytes written by growing the string after the copy. | ||
|
|
||
| NTSCFG_TEST_EQ(remoteData.size(), CLIENT_DATA.size()); | ||
| NTSCFG_TEST_EQ(remoteData, CLIENT_DATA); |
There was a problem hiding this comment.
Fails in main:
684/1013 Test #684: ntc ntcd::MachineTest::verifyReceiveIntoString ......................................Subprocess aborted***Exception: 0.13 sec
[ 2026-09-18T20:58:51.514384Z ][ FATAL ][ 7F939D8A6780 ][ ntcd_machine.t.cpp:4433 ][ BSLS.LOG ]:
Assertion failed: remoteData == CLIENT_DATA
Found: (remoteData)
Expected: HELLOWORLD (CLIENT_DATA)
99% tests passed, 1 tests failed out of 684
Total Test time (real) = 67.36 sec
https://github.com/bloomberg/ntf-core/actions/runs/35394079728/job/105758779335?pr=417
Avoid data corruption by resizing a buffer before writing to it. Changed 'position' to a constant for clarity and safety. Removed redundant resize call on 'data'.
verifyReceiveIntoString test function| 0, | ||
| NTCCFG_WARNING_NARROW(int, numBytesToCopy)); | ||
|
|
||
| data->resize(position + numBytesToCopy); |
There was a problem hiding this comment.
The old code copied the bytes into the string first, then called resize() to make the string long enough. But growing a string fills the new space with zeros — so it immediately erased the bytes it had just copied. The caller got the right byte count but all zeros, and the packet had already been discarded, so the data was gone for good.
So the fix is to resize the string first, then copy into it.
|
|
||
| bsl::size_t numBytesReceivable = data->capacity() - data->size(); | ||
| if (numBytesReceivable == 0) { | ||
| data->resize(d_data.length()); |
There was a problem hiding this comment.
There was also a problem here. When the string had no spare room at all, the old code tried to make room with resize(d_data.length()) - but that might shrink the string rather than enlarging its buffer, so the copy ran off the end of the allocated memory.
For example, with
data->capacity() == data->size() == 4096
position == 4096
d_data.length() == 128
This leads to data->resize(128) and trying to write past the buffer at data-data() + 4096
bsl::stringinntcd::Packet::dequeueData