Skip to content

Commit f4ff01c

Browse files
authored
IGNITE-23598 Fix LengthPrefixCodec issue with 3 byte buffer (#11777)
1 parent 483ea0e commit f4ff01c

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

modules/platforms/cpp/network/include/ignite/network/data_buffer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ namespace ignite
4848
*
4949
* @param data Data.
5050
* @param pos Start of data.
51-
* @param len Length.
51+
* @param end End of the data.
5252
*/
53-
DataBuffer(const impl::interop::SP_ConstInteropMemory& data, int32_t pos, int32_t len);
53+
DataBuffer(const impl::interop::SP_ConstInteropMemory& data, int32_t pos, int32_t end);
5454

5555
/**
5656
* Destructor.
@@ -127,8 +127,8 @@ namespace ignite
127127
/** Position in current data. */
128128
int32_t position;
129129

130-
/** Data length. */
131-
int32_t length;
130+
/** End position. */
131+
int32_t endPos;
132132

133133
/** Data */
134134
impl::interop::SP_ConstInteropMemory data;

modules/platforms/cpp/network/src/network/data_buffer.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ namespace ignite
2626
{
2727
DataBuffer::DataBuffer() :
2828
position(0),
29-
length(0),
29+
endPos(0),
3030
data()
3131
{
3232
// No-op.
3333
}
3434

3535
DataBuffer::DataBuffer(const impl::interop::SP_ConstInteropMemory& data0) :
3636
position(0),
37-
length(data0.Get()->Length()),
37+
endPos(data0.Get()->Length()),
3838
data(data0)
3939
{
4040
// No-op.
4141
}
4242

43-
DataBuffer::DataBuffer(const impl::interop::SP_ConstInteropMemory& data0, int32_t pos, int32_t len) :
43+
DataBuffer::DataBuffer(const impl::interop::SP_ConstInteropMemory& data0, int32_t pos, int32_t end) :
4444
position(pos),
45-
length(len),
45+
endPos(end),
4646
data(data0)
4747
{
4848
// No-op.
@@ -75,7 +75,7 @@ namespace ignite
7575
if (!data.IsValid())
7676
return 0;
7777

78-
return length - position;
78+
return endPos - position;
7979
}
8080

8181
bool DataBuffer::IsEmpty() const
@@ -98,7 +98,7 @@ namespace ignite
9898

9999
impl::interop::InteropInputStream DataBuffer::GetInputStream() const
100100
{
101-
impl::interop::InteropInputStream stream = impl::interop::InteropInputStream(data.Get(), length);
101+
impl::interop::InteropInputStream stream = impl::interop::InteropInputStream(data.Get(), endPos);
102102
stream.Position(position);
103103

104104
return stream;
@@ -109,11 +109,11 @@ namespace ignite
109109
if (IsEmpty())
110110
return DataBuffer();
111111

112-
impl::interop::SP_InteropMemory mem(new impl::interop::InteropUnpooledMemory(length));
113-
mem.Get()->Length(length);
114-
std::memcpy(mem.Get()->Data(), data.Get()->Data() + position, length);
112+
impl::interop::SP_InteropMemory mem(new impl::interop::InteropUnpooledMemory(endPos));
113+
mem.Get()->Length(endPos);
114+
std::memcpy(mem.Get()->Data(), data.Get()->Data() + position, endPos);
115115

116-
return DataBuffer(mem, 0, length);
116+
return DataBuffer(mem, 0, endPos);
117117
}
118118

119119
void DataBuffer::Skip(int32_t bytes)

modules/platforms/cpp/network/src/network/length_prefix_codec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace ignite
4545

4646
DataBuffer LengthPrefixCodec::Decode(DataBuffer& data)
4747
{
48-
if (packet.IsValid() && packet.Get()->Length() == (PACKET_HEADER_SIZE + packetSize))
48+
if (packet.IsValid() && packetSize != -1 && packet.Get()->Length() == (PACKET_HEADER_SIZE + packetSize))
4949
{
5050
packetSize = -1;
5151
packet.Get()->Length(0);

modules/platforms/cpp/thin-client-test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ set(SOURCES
3838
src/test_utils.cpp
3939
src/ignite_client_test.cpp
4040
src/interop_test.cpp
41+
src/network_codec_test.cpp
4142
src/scan_query_test.cpp
4243
src/sql_fields_query_test.cpp
4344
src/auth_test.cpp
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <boost/test/unit_test.hpp>
19+
20+
#include <ignite/impl/interop/interop.h>
21+
#include <ignite/network/length_prefix_codec.h>
22+
23+
using namespace boost::unit_test;
24+
using namespace ignite::network;
25+
26+
27+
BOOST_AUTO_TEST_SUITE(NetworkCodecTestSuite)
28+
29+
BOOST_AUTO_TEST_CASE(LengthPrefixCodec_3bytes)
30+
{
31+
ignite::impl::interop::SP_InteropMemory mem(new ignite::impl::interop::InteropUnpooledMemory(8));
32+
ignite::impl::interop::InteropOutputStream stream(mem.Get());
33+
stream.WriteInt32(4);
34+
stream.WriteInt32(123456789);
35+
36+
DataBuffer in1(mem, 0, 3);
37+
DataBuffer in2(mem, 3, 8);
38+
39+
SP_Codec codec(new LengthPrefixCodec());
40+
41+
DataBuffer out1 = codec.Get()->Decode(in1);
42+
BOOST_CHECK(out1.IsEmpty());
43+
44+
DataBuffer out2 = codec.Get()->Decode(in2);
45+
BOOST_CHECK(!out2.IsEmpty());
46+
BOOST_CHECK_EQUAL(out2.GetSize(), 8);
47+
}
48+
49+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)