diff --git a/src/Access/examples/kerberos_init.cpp b/src/Access/examples/kerberos_init.cpp index 5dbe92a5b57..f9f9d882ece 100644 --- a/src/Access/examples/kerberos_init.cpp +++ b/src/Access/examples/kerberos_init.cpp @@ -25,7 +25,7 @@ int main(int argc, char ** argv) return 0; } - String cache_name = ""; + const char * cache_name = ""; if (argc == 4) cache_name = argv[3]; diff --git a/src/Common/CompactArray.h b/src/Common/CompactArray.h index cf97206edb8..c2dd696d651 100644 --- a/src/Common/CompactArray.h +++ b/src/Common/CompactArray.h @@ -130,7 +130,7 @@ public: private: ReadBuffer & in; /// The physical location of the current cell. - Locus locus; + Locus locus{}; /// The current position in the file as a cell number. BucketIndex current_bucket_index = 0; /// The number of bytes read. diff --git a/src/Core/examples/coro.cpp b/src/Core/examples/coro.cpp index 956b4e988e4..370820a228d 100644 --- a/src/Core/examples/coro.cpp +++ b/src/Core/examples/coro.cpp @@ -12,7 +12,7 @@ #if defined(__clang__) #include -namespace std +namespace std // NOLINT(cert-dcl58-cpp) { using namespace experimental::coroutines_v1; } @@ -97,14 +97,14 @@ struct Task static bool resumeImpl(Task *r) { if (r->value) - return false; + return false; auto & next = r->my.promise().next; if (next) { if (resumeImpl(next.promise().r)) - return true; + return true; next = {}; } @@ -183,7 +183,7 @@ int main() auto t = foo("foo"); std::cout << ".. started" << std::endl; while (t.resume()) - std::cout << ".. yielded" << std::endl; + std::cout << ".. yielded" << std::endl; std::cout << ".. done: " << t.res() << std::endl; } catch (DB::Exception & e) diff --git a/src/Interpreters/examples/hash_map3.cpp b/src/Interpreters/examples/hash_map3.cpp index a8673e06e9b..110dcab3f76 100644 --- a/src/Interpreters/examples/hash_map3.cpp +++ b/src/Interpreters/examples/hash_map3.cpp @@ -1,13 +1,9 @@ -#include - #define DBMS_HASH_MAP_DEBUG_RESIZES #define DBMS_HASH_MAP_COUNT_COLLISIONS - -#include - +#include +#include #include - #include #include diff --git a/src/Storages/examples/get_current_inserts_in_replicated.cpp b/src/Storages/examples/get_current_inserts_in_replicated.cpp index d7dedbcab9c..0d04cbd260e 100644 --- a/src/Storages/examples/get_current_inserts_in_replicated.cpp +++ b/src/Storages/examples/get_current_inserts_in_replicated.cpp @@ -85,7 +85,7 @@ try for (BlockInfo & block : block_infos) { Coordination::GetResponse resp = block.contents_future.get(); - if (resp.error == Coordination::Error::ZOK && lock_holder_paths.count(resp.data)) + if (resp.error == Coordination::Error::ZOK && lock_holder_paths.contains(resp.data)) { ++total_count; current_inserts[block.partition].insert(block.number); diff --git a/utils/compressor/decompress_perf.cpp b/utils/compressor/decompress_perf.cpp index 20f974aab7f..e3210164d79 100644 --- a/utils/compressor/decompress_perf.cpp +++ b/utils/compressor/decompress_perf.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include @@ -60,7 +60,7 @@ protected: compressed_in->readStrict(reinterpret_cast(&checksum), sizeof(checksum)); own_compressed_buffer.resize(COMPRESSED_BLOCK_HEADER_SIZE); - compressed_in->readStrict(&own_compressed_buffer[0], COMPRESSED_BLOCK_HEADER_SIZE); + compressed_in->readStrict(own_compressed_buffer.data(), COMPRESSED_BLOCK_HEADER_SIZE); UInt8 method = own_compressed_buffer[0]; /// See CompressedWriteBuffer.h @@ -90,7 +90,7 @@ protected: else { own_compressed_buffer.resize(size_compressed + (variant == LZ4_REFERENCE ? 0 : LZ4::ADDITIONAL_BYTES_AT_END_OF_BUFFER)); - compressed_buffer = &own_compressed_buffer[0]; + compressed_buffer = own_compressed_buffer.data(); compressed_in->readStrict(compressed_buffer + COMPRESSED_BLOCK_HEADER_SIZE, size_compressed - COMPRESSED_BLOCK_HEADER_SIZE); } @@ -143,7 +143,7 @@ private: return false; memory.resize(size_decompressed + LZ4::ADDITIONAL_BYTES_AT_END_OF_BUFFER); - working_buffer = Buffer(&memory[0], &memory[size_decompressed]); + working_buffer = Buffer(memory.data(), &memory[size_decompressed]); decompress(working_buffer.begin(), size_decompressed, size_compressed_without_checksum); diff --git a/utils/db-generator/query_db_generator.cpp b/utils/db-generator/query_db_generator.cpp index 18c9bdbca38..00785af89f7 100644 --- a/utils/db-generator/query_db_generator.cpp +++ b/utils/db-generator/query_db_generator.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -59,8 +59,8 @@ std::string randomDate() int32_t month = rng() % 12 + 1; int32_t day = rng() % 12 + 1; char answer[13]; - sprintf(answer, "'%04u-%02u-%02u'", year, month, day); - return std::string(answer); + size_t size = sprintf(answer, "'%04u-%02u-%02u'", year, month, day); + return std::string(answer, size); } std::string randomDatetime() @@ -72,7 +72,7 @@ std::string randomDatetime() int32_t minutes = rng() % 60; int32_t seconds = rng() % 60; char answer[22]; - sprintf( + size_t size = sprintf( answer, "'%04u-%02u-%02u %02u:%02u:%02u'", year, @@ -81,7 +81,7 @@ std::string randomDatetime() hours, minutes, seconds); - return std::string(answer); + return std::string(answer, size); } TableAndColumn get_table_a_column(const std::string & c) { diff --git a/utils/graphite-rollup/graphite-rollup-bench.cpp b/utils/graphite-rollup/graphite-rollup-bench.cpp index 49a3d509be6..a46d214edbf 100644 --- a/utils/graphite-rollup/graphite-rollup-bench.cpp +++ b/utils/graphite-rollup/graphite-rollup-bench.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include @@ -15,50 +14,35 @@ #include #include #include +#include +#include using namespace DB; static SharedContextHolder shared_context = Context::createShared(); -std::vector loadMetrics(const std::string & metrics_file) +auto loadMetrics(const std::string & metrics_file) { - std::vector metrics; + std::vector metrics; + ReadBufferFromFile in(metrics_file); + String line; - FILE * stream; - char * line = nullptr; - size_t len = 0; - ssize_t nread; - - stream = fopen(metrics_file.c_str(), "r"); - if (stream == nullptr) + while (!in.eof()) { - throw std::runtime_error(strerror(errno)); - } - - while ((nread = getline(&line, &len, stream)) != -1) /// NOLINT - { - size_t l = strlen(line); - if (l > 0) + readEscapedStringUntilEOL(line, in); + if (!in.eof()) { - if (line[l - 1] == '\n') - { - line[l - 1] = '\0'; - l--; - } - if (l > 0) - { - metrics.emplace_back(std::string_view(strdup(line), l)); - } + ++in.position(); + } + if (!line.empty() && line.back() == '\n') + { + line.pop_back(); + } + if (!line.empty()) + { + metrics.emplace_back(line); } } - free(line); - if (ferror(stream)) - { - fclose(stream); - throw std::runtime_error(strerror(errno)); - } - - fclose(stream); return metrics; } @@ -80,7 +64,7 @@ void bench(const std::string & config_path, const std::string & metrics_file, si Graphite::Params params; setGraphitePatternsFromConfig(context, "graphite_rollup", params); - std::vector metrics = loadMetrics(metrics_file); + auto metrics = loadMetrics(metrics_file); std::vector durations(metrics.size()); size_t j, i; @@ -99,15 +83,14 @@ void bench(const std::string & config_path, const std::string & metrics_file, si if (j == 0 && verbose) { - std::cout << metrics[i].data() << ": rule with regexp '" << rule.second->regexp_str << "' found\n"; + std::cout << metrics[i] << ": rule with regexp '" << rule.second->regexp_str << "' found\n"; } } } for (i = 0; i < metrics.size(); i++) { - std::cout << metrics[i].data() << " " << durations[i] / n << " ns\n"; - free(const_cast(static_cast(metrics[i].data()))); + std::cout << metrics[i] << " " << durations[i] / n << " ns\n"; } } diff --git a/utils/iotest/iotest.cpp b/utils/iotest/iotest.cpp index f23ee6929fa..7a1f35ddd52 100644 --- a/utils/iotest/iotest.cpp +++ b/utils/iotest/iotest.cpp @@ -15,8 +15,7 @@ #include #include -#include -#include +#include #include @@ -54,7 +53,7 @@ void thread(int fd, int mode, size_t min_offset, size_t max_offset, size_t block if ((mode & MODE_DIRECT)) buf = direct_buf.data(); else - buf = &simple_buf[0]; + buf = simple_buf.data(); pcg64 rng(randomSeed()); diff --git a/utils/iotest/iotest_aio.cpp b/utils/iotest/iotest_aio.cpp index 05074bc85f4..c0cf002ce58 100644 --- a/utils/iotest/iotest_aio.cpp +++ b/utils/iotest/iotest_aio.cpp @@ -110,12 +110,12 @@ void thread(int fd, int mode, size_t min_offset, size_t max_offset, size_t block } /// Send queries. - if (io_submit(ctx.ctx, query_cbs.size(), &query_cbs[0]) < 0) + if (io_submit(ctx.ctx, query_cbs.size(), query_cbs.data()) < 0) throwFromErrno("io_submit failed", ErrorCodes::CANNOT_IO_SUBMIT); /// Receive answers. If we have something else to send, then receive at least one answer (after that send them), otherwise wait all answers. - memset(&events[0], 0, buffers_count * sizeof(events[0])); - int evs = io_getevents(ctx.ctx, (blocks_sent < count ? 1 : in_progress), buffers_count, &events[0], nullptr); + memset(events.data(), 0, buffers_count * sizeof(events[0])); + int evs = io_getevents(ctx.ctx, (blocks_sent < count ? 1 : in_progress), buffers_count, events.data(), nullptr); if (evs < 0) throwFromErrno("io_getevents failed", ErrorCodes::CANNOT_IO_GETEVENTS); diff --git a/utils/iotest/iotest_nonblock.cpp b/utils/iotest/iotest_nonblock.cpp index 430e951d36b..33fab4d04e6 100644 --- a/utils/iotest/iotest_nonblock.cpp +++ b/utils/iotest/iotest_nonblock.cpp @@ -13,8 +13,8 @@ #include #include -#include -#include +#include +#include #include #if defined (OS_LINUX) @@ -101,7 +101,7 @@ int mainImpl(int argc, char ** argv) size_t ops = 0; while (ops < count) { - if (poll(&polls[0], descriptors, -1) <= 0) + if (poll(polls.data(), descriptors, -1) <= 0) throwFromErrno("poll failed", ErrorCodes::SYSTEM_ERROR); for (size_t i = 0; i < descriptors; ++i) { @@ -123,12 +123,12 @@ int mainImpl(int argc, char ** argv) if (mode == MODE_READ) { - if (static_cast(block_size) != pread(fds[i], &buf[0], block_size, offset)) + if (static_cast(block_size) != pread(fds[i], buf.data(), block_size, offset)) throwFromErrno("Cannot read", ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR); } else { - if (static_cast(block_size) != pwrite(fds[i], &buf[0], block_size, offset)) + if (static_cast(block_size) != pwrite(fds[i], buf.data(), block_size, offset)) throwFromErrno("Cannot write", ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR); } } diff --git a/utils/zookeeper-adjust-block-numbers-to-parts/main.cpp b/utils/zookeeper-adjust-block-numbers-to-parts/main.cpp index 5c694ee04ef..7736921a9c6 100644 --- a/utils/zookeeper-adjust-block-numbers-to-parts/main.cpp +++ b/utils/zookeeper-adjust-block-numbers-to-parts/main.cpp @@ -179,8 +179,8 @@ void setCurrentBlockNumber(zkutil::ZooKeeper & zk, const std::string & path, Int if (number != current_block_number) { char suffix[11] = ""; - sprintf(suffix, "%010lld", current_block_number); - std::string expected_path = block_prefix + suffix; + size_t size = sprintf(suffix, "%010lld", current_block_number); + std::string expected_path = block_prefix + std::string(suffix, size); std::cerr << "\t" << path_created << ": Ephemeral node has been created with an unexpected path (expected something like " << expected_path << ")." << std::endl; return false; diff --git a/utils/zookeeper-test/main.cpp b/utils/zookeeper-test/main.cpp index 5d0b54aa74b..2d5f2ba34ab 100644 --- a/utils/zookeeper-test/main.cpp +++ b/utils/zookeeper-test/main.cpp @@ -238,9 +238,9 @@ std::string currentDateTime() tstruct = *localtime(&now); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tstruct); + size_t size = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tstruct); - return buf; + return std::string(buf, size); }