diff --git a/cpp/.clang-tidy b/cpp/.clang-tidy new file mode 100644 index 000000000..b118351b9 --- /dev/null +++ b/cpp/.clang-tidy @@ -0,0 +1,49 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# clang-tidy configuration for Apache TsFile C++. +# Scope is tsfile's own sources (cpp/src); third_party is excluded by the +# header filter below and by the run script's file regex. +# +# Enabled: clang-analyzer (real bug hunting), bugprone, performance. +# Disabled noisy groups: +# -bugprone-assignment-in-if-condition RET_FAIL/IS_SUCC macro idiom (717 hits) +# -bugprone-branch-clone switch tables share bodies (127 hits) +# -bugprone-macro-parentheses project macro style (59 hits) +# -bugprone-multi-level-implicit-pointer-conversion cwrapper void* pattern (11 hits) +# -performance-enum-size enum base types are on-disk ABI +# NOTE: no comment lines inside the folded scalar below; they would be +# parsed as part of the checks list. +Checks: > + -*, + clang-analyzer-*, + bugprone-*, + performance-*, + -bugprone-easily-swappable-parameters, + -bugprone-narrowing-conversions, + -bugprone-implicit-widening-of-multiplication-result, + -bugprone-reserved-identifier, + -bugprone-unhandled-self-assignment, + -performance-inefficient-string-concatenation, + -bugprone-assignment-in-if-condition, + -bugprone-branch-clone, + -bugprone-macro-parentheses, + -bugprone-multi-level-implicit-pointer-conversion, + -performance-enum-size +WarningsAsErrors: '' +HeaderFilterRegex: '^.*/cpp/(src|test)/.*\.h$' +FormatStyle: none diff --git a/cpp/src/common/allocator/byte_stream.h b/cpp/src/common/allocator/byte_stream.h index b403d3b5c..9caac227b 100644 --- a/cpp/src/common/allocator/byte_stream.h +++ b/cpp/src/common/allocator/byte_stream.h @@ -420,7 +420,7 @@ class ByteStream { uint32_t write_len = 0; while (write_len < len) { if (RET_FAIL(prepare_space())) { - std::cout << "write_buf error " << ret << std::endl; + std::cout << "write_buf error " << ret << '\n'; return ret; } uint32_t remainder = page_size_ - (total_size_.load() & page_mask_); diff --git a/cpp/src/common/allocator/mem_alloc.cc b/cpp/src/common/allocator/mem_alloc.cc index deda0b8d8..c8091c394 100644 --- a/cpp/src/common/allocator/mem_alloc.cc +++ b/cpp/src/common/allocator/mem_alloc.cc @@ -223,17 +223,16 @@ void ModStat::print_stat() { } } - std::cout << "=== Memory Statistics ===" << std::endl; + std::cout << "=== Memory Statistics ===" << '\n'; for (int i = 0; i < count; i++) { std::cout << " " << entries[i].name << ": " << entries[i].val - << " bytes" << std::endl; + << " bytes" << '\n'; } double kb = total / 1024.0; double mb = kb / 1024.0; std::cout << " TOTAL: " << total << " bytes / " << std::fixed - << std::setprecision(2) << kb << " KB / " << mb << " MB" - << std::endl; - std::cout << "=========================" << std::endl; + << std::setprecision(2) << kb << " KB / " << mb << " MB" << '\n'; + std::cout << "=========================" << '\n'; } BaseAllocator g_base_allocator; diff --git a/cpp/src/common/allocator/page_arena.h b/cpp/src/common/allocator/page_arena.h index 40aad4c96..804c44563 100644 --- a/cpp/src/common/allocator/page_arena.h +++ b/cpp/src/common/allocator/page_arena.h @@ -84,9 +84,14 @@ class PageArena { } INLINE char* alloc(uint32_t size, size_t alignment = alignof(std::max_align_t)) { - auto current = reinterpret_cast(cur_alloc_); - uintptr_t aligned = (current + alignment - 1) & ~(alignment - 1); - char* ret = reinterpret_cast(aligned); + // Compute the padding in address space and advance the pointer + // instead of casting an integer back to a pointer (which + // pessimizes aliasing analysis). + const uintptr_t misalignment = + reinterpret_cast(cur_alloc_) & (alignment - 1); + const uintptr_t padding = + (alignment - misalignment) & (alignment - 1); + char* ret = cur_alloc_ + padding; if (ret + size > page_end_) { return nullptr; } diff --git a/cpp/src/common/device_id.cc b/cpp/src/common/device_id.cc index 0b5e6c107..6e096ebc5 100644 --- a/cpp/src/common/device_id.cc +++ b/cpp/src/common/device_id.cc @@ -235,9 +235,9 @@ std::vector StringArrayDeviceID::split_device_id_string( if (segment_cnt == 1) { // "root" -> {"root"} final_segments.push_back(splits[0]); - } else if (segment_cnt < - static_cast(storage::DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME + - 1)) { + } else if (segment_cnt < static_cast( + storage::DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME) + + 1) { // "root.a" -> {"root", "a"} // "root.a.b" -> {"root.a", "b"} std::string table_name = std::accumulate( diff --git a/cpp/src/common/row_record.h b/cpp/src/common/row_record.h index a640ecc29..b822c8002 100644 --- a/cpp/src/common/row_record.h +++ b/cpp/src/common/row_record.h @@ -93,7 +93,7 @@ struct Field { } default: { assert(false); - std::cout << "unknown data type" << std::endl; + std::cout << "unknown data type" << '\n'; } } } @@ -114,7 +114,7 @@ struct Field { case common::TSDataType::DOUBLE: return value_.dval_; default: - std::cout << "unknown data type" << std::endl; + std::cout << "unknown data type" << '\n'; break; } return -1; // when data type is unknown diff --git a/cpp/src/common/schema.h b/cpp/src/common/schema.h index 007e1f1db..76e1133fa 100644 --- a/cpp/src/common/schema.h +++ b/cpp/src/common/schema.h @@ -439,6 +439,7 @@ class TableSchema { std::vector get_data_types() const { std::vector ret; + ret.reserve(column_schemas_.size()); for (const auto& measurement_schema : column_schemas_) { ret.emplace_back(measurement_schema->data_type_); } diff --git a/cpp/src/common/tablet.cc b/cpp/src/common/tablet.cc index 3bfc54bb9..d234e61d6 100644 --- a/cpp/src/common/tablet.cc +++ b/cpp/src/common/tablet.cc @@ -531,8 +531,10 @@ int Tablet::add_value(uint32_t row_index, const std::string& measurement_name, } template <> -int Tablet::add_value(uint32_t row_index, const std::string& measurement_name, - std::string val) { +int Tablet::add_value( + uint32_t row_index, const std::string& measurement_name, + // Signature must match the primary template (T val). + std::string val) { // NOLINT(performance-unnecessary-value-param) return add_value(row_index, measurement_name, String(val)); } diff --git a/cpp/src/common/tablet.h b/cpp/src/common/tablet.h index 095157ac7..7d6d5dcbc 100644 --- a/cpp/src/common/tablet.h +++ b/cpp/src/common/tablet.h @@ -143,12 +143,12 @@ class Tablet { int max_rows = DEFAULT_MAX_ROWS) : max_row_num_(max_rows), insert_target_name_(device_id), - schema_vec_(schema_vec), + schema_vec_(std::move(schema_vec)), timestamps_(nullptr), value_matrix_(nullptr), bitmaps_(nullptr) { ASSERT(device_id.size() >= 1); - ASSERT(schema_vec != NULL); + ASSERT(schema_vec_ != NULL); ASSERT(max_rows > 0 && max_rows < (1 << 30)); if (max_rows < 0) { ASSERT(false); diff --git a/cpp/src/common/tsfile_common.cc b/cpp/src/common/tsfile_common.cc index 02a0b1216..05ebc8736 100644 --- a/cpp/src/common/tsfile_common.cc +++ b/cpp/src/common/tsfile_common.cc @@ -173,6 +173,7 @@ int TSMIterator::get_next(std::shared_ptr& ret_device_id, ret_ts_index.finish(); } if (UNLIKELY(ret_device_id == nullptr)) { + // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores) ret = E_TSFILE_WRITER_META_ERR; // log_err("null device name from chunk_group_meta_iter, ret=%d", ret); ASSERT(false); @@ -383,14 +384,16 @@ int TsFileMeta::deserialize_from(common::ByteStream& in) { /* ================ MetaIndexNode ================ */ int MetaIndexNode::binary_search_children( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI std::shared_ptr key, bool exact_search, std::shared_ptr& ret_index_entry, int64_t& ret_end_offset) { - return binary_search_children(key, exact_search, ret_index_entry, + return binary_search_children(std::move(key), exact_search, ret_index_entry, ret_end_offset, *pa_); } int MetaIndexNode::binary_search_children( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI std::shared_ptr key, bool exact_search, std::shared_ptr& ret_index_entry, int64_t& ret_end_offset, common::PageArena& result_pa) { diff --git a/cpp/src/common/tsfile_common.h b/cpp/src/common/tsfile_common.h index 1ff7cc2d1..a61f1b845 100644 --- a/cpp/src/common/tsfile_common.h +++ b/cpp/src/common/tsfile_common.h @@ -176,9 +176,11 @@ struct ChunkHeader { #ifndef NDEBUG friend std::ostream& operator<<(std::ostream& os, const ChunkHeader& h) { os << "{measurement_name=" << h.measurement_name_ - << ", data_size=" << h.data_size_ << ", data_type=" << h.data_type_ - << ", compression_type=" << h.compression_type_ - << ", encoding_type=" << h.encoding_type_ + << ", data_size=" << h.data_size_ + << ", data_type=" << static_cast(h.data_type_) + << ", compression_type=" + << static_cast(h.compression_type_) + << ", encoding_type=" << static_cast(h.encoding_type_) << ", num_of_pages=" << h.num_of_pages_ << ", serialized_size=" << h.serialized_size_ << ", chunk_type=" << (int)h.chunk_type_ << "}"; @@ -282,7 +284,7 @@ struct ChunkMeta { #ifndef NDEBUG friend std::ostream& operator<<(std::ostream& os, const ChunkMeta& cm) { os << "{measurement_name=" << cm.measurement_name_ - << ", data_type=" << cm.data_type_ + << ", data_type=" << static_cast(cm.data_type_) << ", offset_of_chunk_header=" << cm.offset_of_chunk_header_ << ", mask=" << ((int)cm.mask_); if (cm.statistic_ == nullptr) { @@ -303,7 +305,7 @@ struct ChunkGroupMeta { : chunk_meta_list_(pa_ptr) {} FORCE_INLINE int init(std::shared_ptr device_id) { - device_id_ = device_id; + device_id_ = std::move(device_id); return 0; } FORCE_INLINE int push(ChunkMeta* cm) { @@ -1158,7 +1160,7 @@ struct MetaIndexNode { FORCE_INLINE bool is_empty() const { return children_.size() == 0; } - FORCE_INLINE int push_entry(std::shared_ptr entry) { + FORCE_INLINE int push_entry(const std::shared_ptr& entry) { #if DEBUG_SE std::cout << "MetaIndexNode.push_entry(" << *entry << ")" << std::endl; #endif diff --git a/cpp/src/compress/lz4_compressor.h b/cpp/src/compress/lz4_compressor.h index 108998345..887bb6563 100644 --- a/cpp/src/compress/lz4_compressor.h +++ b/cpp/src/compress/lz4_compressor.h @@ -42,7 +42,7 @@ class LZ4Compressor : public Compressor { : compressed_buf_(nullptr), uncompressed_buf_(nullptr), uncompressed_buf_capacity_(0){}; - ~LZ4Compressor() { destroy(); } + ~LZ4Compressor() { LZ4Compressor::destroy(); } // Owns the reused decompression buffer; a copy would double-free it. LZ4Compressor(const LZ4Compressor&) = delete; LZ4Compressor& operator=(const LZ4Compressor&) = delete; diff --git a/cpp/src/compress/zstd_compressor.h b/cpp/src/compress/zstd_compressor.h index c0649f6ee..655d0f273 100644 --- a/cpp/src/compress/zstd_compressor.h +++ b/cpp/src/compress/zstd_compressor.h @@ -32,7 +32,7 @@ namespace storage { class ZstdCompressor : public Compressor { public: ZstdCompressor() : compressed_buf_(nullptr), uncompressed_buf_(nullptr) {} - ~ZstdCompressor() OVERRIDE { destroy(); } + ~ZstdCompressor() OVERRIDE { ZstdCompressor::destroy(); } int reset(bool for_compress) OVERRIDE; void destroy() OVERRIDE; diff --git a/cpp/src/cwrapper/tsfile_cwrapper.cc b/cpp/src/cwrapper/tsfile_cwrapper.cc index c2e6f2a0d..9672ff3b4 100644 --- a/cpp/src/cwrapper/tsfile_cwrapper.cc +++ b/cpp/src/cwrapper/tsfile_cwrapper.cc @@ -845,6 +845,7 @@ ResultSet tsfile_query_table(TsFileReader reader, const char* table_name, auto* r = static_cast(reader); storage::ResultSet* table_result_set = nullptr; std::vector column_names; + column_names.reserve(column_num); for (uint32_t i = 0; i < column_num; i++) { column_names.emplace_back(columns[i]); } @@ -859,6 +860,7 @@ ResultSet tsfile_query_table_on_tree(TsFileReader reader, char** columns, auto* r = static_cast(reader); storage::ResultSet* table_result_set = nullptr; std::vector column_names; + column_names.reserve(column_num); for (uint32_t i = 0; i < column_num; i++) { column_names.emplace_back(columns[i]); } @@ -998,6 +1000,7 @@ ResultSet tsfile_query_table_batch(TsFileReader reader, const char* table_name, auto* r = static_cast(reader); storage::ResultSet* table_result_set = nullptr; std::vector column_names; + column_names.reserve(column_num); for (uint32_t i = 0; i < column_num; i++) { column_names.emplace_back(columns[i]); } @@ -2049,6 +2052,9 @@ ERRNO populate_c_metadata_map_from_cpp( if (idx == nullptr) { continue; } + // slot stays below timeseries_count: the array is sized + // by the non-null count and incremented once per entry. + // NOLINTNEXTLINE(clang-analyzer-security.ArrayBound) TimeseriesMetadata& m = e.timeseries[slot]; common::String mn = idx->get_measurement_name(); m.measurement_name = strdup(mn.to_std_string().c_str()); @@ -3001,6 +3007,7 @@ ResultSet tsfile_query_table_with_tag_filter( auto* r = static_cast(reader); storage::ResultSet* table_result_set = nullptr; std::vector column_names; + column_names.reserve(column_num); for (uint32_t i = 0; i < column_num; i++) { column_names.emplace_back(columns[i]); } diff --git a/cpp/src/dataset/dataset_index.cc b/cpp/src/dataset/dataset_index.cc index f098e3aa2..7bda93f8e 100644 --- a/cpp/src/dataset/dataset_index.cc +++ b/cpp/src/dataset/dataset_index.cc @@ -957,7 +957,11 @@ DatasetIndexStatus MappedDatasetIndex::find_device_id( return DatasetIndexStatus::NOT_FOUND; } DatasetIndexSectionView view; - section(DatasetIndexSectionType::DEVICE_NAME_INDEX, view); + if (section(DatasetIndexSectionType::DEVICE_NAME_INDEX, view) != + DatasetIndexStatus::OK || + view.data == nullptr) { + return DatasetIndexStatus::NOT_FOUND; + } const DeviceNameIndexRecord* records = reinterpret_cast(view.data); const uint64_t hash = dataset_index_name_hash(name.data(), name.size()); @@ -990,7 +994,11 @@ DatasetIndexStatus MappedDatasetIndex::find_column_id( return DatasetIndexStatus::NOT_FOUND; } DatasetIndexSectionView view; - section(DatasetIndexSectionType::COLUMN_NAME_INDEX, view); + if (section(DatasetIndexSectionType::COLUMN_NAME_INDEX, view) != + DatasetIndexStatus::OK || + view.data == nullptr) { + return DatasetIndexStatus::NOT_FOUND; + } const ColumnNameIndexRecord* records = reinterpret_cast(view.data); const uint64_t hash = dataset_index_name_hash(name.data(), name.size()); @@ -1023,7 +1031,11 @@ DatasetIndexStatus MappedDatasetIndex::find_series_id( return DatasetIndexStatus::NOT_FOUND; } DatasetIndexSectionView view; - section(DatasetIndexSectionType::LOGICAL_SERIES, view); + if (section(DatasetIndexSectionType::LOGICAL_SERIES, view) != + DatasetIndexStatus::OK || + view.data == nullptr) { + return DatasetIndexStatus::NOT_FOUND; + } const LogicalSeriesRecord* records = reinterpret_cast(view.data); uint32_t low = device->first_series_id; diff --git a/cpp/src/encoding/alp/alp_traits.h b/cpp/src/encoding/alp/alp_traits.h index e1046df69..6508ddf31 100644 --- a/cpp/src/encoding/alp/alp_traits.h +++ b/cpp/src/encoding/alp/alp_traits.h @@ -88,6 +88,9 @@ struct AlpTypeTraits { } static bool BitwiseEqual(float lhs, float rhs) { + // Bitwise comparison is intentional here: ALP deduplicates + // identical bit patterns, including -0.0 vs 0.0 and NaNs. + // NOLINTNEXTLINE(bugprone-suspicious-memory-comparison) return std::memcmp(&lhs, &rhs, sizeof(float)) == 0; } }; @@ -148,6 +151,8 @@ struct AlpTypeTraits { } static bool BitwiseEqual(double lhs, double rhs) { + // See BitwiseEqual(float): bitwise comparison is intentional. + // NOLINTNEXTLINE(bugprone-suspicious-memory-comparison) return std::memcmp(&lhs, &rhs, sizeof(double)) == 0; } }; diff --git a/cpp/src/encoding/chimp_decoder.h b/cpp/src/encoding/chimp_decoder.h index 25f657113..6f1c26b86 100644 --- a/cpp/src/encoding/chimp_decoder.h +++ b/cpp/src/encoding/chimp_decoder.h @@ -32,8 +32,8 @@ namespace storage { -static constexpr int8_t CHIMP_LEADING_DECODING[8] = {0, 8, 12, 16, - 18, 20, 22, 24}; +static constexpr uint8_t CHIMP_LEADING_DECODING[8] = {0, 8, 12, 16, + 18, 20, 22, 24}; template class ChimpDecoder : public Decoder { diff --git a/cpp/src/encoding/chimp_encoder.h b/cpp/src/encoding/chimp_encoder.h index 0be5d3b63..a5d6b6063 100644 --- a/cpp/src/encoding/chimp_encoder.h +++ b/cpp/src/encoding/chimp_encoder.h @@ -39,7 +39,7 @@ static constexpr int8_t CHIMP_LEADING_REPRESENTATION[64] = { 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}; -static constexpr int8_t CHIMP_LEADING_ROUND[64] = { +static constexpr uint8_t CHIMP_LEADING_ROUND[64] = { 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 12, 12, 12, 12, 16, 16, 18, 18, 20, 20, 22, 22, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, diff --git a/cpp/src/encoding/dictionary_encoder.h b/cpp/src/encoding/dictionary_encoder.h index 8f7c495c4..fa76bdb12 100644 --- a/cpp/src/encoding/dictionary_encoder.h +++ b/cpp/src/encoding/dictionary_encoder.h @@ -79,7 +79,7 @@ class DictionaryEncoder : public Encoder { return encode(std::string(value), out); } - int encode(std::string value, common::ByteStream& out) { + int encode(const std::string& value, common::ByteStream& out) { if (entry_index_.count(value) == 0) { index_entry_.push_back(value); map_size_ = map_size_ + value.length(); diff --git a/cpp/src/encoding/plain_encoder.h b/cpp/src/encoding/plain_encoder.h index 3db87e173..21784dca1 100644 --- a/cpp/src/encoding/plain_encoder.h +++ b/cpp/src/encoding/plain_encoder.h @@ -33,7 +33,9 @@ namespace storage { class PlainEncoder : public Encoder { public: PlainEncoder() {} - ~PlainEncoder() { destroy(); } + // Qualified call: destroy() is virtual, but a destructor must not + // dispatch to derived overrides (and none exist for PlainEncoder). + ~PlainEncoder() { PlainEncoder::destroy(); } void destroy() override { /* do nothing for PlainEncoder */ } void reset() override { /* do thing for PlainEncoder */ diff --git a/cpp/src/encoding/ts2diff_decoder.h b/cpp/src/encoding/ts2diff_decoder.h index 5fa1b62bd..521b85ce4 100644 --- a/cpp/src/encoding/ts2diff_decoder.h +++ b/cpp/src/encoding/ts2diff_decoder.h @@ -475,7 +475,7 @@ class TS2DIFFDecoder : public Decoder { template <> inline int32_t TS2DIFFDecoder::decode(common::ByteStream& in) { - int32_t ret_value = stored_value_; + int32_t ret_value = 0; if (UNLIKELY(current_index_ == 0)) { if (read_header(in) != common::E_OK) { // Poison the block state so callers stop; value is undefined @@ -518,7 +518,7 @@ inline int32_t TS2DIFFDecoder::decode(common::ByteStream& in) { template <> inline int64_t TS2DIFFDecoder::decode(common::ByteStream& in) { - int64_t ret_value = stored_value_; + int64_t ret_value = 0; if (UNLIKELY(current_index_ == 0)) { if (read_header(in) != common::E_OK) { write_index_ = 0; diff --git a/cpp/src/encoding/ts2diff_encoder.h b/cpp/src/encoding/ts2diff_encoder.h index e08af0526..1e71c653f 100644 --- a/cpp/src/encoding/ts2diff_encoder.h +++ b/cpp/src/encoding/ts2diff_encoder.h @@ -100,7 +100,9 @@ class TS2DIFFEncoder : public Encoder { public: TS2DIFFEncoder() { init(); } - ~TS2DIFFEncoder() { destroy(); } + // Qualified call: destroy() is virtual, but a destructor must not + // dispatch to derived overrides. + ~TS2DIFFEncoder() { TS2DIFFEncoder::destroy(); } void reset() override { write_index_ = -1; } diff --git a/cpp/src/encoding/zigzag_encoder.h b/cpp/src/encoding/zigzag_encoder.h index 96d3cb9cc..4ecea6e30 100644 --- a/cpp/src/encoding/zigzag_encoder.h +++ b/cpp/src/encoding/zigzag_encoder.h @@ -144,7 +144,6 @@ inline int ZigzagEncoder::encode(int32_t value) { } write_byte_without_subsequence(value_zigzag); - value_zigzag = (uint32_t)value_zigzag >> 7; return common::E_OK; } @@ -176,7 +175,6 @@ inline int ZigzagEncoder::encode(int64_t value) { } write_byte_without_subsequence(value_zigzag); - value_zigzag = (uint64_t)value_zigzag >> 7; return common::E_OK; } diff --git a/cpp/src/file/local_random_access_read_file.cc b/cpp/src/file/local_random_access_read_file.cc index cc42d4161..719245948 100644 --- a/cpp/src/file/local_random_access_read_file.cc +++ b/cpp/src/file/local_random_access_read_file.cc @@ -97,10 +97,10 @@ int LocalRandomAccessReadFile::generation(uint64_t& size, int LocalRandomAccessReadFile::get_file_generation_from_descriptor( uint64_t& size, uint64_t& fingerprint) const { int64_t mtime_ns = 0; -#ifdef _WIN32 if (fd_ < 0) { return E_FILE_READ_ERR; } +#ifdef _WIN32 intptr_t handle_value = _get_osfhandle(fd_); if (handle_value == -1) { return E_FILE_READ_ERR; diff --git a/cpp/src/file/local_random_access_read_file.h b/cpp/src/file/local_random_access_read_file.h index 4d74254fd..392aea3a2 100644 --- a/cpp/src/file/local_random_access_read_file.h +++ b/cpp/src/file/local_random_access_read_file.h @@ -35,12 +35,14 @@ namespace storage { class LocalRandomAccessReadFile : public RandomAccessReadFile { public: LocalRandomAccessReadFile(); - ~LocalRandomAccessReadFile() override { destroy(); } + ~LocalRandomAccessReadFile() override { + LocalRandomAccessReadFile::destroy(); + } LocalRandomAccessReadFile(const LocalRandomAccessReadFile&) = delete; LocalRandomAccessReadFile& operator=(const LocalRandomAccessReadFile&) = delete; - void destroy() { close(); } + void destroy() { LocalRandomAccessReadFile::close(); } int open(const std::string& file_path); FORCE_INLINE bool is_opened() const override { diff --git a/cpp/src/file/restorable_tsfile_io_writer.cc b/cpp/src/file/restorable_tsfile_io_writer.cc index 605ff6e03..7d931450a 100644 --- a/cpp/src/file/restorable_tsfile_io_writer.cc +++ b/cpp/src/file/restorable_tsfile_io_writer.cc @@ -825,6 +825,22 @@ int RestorableTsFileIOWriter::self_check(bool truncate_corrupted) { return E_TSFILE_CORRUPTED; } + // Read errors during the scan are treated as a truncated tail and + // recovered below, but an allocation failure is not corruption: surface + // it instead of silently returning a recovered writer. + if (ret == common::E_OOM) { + // The pending chunk group lives in the self-check arena and was + // never registered for cleanup. Release its device reference + // before the arena is torn down without running destructors. + if (cur_cgm != nullptr) { + cur_cgm->device_id_.reset(); + cur_cgm = nullptr; + } + cur_device_id.reset(); + reader.close(); + return ret; + } + flush_chunk_group(); get_schema()->finalize_table_schemas(); reader.close(); diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index 6de4ed20d..2b5fffb04 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -86,19 +86,20 @@ void TsFileIOReader::reset() { } } -int TsFileIOReader::alloc_ssi(std::shared_ptr device_id, - const std::string& measurement_name, - TsFileSeriesScanIterator*& ssi, - common::PageArena& pa, Filter* time_filter) { - return alloc_ssi(device_id, measurement_name, ssi, pa, time_filter, - nullptr); +int TsFileIOReader::alloc_ssi( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI + std::shared_ptr device_id, const std::string& measurement_name, + TsFileSeriesScanIterator*& ssi, common::PageArena& pa, + Filter* time_filter) { + return alloc_ssi(std::move(device_id), measurement_name, ssi, pa, + time_filter, nullptr); } -int TsFileIOReader::alloc_ssi(std::shared_ptr device_id, - const std::string& measurement_name, - TsFileSeriesScanIterator*& ssi, - common::PageArena& pa, Filter* time_filter, - common::TSDataType* out_series_type) { +int TsFileIOReader::alloc_ssi( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI + std::shared_ptr device_id, const std::string& measurement_name, + TsFileSeriesScanIterator*& ssi, common::PageArena& pa, Filter* time_filter, + common::TSDataType* out_series_type) { int ret = E_OK; if (RET_FAIL(load_tsfile_meta_if_necessary())) { } else if (!bloom_filter_contains(device_id->get_device_name(), @@ -339,6 +340,7 @@ int TsFileIOReader::alloc_prepared_multi_ssi( } int TsFileIOReader::alloc_multi_ssi( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI std::shared_ptr device_id, const std::vector& measurement_names, TsFileSeriesScanIterator*& ssi, common::PageArena& pa, Filter* time_filter, @@ -450,6 +452,7 @@ void TsFileIOReader::revert_ssi(TsFileSeriesScanIterator* ssi) { } int TsFileIOReader::get_device_timeseries_meta_without_chunk_meta( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI std::shared_ptr device_id, std::vector& timeseries_indexs, PageArena& pa) { int ret = E_OK; @@ -677,9 +680,9 @@ std::string TsFileIOReader::device_node_cache_key( return key; } -int TsFileIOReader::get_cached_device_node(std::shared_ptr device_id, - common::PageArena& pa, - CachedDeviceNode& out) { +int TsFileIOReader::get_cached_device_node( + const std::shared_ptr& device_id, common::PageArena& pa, + CachedDeviceNode& out) { (void)pa; std::string dev_name = device_node_cache_key(device_id); @@ -757,8 +760,8 @@ int TsFileIOReader::get_cached_device_node(std::shared_ptr device_id, } int TsFileIOReader::load_timeseries_index_for_ssi( - std::shared_ptr device_id, const std::string& measurement_name, - TsFileSeriesScanIterator*& ssi) { + const std::shared_ptr& device_id, + const std::string& measurement_name, TsFileSeriesScanIterator*& ssi) { int ret = E_OK; auto& pa = ssi->timeseries_index_pa_; @@ -811,6 +814,7 @@ int TsFileIOReader::load_timeseries_index_for_ssi( } int TsFileIOReader::load_device_index_entry( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI std::shared_ptr device_name, std::shared_ptr& device_index_entry, int64_t& end_offset) { int ret = E_OK; @@ -846,7 +850,7 @@ int TsFileIOReader::load_device_index_entry( int TsFileIOReader::load_measurement_index_entry( const std::string& measurement_name_str, - std::shared_ptr top_node, + const std::shared_ptr& top_node, std::shared_ptr& ret_measurement_index_entry, int64_t& ret_end_offset, common::PageArena& result_pa) { int ret = E_OK; @@ -942,6 +946,7 @@ int TsFileIOReader::read_device_meta_index(int64_t start_offset, } int TsFileIOReader::get_timeseries_indexes( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI std::shared_ptr device_id, const std::unordered_set& measurement_names, std::vector& timeseries_indexs, common::PageArena& pa) { @@ -1077,8 +1082,8 @@ int TsFileIOReader::get_timeseries_indexes( * @index_node leaf device node or leaf measurement node */ int TsFileIOReader::search_from_leaf_node( - std::shared_ptr target_name, - std::shared_ptr index_node, + const std::shared_ptr& target_name, + const std::shared_ptr& index_node, std::shared_ptr& ret_index_entry, int64_t& ret_end_offset) { int ret = E_OK; @@ -1160,8 +1165,8 @@ int TsFileIOReader::get_cached_index_node(int64_t start_offset, } int TsFileIOReader::search_from_internal_node( - std::shared_ptr target_name, bool is_device, - std::shared_ptr index_node, + const std::shared_ptr& target_name, bool is_device, + const std::shared_ptr& index_node, std::shared_ptr& ret_index_entry, int64_t& ret_end_offset, common::PageArena* result_pa) { int ret = E_OK; @@ -1172,7 +1177,7 @@ int TsFileIOReader::search_from_internal_node( ASSERT(index_node->node_type_ == INTERNAL_MEASUREMENT || index_node->node_type_ == INTERNAL_DEVICE); if (RET_FAIL(index_node->binary_search_children( - target_name, /*exact=*/false, index_entry, end_offset, + target_name, /*exact_search=*/false, index_entry, end_offset, output_pa))) { return ret; } @@ -1199,15 +1204,15 @@ int TsFileIOReader::search_from_internal_node( } if (cur_level.node->node_type_ == LEAF_DEVICE) { return cur_level.node->binary_search_children( - target_name, /*exact=*/true, ret_index_entry, ret_end_offset, - output_pa); + target_name, /*exact_search=*/true, ret_index_entry, + ret_end_offset, output_pa); } else if (cur_level.node->node_type_ == LEAF_MEASUREMENT) { return cur_level.node->binary_search_children( - target_name, /*exact=*/false, ret_index_entry, ret_end_offset, - output_pa); + target_name, /*exact_search=*/false, ret_index_entry, + ret_end_offset, output_pa); } else { ret = cur_level.node->binary_search_children( - target_name, /*exact=*/false, index_entry, end_offset, + target_name, /*exact_search=*/false, index_entry, end_offset, output_pa); } } @@ -1215,7 +1220,7 @@ int TsFileIOReader::search_from_internal_node( } bool TsFileIOReader::is_aligned_device( - std::shared_ptr measurement_node) { + const std::shared_ptr& measurement_node) { if (measurement_node->children_.empty()) { return false; } @@ -1225,7 +1230,7 @@ bool TsFileIOReader::is_aligned_device( } int TsFileIOReader::get_time_column_metadata( - std::shared_ptr measurement_node, + const std::shared_ptr& measurement_node, TimeseriesIndex*& ret_timeseries_index, PageArena& pa) { int ret = E_OK; if (!is_aligned_device(measurement_node)) { @@ -1486,7 +1491,7 @@ int TsFileIOReader::do_load_all_timeseries_index( } int TsFileIOReader::get_all_leaf( - std::shared_ptr index_node, + const std::shared_ptr& index_node, std::vector, int64_t>>& index_node_entry_list, common::PageArena& pa) { diff --git a/cpp/src/file/tsfile_io_reader.h b/cpp/src/file/tsfile_io_reader.h index f996991cd..fbf890245 100644 --- a/cpp/src/file/tsfile_io_reader.h +++ b/cpp/src/file/tsfile_io_reader.h @@ -175,7 +175,7 @@ class TsFileIOReader { int load_measurement_index_entry( const std::string& measurement_name, - std::shared_ptr top_node, + const std::shared_ptr& top_node, std::shared_ptr& ret_measurement_index_entry, int64_t& ret_end_offset, common::PageArena& result_pa); @@ -184,10 +184,11 @@ class TsFileIOReader { std::vector, int64_t>>& ret_measurement_index_entry); - bool is_aligned_device(std::shared_ptr measurement_node); + bool is_aligned_device( + const std::shared_ptr& measurement_node); int get_time_column_metadata( - std::shared_ptr measurement_node, + const std::shared_ptr& measurement_node, TimeseriesIndex*& ret_timeseries_index, common::PageArena& pa); int do_load_timeseries_index(const std::string& measurement_name_str, @@ -210,18 +211,18 @@ class TsFileIOReader { int64_t start_offset, int64_t end_offset, common::PageArena& pa, std::vector& ts_indexs); - int load_timeseries_index_for_ssi(std::shared_ptr device_id, - const std::string& measurement_name, - TsFileSeriesScanIterator*& ssi); + int load_timeseries_index_for_ssi( + const std::shared_ptr& device_id, + const std::string& measurement_name, TsFileSeriesScanIterator*& ssi); - int search_from_leaf_node(std::shared_ptr target_name, - std::shared_ptr index_node, + int search_from_leaf_node(const std::shared_ptr& target_name, + const std::shared_ptr& index_node, std::shared_ptr& ret_index_entry, int64_t& ret_end_offset); int search_from_internal_node( - std::shared_ptr target_name, bool is_device, - std::shared_ptr index_node, + const std::shared_ptr& target_name, bool is_device, + const std::shared_ptr& index_node, std::shared_ptr& ret_index_entry, int64_t& ret_end_offset, common::PageArena* result_pa = nullptr); @@ -237,7 +238,7 @@ class TsFileIOReader { // outlive index_node_entry_list. Callers pass the same arena they later // hand to do_load_all_timeseries_index(). int get_all_leaf( - std::shared_ptr index_node, + const std::shared_ptr& index_node, std::vector, int64_t>>& index_node_entry_list, common::PageArena& pa); @@ -329,7 +330,7 @@ class TsFileIOReader { // failure (E_DEVICE_NOT_EXIST when the device is absent, the propagated // error otherwise). Copying into out keeps the caller safe from rehash / // concurrent eviction of the cache map. - int get_cached_device_node(std::shared_ptr device_id, + int get_cached_device_node(const std::shared_ptr& device_id, common::PageArena& pa, CachedDeviceNode& out); #ifdef ENABLE_TEST diff --git a/cpp/src/file/tsfile_io_writer.cc b/cpp/src/file/tsfile_io_writer.cc index 20d83e150..7c08ea40f 100644 --- a/cpp/src/file/tsfile_io_writer.cc +++ b/cpp/src/file/tsfile_io_writer.cc @@ -164,6 +164,7 @@ int TsFileIOWriter::start_file() { } int TsFileIOWriter::start_flush_chunk_group( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI std::shared_ptr device_name, bool is_aligned) { int ret = write_byte(CHUNK_GROUP_HEADER_MARKER); if (ret != common::E_OK) { @@ -320,15 +321,15 @@ int TsFileIOWriter::end_file() { OFFSET_DEBUG("before end file"); if (RET_FAIL(write_log_index_range())) { - std::cout << "writer range index error, ret =" << ret << std::endl; + std::cout << "writer range index error, ret =" << ret << '\n'; } else if (RET_FAIL(write_file_index())) { - std::cout << "writer file index error, ret = " << ret << std::endl; + std::cout << "writer file index error, ret = " << ret << '\n'; } else if (RET_FAIL(write_file_footer())) { - std::cout << "writer file footer error, ret = " << ret << std::endl; + std::cout << "writer file footer error, ret = " << ret << '\n'; } else if (g_config_value_.sync_on_close_ && RET_FAIL(sync_file())) { - std::cout << "sync file error, ret = " << ret << std::endl; + std::cout << "sync file error, ret = " << ret << '\n'; } else if (RET_FAIL(close_file())) { - std::cout << "close file error, ret = " << ret << std::endl; + std::cout << "close file error, ret = " << ret << '\n'; } return ret; @@ -341,13 +342,13 @@ int TsFileIOWriter::write_log_index_range() { const int64_t max_plan_index = 0; // 0x8000000000000000; // OFFSET_DEBUG("before writer log index"); if (RET_FAIL(write_byte(OPERATION_INDEX_RANGE))) { - std::cout << "writer byte error " << ret << std::endl; + std::cout << "writer byte error " << ret << '\n'; } else if (RET_FAIL(SerializationUtil::write_i64(min_plan_index, write_stream_))) { - std::cout << "min index error " << ret << std::endl; + std::cout << "min index error " << ret << '\n'; } else if (RET_FAIL(SerializationUtil::write_i64(max_plan_index, write_stream_))) { - std::cout << "max index error " << ret << std::endl; + std::cout << "max index error " << ret << '\n'; } return ret; } @@ -471,9 +472,9 @@ int TsFileIOWriter::write_file_index() { tmp_device_name.dup_from(device_id->get_device_name(), meta_allocator_); // Time column also need add to bloom filter. - ret = filter.add_path_entry(tmp_device_name, measurement_name); - - if (RET_FAIL(ts_index.serialize_to(write_stream_))) { + if (RET_FAIL( + filter.add_path_entry(tmp_device_name, measurement_name))) { + } else if (RET_FAIL(ts_index.serialize_to(write_stream_))) { } else { #if DEBUG_SE std::cout << "ts_index.serialize. ts_index=" << ts_index @@ -487,7 +488,9 @@ int TsFileIOWriter::write_file_index() { if (ret == E_NO_MORE_DATA) { ret = E_OK; } - ASSERT(ret == E_OK); + // A non-OK value here is a propagated failure (for example a failed + // bloom-filter allocation); the guarded finalization below skips + // itself and the error is returned to the caller. if (IS_SUCC(ret) && cur_index_node != nullptr && cur_index_node_queue != nullptr) { // iter finish ASSERT(cur_index_node != nullptr); @@ -719,7 +722,7 @@ int TsFileIOWriter::alloc_and_init_meta_index_node( } int TsFileIOWriter::add_cur_index_node_to_queue( - std::shared_ptr node, + const std::shared_ptr& node, SimpleList>* queue) const { node->end_offset_ = cur_file_position(); #if DEBUG_SE @@ -741,7 +744,7 @@ int TsFileIOWriter::alloc_meta_index_node_queue( } int TsFileIOWriter::add_device_node( - DeviceNodeMap& device_map, std::shared_ptr device_id, + DeviceNodeMap& device_map, const std::shared_ptr& device_id, common::SimpleList>* measurement_index_node_queue, FileIndexWritingMemManager& wmm) { diff --git a/cpp/src/file/tsfile_io_writer.h b/cpp/src/file/tsfile_io_writer.h index b149358b4..c20315157 100644 --- a/cpp/src/file/tsfile_io_writer.h +++ b/cpp/src/file/tsfile_io_writer.h @@ -159,13 +159,13 @@ class TsFileIOWriter { std::shared_ptr& ret_node, MetaIndexNodeType node_type); int add_cur_index_node_to_queue( - std::shared_ptr node, + const std::shared_ptr& node, common::SimpleList>* queue) const; int alloc_meta_index_node_queue( FileIndexWritingMemManager& wmm, common::SimpleList>*& queue); int add_device_node(DeviceNodeMap& device_map, - std::shared_ptr device_id, + const std::shared_ptr& device_id, common::SimpleList>* measurement_index_node_queue, FileIndexWritingMemManager& wmm); diff --git a/cpp/src/file/write_file.h b/cpp/src/file/write_file.h index d62c7e21e..e5ee99014 100644 --- a/cpp/src/file/write_file.h +++ b/cpp/src/file/write_file.h @@ -42,7 +42,7 @@ class WriteFile : public RandomAccessWriteFile { // the fd — harmless on POSIX (unlink works on open files) but on Windows it // keeps the file locked so a subsequent remove() fails. close() is // idempotent, so this is a no-op when the file was already closed normally. - ~WriteFile() override { close(); } + ~WriteFile() override { WriteFile::close(); } int create(const std::string& file_name, int flags, mode_t mode); /** Kept for source compatibility; use is_opened(). */ bool file_opened() const { return is_opened(); } diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index 7ecda10f7..1c8e747d4 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -599,6 +599,7 @@ int AlignedChunkReader::decode_cur_time_page_data() { #endif if (ret != E_OK || time_uncompressed_buf_size != cur_time_page_header_.uncompressed_size_) { + // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores) ret = E_TSFILE_CORRUPTED; ASSERT(false); } @@ -663,6 +664,7 @@ int AlignedChunkReader::decode_cur_value_page_data() { #endif if (ret != E_OK || value_uncompressed_buf_size != cur_value_page_header_.uncompressed_size_) { + // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores) ret = E_TSFILE_CORRUPTED; ASSERT(false); } @@ -1069,7 +1071,7 @@ int AlignedChunkReader::decode_tv_buf_into_tsblock_by_datatype( time_in, value_in, row_appender, *pa, filter); break; default: - ret = E_NOT_SUPPORT; + ret = E_NOT_SUPPORT; // NOLINT(clang-analyzer-deadcode.DeadStores) ASSERT(false); } // An empty result only means every row of this page was filtered out; the diff --git a/cpp/src/reader/block/device_ordered_tsblock_reader.cc b/cpp/src/reader/block/device_ordered_tsblock_reader.cc index 2c9b08841..6a9577b63 100644 --- a/cpp/src/reader/block/device_ordered_tsblock_reader.cc +++ b/cpp/src/reader/block/device_ordered_tsblock_reader.cc @@ -103,9 +103,14 @@ int DeviceOrderedTsBlockReader::next(common::TsBlock*& ret_block) { int ret = common::E_OK; bool next = false; if (RET_FAIL(has_next(next)) || !next) { - } else if (RET_FAIL(current_reader_->next(ret_block))) { + return ret; } - return ret; + // has_next() only reports true after a reader has been created; keep an + // explicit guard so a future change cannot dereference null here. + if (UNLIKELY(current_reader_ == nullptr)) { + return common::E_INVALID_ARG; + } + return current_reader_->next(ret_block); } void DeviceOrderedTsBlockReader::close() { diff --git a/cpp/src/reader/block/single_device_tsblock_reader.h b/cpp/src/reader/block/single_device_tsblock_reader.h index 079943a87..b05bc4ce3 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.h +++ b/cpp/src/reader/block/single_device_tsblock_reader.h @@ -113,7 +113,9 @@ class SingleDeviceTsBlockReader : public TsBlockReader { TsFileIOReader* tsfile_io_reader, Filter* time_filter, Filter* field_filter); - ~SingleDeviceTsBlockReader() { close(); } + // Qualified call: close() is virtual, but a destructor must not dispatch + // to derived overrides. + ~SingleDeviceTsBlockReader() { SingleDeviceTsBlockReader::close(); } int has_next(bool& has_next) override; int next(common::TsBlock*& ret_block) override; int init(DeviceQueryTask* device_query_task, uint32_t block_size, diff --git a/cpp/src/reader/chunk_reader.cc b/cpp/src/reader/chunk_reader.cc index 3c51d28f7..6aac8586a 100644 --- a/cpp/src/reader/chunk_reader.cc +++ b/cpp/src/reader/chunk_reader.cc @@ -355,6 +355,7 @@ int ChunkReader::decode_cur_page_data(TsBlock*& ret_tsblock, Filter* filter, // uncompressed_buf, uncompressed_buf_size); if (ret != E_OK || uncompressed_buf_size != cur_page_header_.uncompressed_size_) { + // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores) ret = E_TSFILE_CORRUPTED; ASSERT(false); } @@ -378,6 +379,7 @@ int ChunkReader::decode_cur_page_data(TsBlock*& ret_tsblock, Filter* filter, << ", time_buf_size=" << time_buf_size << std::endl; #endif if (uncompressed_buf_size <= var_size + time_buf_size) { + // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores) ret = E_TSFILE_CORRUPTED; ASSERT(false); } @@ -892,7 +894,7 @@ int ChunkReader::decode_tv_buf_into_tsblock_by_datatype( time_in, value_in, row_appender, *pa, filter, row_offset); break; default: - ret = E_NOT_SUPPORT; + ret = E_NOT_SUPPORT; // NOLINT(clang-analyzer-deadcode.DeadStores) ASSERT(false); } // A decode that produced zero rows has two very different causes: diff --git a/cpp/src/reader/expression.cc b/cpp/src/reader/expression.cc index 3623b390e..ce8f1613d 100644 --- a/cpp/src/reader/expression.cc +++ b/cpp/src/reader/expression.cc @@ -42,7 +42,7 @@ void QueryExpression::add_time_filter_to_query_filter(Filter* time_filter, } else { std::cout << "Expression should contains only SingleSeriesExpression " "but other type is found" - << std::endl; + << '\n'; } } @@ -61,7 +61,7 @@ Expression* QueryExpression::combine_two_global_time_filter( my_exprs_.push_back(expr); return expr; } - std::cout << "unrecognized QueryFilterOperatorType :" << type << std::endl; + std::cout << "unrecognized QueryFilterOperatorType :" << type << '\n'; return nullptr; } @@ -109,7 +109,7 @@ Expression* QueryExpression::merge_second_tree_to_first_tree( Expression* QueryExpression::push_global_time_filter_to_all_series( Expression* time_filter, std::vector& selected_series) { if (selected_series.empty()) { - std::cout << "size of selectSeries could not be 0" << std::endl; + std::cout << "size of selectSeries could not be 0" << '\n'; return nullptr; } @@ -149,7 +149,7 @@ Expression* QueryExpression::handle_one_global_time_filter( } return merge_second_tree_to_first_tree(after_transform, expr); } - std::cout << "unknown relation in Expression:" << type << std::endl; + std::cout << "unknown relation in Expression:" << type << '\n'; return nullptr; } @@ -192,7 +192,7 @@ Expression* QueryExpression::optimize(Expression* expression, } } } else { - std::cout << "unknown Expression type:" << type << std::endl; + std::cout << "unknown Expression type:" << type << '\n'; } return nullptr; } diff --git a/cpp/src/reader/filter/aligned_field_filter.cc b/cpp/src/reader/filter/aligned_field_filter.cc index afa6bbb72..1ee4b8cbb 100644 --- a/cpp/src/reader/filter/aligned_field_filter.cc +++ b/cpp/src/reader/filter/aligned_field_filter.cc @@ -286,21 +286,25 @@ AlignedFieldFilter::AlignedFieldFilter(Kind kind, std::string column, AlignedFieldFilter::AlignedFieldFilter(Kind kind, Ptr left, Ptr right) : kind_(kind), left_(std::move(left)), right_(std::move(right)) {} -AlignedFieldFilter::Ptr AlignedFieldFilter::compare(std::string column, - CompareOp op, - Literal literal) { +AlignedFieldFilter::Ptr AlignedFieldFilter::compare( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI + std::string column, CompareOp op, Literal literal) { return Ptr(new AlignedFieldFilter(Kind::COMPARISON, normalize_column_name(column), op, std::move(literal))); } -AlignedFieldFilter::Ptr AlignedFieldFilter::is_null(std::string column) { +AlignedFieldFilter::Ptr AlignedFieldFilter::is_null( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI + std::string column) { return Ptr(new AlignedFieldFilter(Kind::IS_NULL, normalize_column_name(column), CompareOp::EQ, Literal())); } -AlignedFieldFilter::Ptr AlignedFieldFilter::is_not_null(std::string column) { +AlignedFieldFilter::Ptr AlignedFieldFilter::is_not_null( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI + std::string column) { return Ptr(new AlignedFieldFilter(Kind::IS_NOT_NULL, normalize_column_name(column), CompareOp::EQ, Literal())); diff --git a/cpp/src/reader/filter/filter.h b/cpp/src/reader/filter/filter.h index fc2494fac..b67f59822 100644 --- a/cpp/src/reader/filter/filter.h +++ b/cpp/src/reader/filter/filter.h @@ -54,11 +54,15 @@ class Filter { ASSERT(false); return false; } + // By-value keeps the virtual interface ABI-compatible: changing the + // parameter type would alter the vtable and every override's symbol. + // NOLINTBEGIN(performance-unnecessary-value-param) virtual bool satisfyRow(int time, std::vector segments) const { ASSERT(false); return false; } + // NOLINTEND(performance-unnecessary-value-param) virtual std::vector* get_time_ranges() { ASSERT(false); return nullptr; diff --git a/cpp/src/reader/filter/filter_type.h b/cpp/src/reader/filter/filter_type.h index 4714a1162..1dd123a04 100644 --- a/cpp/src/reader/filter/filter_type.h +++ b/cpp/src/reader/filter/filter_type.h @@ -39,8 +39,7 @@ FORCE_INLINE std::string filter_type_to_string(FilterType type) { return std::string("group by"); } default: { - std::cout << "filter_type_to_string unknown type:" << type - << std::endl; + std::cout << "filter_type_to_string unknown type:" << type << '\n'; return ""; } } diff --git a/cpp/src/reader/filter/tag_filter.cc b/cpp/src/reader/filter/tag_filter.cc index 03b8ae785..867f10a66 100644 --- a/cpp/src/reader/filter/tag_filter.cc +++ b/cpp/src/reader/filter/tag_filter.cc @@ -24,6 +24,9 @@ namespace storage { +// By-value parameters keep the tag-filter virtual interface ABI-compatible. +// NOLINTBEGIN(performance-unnecessary-value-param) + // TagFilter base class implementation TagFilter::TagFilter(int col_idx, std::string tag_value) : value_(std::move(tag_value)), value2_(""), col_idx_(col_idx) {} @@ -332,4 +335,6 @@ int TagFilterBuilder::get_tag_column_index(const std::string& columnName) { return idColumnOrder + 1; } +// NOLINTEND(performance-unnecessary-value-param) + } // namespace storage \ No newline at end of file diff --git a/cpp/src/reader/qds_without_timegenerator.cc b/cpp/src/reader/qds_without_timegenerator.cc index f4d869aaa..87499d60e 100644 --- a/cpp/src/reader/qds_without_timegenerator.cc +++ b/cpp/src/reader/qds_without_timegenerator.cc @@ -128,7 +128,6 @@ int QDSWithoutTimeGenerator::init_internal(TsFileIOReader* io_reader, ret == E_NOT_EXIST) { // Java-aligned: silently skip paths whose device or measurement // doesn't exist in this file. - ret = E_OK; continue; } if (ret == E_NO_MORE_DATA) { @@ -145,7 +144,6 @@ int QDSWithoutTimeGenerator::init_internal(TsFileIOReader* io_reader, valid_paths.push_back(paths[i]); column_names.push_back(paths[i].full_path_); ssi_data_types.push_back(empty_series_type); - ret = E_OK; continue; } if (ret != E_OK) { diff --git a/cpp/src/reader/result_set.h b/cpp/src/reader/result_set.h index 0b73595d4..c7e97df11 100644 --- a/cpp/src/reader/result_set.h +++ b/cpp/src/reader/result_set.h @@ -99,7 +99,7 @@ class ResultSetIterator; * it should be QDSWithTimeGenerator. * @note If the query uses the table model, the cast should be TableResultSet */ -class ResultSet : std::enable_shared_from_this { +class ResultSet : public std::enable_shared_from_this { public: ResultSet() {} virtual ~ResultSet() {} @@ -361,19 +361,19 @@ inline ResultSetIterator ResultSet::iterator() { MAYBE_UNUSED static void print_table_result_set( storage::ResultSet* table_result_set) { if (table_result_set == nullptr) { - std::cout << "TableResultSet is nullptr" << std::endl; + std::cout << "TableResultSet is nullptr" << '\n'; return; } auto metadata = table_result_set->get_metadata(); if (metadata == nullptr) { - std::cout << "Metadata is nullptr" << std::endl; + std::cout << "Metadata is nullptr" << '\n'; return; } uint32_t column_count = metadata->get_column_count(); if (column_count == 0) { - std::cout << "No columns in result set" << std::endl; + std::cout << "No columns in result set" << '\n'; return; } @@ -383,7 +383,7 @@ MAYBE_UNUSED static void print_table_result_set( std::cout << "\t"; } } - std::cout << std::endl; + std::cout << '\n'; bool has_next = false; int row_count = 0; @@ -440,10 +440,10 @@ MAYBE_UNUSED static void print_table_result_set( std::cout << "\t"; } } - std::cout << std::endl; + std::cout << '\n'; row_count++; } - std::cout << "Total rows: " << row_count << std::endl; + std::cout << "Total rows: " << row_count << '\n'; } } // namespace storage diff --git a/cpp/src/reader/table_query_executor.cc b/cpp/src/reader/table_query_executor.cc index 8ecc1256d..772c5dfee 100644 --- a/cpp/src/reader/table_query_executor.cc +++ b/cpp/src/reader/table_query_executor.cc @@ -220,6 +220,7 @@ int TableQueryExecutor::query_on_tree( } std::vector col_schema; + col_schema.reserve(tag_columns.size()); for (auto const& tag : tag_columns) { col_schema.emplace_back(tag, common::TSDataType::STRING, common::ColumnCategory::TAG); diff --git a/cpp/src/reader/table_result_set.cc b/cpp/src/reader/table_result_set.cc index 63c598c61..99588dde1 100644 --- a/cpp/src/reader/table_result_set.cc +++ b/cpp/src/reader/table_result_set.cc @@ -36,7 +36,9 @@ void TableResultSet::init() { std::make_shared(column_names_, data_types_); } -TableResultSet::~TableResultSet() { close(); } +// Qualified call: close() is virtual, but a destructor must not dispatch to +// derived overrides. +TableResultSet::~TableResultSet() { TableResultSet::close(); } int TableResultSet::next(bool& has_next) { int ret = common::E_OK; diff --git a/cpp/src/reader/table_result_set.h b/cpp/src/reader/table_result_set.h index b42b69c3f..3d724c637 100644 --- a/cpp/src/reader/table_result_set.h +++ b/cpp/src/reader/table_result_set.h @@ -34,8 +34,8 @@ class TableResultSet : public ResultSet { std::vector data_types, int return_mode = RETURN_ROW) : tsblock_reader_(std::move(tsblock_reader)), - column_names_(column_names), - data_types_(data_types), + column_names_(std::move(column_names)), + data_types_(std::move(data_types)), return_mode_(return_mode) { init(); } diff --git a/cpp/src/reader/task/device_query_task.cc b/cpp/src/reader/task/device_query_task.cc index 6345c93fa..7bfe6bb11 100644 --- a/cpp/src/reader/task/device_query_task.cc +++ b/cpp/src/reader/task/device_query_task.cc @@ -31,7 +31,8 @@ DeviceQueryTask* DeviceQueryTask::create_device_query_task( return nullptr; } DeviceQueryTask* task = new (buf) DeviceQueryTask( - device_id, column_names, column_mapping, index_root, table_schema); + std::move(device_id), std::move(column_names), + std::move(column_mapping), index_root, std::move(table_schema)); return task; } diff --git a/cpp/src/reader/task/device_query_task.h b/cpp/src/reader/task/device_query_task.h index 75043e44b..a51a4ac1b 100644 --- a/cpp/src/reader/task/device_query_task.h +++ b/cpp/src/reader/task/device_query_task.h @@ -30,11 +30,11 @@ class DeviceQueryTask { std::shared_ptr column_mapping, MetaIndexNode* index_root, std::shared_ptr table_schema) - : device_id_(device_id), - column_names_(column_names), - column_mapping_(column_mapping), + : device_id_(std::move(device_id)), + column_names_(std::move(column_names)), + column_mapping_(std::move(column_mapping)), index_root_(index_root), - table_schema_(table_schema) {} + table_schema_(std::move(table_schema)) {} ~DeviceQueryTask(); static DeviceQueryTask* create_device_query_task( diff --git a/cpp/src/reader/task/device_task_iterator.h b/cpp/src/reader/task/device_task_iterator.h index ad86dbc1d..1faaadc3f 100644 --- a/cpp/src/reader/task/device_task_iterator.h +++ b/cpp/src/reader/task/device_task_iterator.h @@ -36,11 +36,11 @@ class DeviceTaskIterator { IMetadataQuerier* metadata_querier, const Filter* id_filter, std::shared_ptr table_schema) - : column_names_(column_names), - column_mapping_(column_mapping), + : column_names_(std::move(column_names)), + column_mapping_(std::move(column_mapping)), device_meta_iterator_( metadata_querier->device_iterator(index_root, id_filter)), - table_schema_(table_schema) { + table_schema_(std::move(table_schema)) { pa_.init(512, common::MOD_DEVICE_TASK_ITER); } @@ -50,11 +50,11 @@ class DeviceTaskIterator { IMetadataQuerier* metadata_querier, const Filter* id_filter, std::shared_ptr table_schema) - : column_names_(column_names), - column_mapping_(column_mapping), - device_meta_iterator_( - metadata_querier->device_iterator(index_roots, id_filter)), - table_schema_(table_schema) { + : column_names_(std::move(column_names)), + column_mapping_(std::move(column_mapping)), + device_meta_iterator_(metadata_querier->device_iterator( + std::move(index_roots), id_filter)), + table_schema_(std::move(table_schema)) { pa_.init(512, common::MOD_DEVICE_TASK_ITER); } diff --git a/cpp/src/reader/tsfile_reader.cc b/cpp/src/reader/tsfile_reader.cc index 4a39a9f23..fb538db6a 100644 --- a/cpp/src/reader/tsfile_reader.cc +++ b/cpp/src/reader/tsfile_reader.cc @@ -56,7 +56,7 @@ int parse_paths(const std::vector& path_list, } int get_all_device_entries(std::vector& entries, - std::shared_ptr index_node, + const std::shared_ptr& index_node, RandomAccessReadFile* read_file, PageArena& pa) { int ret = E_OK; if (index_node == nullptr) { @@ -586,7 +586,7 @@ std::vector> TsFileReader::get_all_devices( // The error code is ignored to preserve the legacy interface. // New code should use the error-reporting overload and check its return // code. - get_all_devices(table_name, device_ids); + get_all_devices(std::move(table_name), device_ids); return device_ids; } @@ -650,7 +650,7 @@ int TsFileReader::get_all_devices( int TsFileReader::get_all_devices( std::vector>& device_ids, - std::shared_ptr index_node, PageArena& pa) { + const std::shared_ptr& index_node, PageArena& pa) { int ret = E_OK; if (index_node != nullptr) { if (index_node->node_type_ == LEAF_DEVICE) { @@ -744,6 +744,7 @@ int read_chunk_header_codec(RandomAccessReadFile* read_file, } // namespace int TsFileReader::get_timeseries_schema( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI std::shared_ptr device_id, std::vector& result) { int ret = E_OK; @@ -752,7 +753,7 @@ int TsFileReader::get_timeseries_schema( pa.init(512, MOD_TSFILE_READER); if (RET_FAIL(tsfile_executor_->get_tsfile_io_reader() ->get_device_timeseries_meta_without_chunk_meta( - device_id, timeseries_indexs, pa))) { + std::move(device_id), timeseries_indexs, pa))) { } else { for (auto timeseries_index : timeseries_indexs) { // AlignedTimeseriesIndex::get_data_type() returns the time @@ -807,7 +808,7 @@ int TsFileReader::get_timeseries_schema( } int TsFileReader::get_timeseries_metadata_impl( - std::shared_ptr device_id, + const std::shared_ptr& device_id, std::vector>& result) { int ret = E_OK; std::vector timeseries_indexs; @@ -916,6 +917,7 @@ int TsFileReader::get_timeseries_metadata(DeviceTimeseriesMetadataMap& result) { return ret; } else { std::vector> list; + list.reserve(raw_ts_indexes.size()); for (auto ts_idx : raw_ts_indexes) { list.emplace_back( std::shared_ptr(ts_idx, noop_deleter)); diff --git a/cpp/src/reader/tsfile_reader.h b/cpp/src/reader/tsfile_reader.h index 6f2c7d340..61aa858c1 100644 --- a/cpp/src/reader/tsfile_reader.h +++ b/cpp/src/reader/tsfile_reader.h @@ -347,10 +347,10 @@ class TsFileReader { unsigned char file_version); int ensure_table_query_executor(int batch_size); int get_timeseries_metadata_impl( - std::shared_ptr device_id, + const std::shared_ptr& device_id, std::vector>& result); int get_all_devices(std::vector>& device_ids, - std::shared_ptr index_node, + const std::shared_ptr& index_node, common::PageArena& pa); std::unique_ptr read_file_; storage::TsFileExecutor* tsfile_executor_; diff --git a/cpp/src/reader/tsfile_series_scan_iterator.cc b/cpp/src/reader/tsfile_series_scan_iterator.cc index ba7834e0b..e6a7c56f9 100644 --- a/cpp/src/reader/tsfile_series_scan_iterator.cc +++ b/cpp/src/reader/tsfile_series_scan_iterator.cc @@ -284,7 +284,10 @@ int TsFileSeriesScanIterator::get_next(TsBlock*& ret_tsblock, bool alloc, bool force_load_next_chunk = false; while (true) { if (!chunk_reader_->has_more_data() || force_load_next_chunk) { - force_load_next_chunk = false; + // Defensive reset; the flag is only re-read through the outer + // condition after being set again below. + force_load_next_chunk = + false; // NOLINT(clang-analyzer-deadcode.DeadStores) while (true) { if (!has_next_chunk()) { return E_NO_MORE_DATA; diff --git a/cpp/src/reader/tsfile_series_scan_iterator.h b/cpp/src/reader/tsfile_series_scan_iterator.h index 44cca99b5..fb5b44d2e 100644 --- a/cpp/src/reader/tsfile_series_scan_iterator.h +++ b/cpp/src/reader/tsfile_series_scan_iterator.h @@ -66,7 +66,7 @@ class TsFileSeriesScanIterator { common::PageArena& data_pa, AlignedFieldFilter* field_filter = nullptr) { ASSERT(read_file != nullptr); - device_id_ = device_id; + device_id_ = std::move(device_id); measurement_name_ = measurement_name; read_file_ = read_file; time_filter_ = time_filter; diff --git a/cpp/src/reader/tsfile_tree_reader.cc b/cpp/src/reader/tsfile_tree_reader.cc index 6d7b77e1b..54ebe79b7 100644 --- a/cpp/src/reader/tsfile_tree_reader.cc +++ b/cpp/src/reader/tsfile_tree_reader.cc @@ -76,7 +76,8 @@ std::vector TsFileTreeReader::get_device_schema( std::vector TsFileTreeReader::get_all_device_ids() { std::vector ret_device_ids; auto device_ids = tsfile_reader_->get_all_device_ids(); - for (auto device_id : device_ids) { + ret_device_ids.reserve(device_ids.size()); + for (const auto& device_id : device_ids) { ret_device_ids.emplace_back(device_id->get_device_name()); } return ret_device_ids; diff --git a/cpp/src/writer/tsfile_tree_writer.cc b/cpp/src/writer/tsfile_tree_writer.cc index e3f1a79bb..4a5a4e45b 100644 --- a/cpp/src/writer/tsfile_tree_writer.cc +++ b/cpp/src/writer/tsfile_tree_writer.cc @@ -46,6 +46,7 @@ int TsFileTreeWriter::register_timeseries(std::string& device_id, } int TsFileTreeWriter::register_timeseries( + // NOLINTNEXTLINE(performance-unnecessary-value-param) // public ABI std::string& device_id, std::vector schemas) { return tsfile_writer_->register_aligned_timeseries(device_id, schemas); } diff --git a/cpp/src/writer/tsfile_writer.cc b/cpp/src/writer/tsfile_writer.cc index 5e8cbb683..5677e8f47 100644 --- a/cpp/src/writer/tsfile_writer.cc +++ b/cpp/src/writer/tsfile_writer.cc @@ -170,7 +170,8 @@ int TsFileWriter::init(std::unique_ptr write_file) { if (ret != E_OK) { return ret; } - write_file.release(); + // release() yields the raw pointer that init() already stored. + write_file.release(); // NOLINT(bugprone-unused-return-value) if (previous != nullptr && previous != raw) { delete previous; } @@ -380,7 +381,8 @@ int TsFileWriter::register_aligned_timeseries( } } schemas_.insert(std::make_pair(id, group.get())); - group.release(); + // Ownership is tracked through schemas_; the pointer is stored. + group.release(); // NOLINT(bugprone-unused-return-value) return E_OK; } @@ -547,7 +549,7 @@ std::shared_ptr TsFileWriter::get_table_schema( template int TsFileWriter::do_check_schema( - std::shared_ptr device_id, + const std::shared_ptr& device_id, MeasurementNamesGetter& measurement_names, SimpleVector& chunk_writers, SimpleVector& data_types) { @@ -619,7 +621,7 @@ int TsFileWriter::ensure_aligned_value_chunk_writer( template int TsFileWriter::do_check_schema_aligned( - std::shared_ptr device_id, + const std::shared_ptr& device_id, MeasurementNamesGetter& measurement_names, storage::TimeChunkWriter*& time_chunk_writer, common::SimpleVector& value_chunk_writers, @@ -661,7 +663,7 @@ int TsFileWriter::do_check_schema_aligned( } int TsFileWriter::do_check_schema_table( - std::shared_ptr device_id, Tablet& tablet, + const std::shared_ptr& device_id, Tablet& tablet, storage::TimeChunkWriter*& time_chunk_writer, common::SimpleVector& value_chunk_writers) { int ret = E_OK; diff --git a/cpp/src/writer/tsfile_writer.h b/cpp/src/writer/tsfile_writer.h index 953cc67e1..934299c31 100644 --- a/cpp/src/writer/tsfile_writer.h +++ b/cpp/src/writer/tsfile_writer.h @@ -178,20 +178,20 @@ class TsFileWriter { template int do_check_schema( - std::shared_ptr device_id, + const std::shared_ptr& device_id, MeasurementNamesGetter& measurement_names, common::SimpleVector& chunk_writers, common::SimpleVector& data_types); template int do_check_schema_aligned( - std::shared_ptr device_id, + const std::shared_ptr& device_id, MeasurementNamesGetter& measurement_names, storage::TimeChunkWriter*& time_chunk_writer, common::SimpleVector& value_chunk_writers, common::SimpleVector& data_types); int do_check_schema_table( - std::shared_ptr device_id, Tablet& tablet, + const std::shared_ptr& device_id, Tablet& tablet, storage::TimeChunkWriter*& time_chunk_writer, common::SimpleVector& value_chunk_writers); diff --git a/cpp/tools/commands/row_query.cc b/cpp/tools/commands/row_query.cc index 04908a8df..5954d69ec 100644 --- a/cpp/tools/commands/row_query.cc +++ b/cpp/tools/commands/row_query.cc @@ -44,6 +44,7 @@ class CliFullMatchTagRegExp : public storage::TagFilter { CliFullMatchTagRegExp(int col_idx, const std::string& pattern) : storage::TagFilter(col_idx, pattern), pattern_(pattern) {} + // NOLINTNEXTLINE(performance-unnecessary-value-param) // virtual ABI bool satisfyRow(std::vector segments) const override { if (col_idx_ >= segments.size() || segments[col_idx_] == nullptr) { return false;