This is an automated email from the ASF dual-hosted git repository. kxiao pushed a commit to branch clucene in repository https://gitbox.apache.org/repos/asf/doris-thirdparty.git
The following commit(s) were added to refs/heads/clucene by this push: new 5e9566ab364 fix CLStream memcpy read 1 extra byte (#240) 5e9566ab364 is described below commit 5e9566ab364d71b64c436ee46e5c848eed0ab7f7 Author: Kang <kxiao.ti...@gmail.com> AuthorDate: Mon Sep 16 14:44:01 2024 +0800 fix CLStream memcpy read 1 extra byte (#240) * fix CLStream memcpy read 1 extra byte * fix comment and add unit test --- src/core/CLucene/util/CLStreams.h | 16 +++++++- src/test/CMakeLists.txt | 1 + src/test/test.h | 1 + src/test/tests.cpp | 1 + src/test/util/TestStringReader.cpp | 83 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/core/CLucene/util/CLStreams.h b/src/core/CLucene/util/CLStreams.h index 3f60f2d97aa..5fd08531a00 100644 --- a/src/core/CLucene/util/CLStreams.h +++ b/src/core/CLucene/util/CLStreams.h @@ -195,6 +195,8 @@ public: this->buffer_size = 0; this->init(_value, _length, copyData); } + + // _value should be type T* void init(const void *_value, int32_t _length, bool copyData = true) override { const size_t length = (size_t)_length; this->pos = 0; @@ -207,7 +209,10 @@ public: tmp = (T *) realloc(tmp, sizeof(T) * (length + 1)); this->buffer_size = length; } - memcpy(tmp, _value, length + 1); + // copy data + memcpy(tmp, _value, length * sizeof(T)); + // add trailing zero + tmp[length] = 0; this->value = tmp; } else { if (ownValue && this->value != NULL) { @@ -227,6 +232,15 @@ public: } } + // for test only + int testValueAt(const size_t i) { + if (i <= this->m_size) { + return this->value[i]; + } else { + return -1; + } + } + int32_t read(const void **start, int32_t min, int32_t max) override { if (m_size == pos) return -1; diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 88c7c229dd9..fa8e4d3db03 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -101,6 +101,7 @@ SET(test_files ./tests.cpp ./util/TestBKD.cpp ./util/TestMSBRadixSorter.cpp ./util/TestStringBuffer.cpp + ./util/TestStringReader.cpp ./util/English.cpp ./util/TestStrConvert.cpp ./query/TestMultiPhraseQuery.cpp diff --git a/src/test/test.h b/src/test/test.h index cb929538825..39959327f63 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -84,6 +84,7 @@ CuSuite *testStrConvert(void); CuSuite *testSearchRange(void); CuSuite *testMultiPhraseQuery(void); CuSuite *testIndexCompaction(void); +CuSuite *testStringReader(void); #ifdef TEST_CONTRIB_LIBS //CuSuite *testGermanAnalyzer(void); diff --git a/src/test/tests.cpp b/src/test/tests.cpp index 3a193a3c382..5708ff62986 100644 --- a/src/test/tests.cpp +++ b/src/test/tests.cpp @@ -18,6 +18,7 @@ unittest tests[] = { {"searchRange", testSearchRange}, {"MultiPhraseQuery", testMultiPhraseQuery}, {"IndexCompaction", testIndexCompaction}, + {"testStringReader", testStringReader}, #ifdef TEST_CONTRIB_LIBS {"chinese", testchinese}, #endif diff --git a/src/test/util/TestStringReader.cpp b/src/test/util/TestStringReader.cpp new file mode 100644 index 00000000000..0fbc85eefb7 --- /dev/null +++ b/src/test/util/TestStringReader.cpp @@ -0,0 +1,83 @@ +/*------------------------------------------------------------------------------ +* Copyright (C) 2003-2010 Ben van Klinken and the CLucene Team +* +* Distributable under the terms of either the Apache License (Version 2.0) or +* the GNU Lesser General Public License, as specified in the COPYING file. +------------------------------------------------------------------------------*/ + +#include "test.h" +#include "CLucene/util/CLStreams.h" +#include <stdexcept> + +CL_NS_USE(util) + +void testSStringReaderInit(CuTest *tc) { + // test for char + // test default constructor and internal status + SStringReader<char> r11; + CuAssertEquals(tc, 0, r11.size()); + CuAssertEquals(tc, 0, r11.position()); + + char chars[5] = {'t', 'e', 's', 't', 'x'}; + + // test constructor without copy and internal status + SStringReader<char> r12 {chars, 4, false}; + CuAssertEquals(tc, 4, r12.size()); + CuAssertEquals(tc, 0, r12.position()); + CuAssertEquals(tc, 't', r12.testValueAt(0)); + CuAssertEquals(tc, 'e', r12.testValueAt(1)); + CuAssertEquals(tc, 's', r12.testValueAt(2)); + CuAssertEquals(tc, 't', r12.testValueAt(3)); + // it is 'x' as original chars since not copying + CuAssertEquals(tc, 'x', r12.testValueAt(4)); + + // test constructor with copy and internal status + SStringReader<char> r13 {chars, 4, true}; + CuAssertEquals(tc, 4, r13.size()); + CuAssertEquals(tc, 0, r13.position()); + CuAssertEquals(tc, 't', r13.testValueAt(0)); + CuAssertEquals(tc, 'e', r13.testValueAt(1)); + CuAssertEquals(tc, 's', r13.testValueAt(2)); + CuAssertEquals(tc, 't', r13.testValueAt(3)); + // it is 0 since only copying 4 chars and add 0 + CuAssertEquals(tc, 0, r13.testValueAt(4)); + + + // test for wchar + // test default constructor and internal status + SStringReader<wchar_t> r21; + CuAssertEquals(tc, 0, r21.size()); + CuAssertEquals(tc, 0, r21.position()); + + wchar_t wchars[5] = {'t', 'e', 's', 't', 'x'}; + + // test constructor without copy and internal status + SStringReader<wchar_t> r22 {wchars, 4, false}; + CuAssertEquals(tc, 4, r22.size()); + CuAssertEquals(tc, 0, r22.position()); + CuAssertEquals(tc, 't', r22.testValueAt(0)); + CuAssertEquals(tc, 'e', r22.testValueAt(1)); + CuAssertEquals(tc, 's', r22.testValueAt(2)); + CuAssertEquals(tc, 't', r22.testValueAt(3)); + // it is 'x' as original chars since not copying + CuAssertEquals(tc, 'x', r22.testValueAt(4)); + + // test constructor with copy and internal status + SStringReader<wchar_t> r23 {wchars, 4, true}; + CuAssertEquals(tc, 4, r23.size()); + CuAssertEquals(tc, 0, r23.position()); + CuAssertEquals(tc, 't', r23.testValueAt(0)); + CuAssertEquals(tc, 'e', r23.testValueAt(1)); + CuAssertEquals(tc, 's', r23.testValueAt(2)); + CuAssertEquals(tc, 't', r23.testValueAt(3)); + // it is 0 since only copying 4 chars and add 0 + CuAssertEquals(tc, 0, r23.testValueAt(4)); +} + +CuSuite *testStringReader(void) { + CuSuite *suite = CuSuiteNew(_T("CLucene SStringReader Test")); + + SUITE_ADD_TEST(suite, testSStringReaderInit); + + return suite; +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org