From 6a239a3bdec66d2319df5c2e9fa467992f5c3cf9 Mon Sep 17 00:00:00 2001 From: wangyunlai Date: Wed, 13 Sep 2023 20:08:12 +0800 Subject: [PATCH] Create codeql.yml (#265) ### What problem were solved in this pull request? Problem: add code quality scan. --- .github/workflows/codeql.yml | 87 +++++++++++++++++++ build.sh | 3 +- deps/common/log/log.cpp | 2 +- deps/common/mm/mem_pool.cpp | 2 +- src/observer/storage/index/bplus_tree.cpp | 22 ++--- src/observer/storage/persist/persist.cpp | 3 - .../storage/record/record_manager.cpp | 4 +- unittest/persist_test.cpp | 3 - 8 files changed, 104 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/codeql.yml diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..8070ca3 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,87 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ "main" ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ "main" ] + schedule: + - cron: '37 12 * * 5' + +jobs: + analyze: + name: Analyze + # Runner size impacts CodeQL analysis time. To learn more, please see: + # - https://gh.io/recommended-hardware-resources-for-running-codeql + # - https://gh.io/supported-runners-and-hardware-resources + # - https://gh.io/using-larger-runners + # Consider using larger runners for possible analysis time improvements. + runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} + timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 600 }} + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'cpp' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby', 'swift' ] + # Use only 'java' to analyze code written in Java, Kotlin or both + # Use only 'javascript' to analyze code written in JavaScript, TypeScript or both + # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + + - name: Build init + run: sudo bash build.sh init + + # Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v2 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + + # If the Autobuild fails above, remove it and uncomment the following three lines. + # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. + + # - name: Build C++ + # run: | + # echo "Run, Build Application using script" + # sudo bash build.sh init + # bash build.sh + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 + with: + category: "/language:${{matrix.language}}" diff --git a/build.sh b/build.sh index 7dfa01c..cf589de 100755 --- a/build.sh +++ b/build.sh @@ -57,7 +57,8 @@ function try_make { if [[ $MAKE != false ]] then - $MAKE "${MAKE_ARGS[@]}" + # use single thread `make` if concurrent building failed + $MAKE "${MAKE_ARGS[@]}" || $MAKE fi } diff --git a/deps/common/log/log.cpp b/deps/common/log/log.cpp index f3324ec..f9c2a85 100644 --- a/deps/common/log/log.cpp +++ b/deps/common/log/log.cpp @@ -373,7 +373,7 @@ const char *lbt() int size = backtrace(buffer, buffer_size); int offset = 0; - for (int i = 0; i < size; i++) { + for (int i = 0; i < size && offset < bt_buffer_size - 1; i++) { const char *format = (0 == i) ? "0x%lx" : " 0x%lx"; offset += snprintf(backtrace_buffer + offset, sizeof(backtrace_buffer) - offset, format, reinterpret_cast(buffer[i])); diff --git a/deps/common/mm/mem_pool.cpp b/deps/common/mm/mem_pool.cpp index c5f6256..6f27096 100644 --- a/deps/common/mm/mem_pool.cpp +++ b/deps/common/mm/mem_pool.cpp @@ -81,7 +81,7 @@ int MemPoolItem::extend() } MUTEX_LOCK(&this->mutex); - void *pool = malloc(item_num_per_pool * item_size); + void *pool = malloc(static_cast(item_num_per_pool) * item_size); if (pool == nullptr) { MUTEX_UNLOCK(&this->mutex); LOG_ERROR("Failed to extend memory pool, this->size:%d, item_num_per_pool:%d, this->name:%s.", diff --git a/src/observer/storage/index/bplus_tree.cpp b/src/observer/storage/index/bplus_tree.cpp index 590b9b3..7f5337c 100644 --- a/src/observer/storage/index/bplus_tree.cpp +++ b/src/observer/storage/index/bplus_tree.cpp @@ -211,7 +211,7 @@ int LeafIndexNodeHandler::lookup(const KeyComparator &comparator, const char *ke void LeafIndexNodeHandler::insert(int index, const char *key, const char *value) { if (index < size()) { - memmove(__item_at(index + 1), __item_at(index), (size() - index) * item_size()); + memmove(__item_at(index + 1), __item_at(index), (static_cast(size()) - index) * item_size()); } memcpy(__item_at(index), key, key_size()); memcpy(__item_at(index) + key_size(), value, value_size()); @@ -221,7 +221,7 @@ void LeafIndexNodeHandler::remove(int index) { assert(index >= 0 && index < size()); if (index < size() - 1) { - memmove(__item_at(index), __item_at(index + 1), (size() - index - 1) * item_size()); + memmove(__item_at(index), __item_at(index + 1), (static_cast(size()) - index - 1) * item_size()); } increase_size(-1); } @@ -242,7 +242,7 @@ RC LeafIndexNodeHandler::move_half_to(LeafIndexNodeHandler &other, DiskBufferPoo const int size = this->size(); const int move_index = size / 2; - memcpy(other.__item_at(0), this->__item_at(move_index), item_size() * (size - move_index)); + memcpy(other.__item_at(0), this->__item_at(move_index), static_cast(item_size()) * (size - move_index)); other.increase_size(size - move_index); this->increase_size(-(size - move_index)); return RC::SUCCESS; @@ -252,7 +252,7 @@ RC LeafIndexNodeHandler::move_first_to_end(LeafIndexNodeHandler &other, DiskBuff other.append(__item_at(0)); if (size() >= 1) { - memmove(__item_at(0), __item_at(1), (size() - 1) * item_size()); + memmove(__item_at(0), __item_at(1), (static_cast(size()) - 1) * item_size()); } increase_size(-1); return RC::SUCCESS; @@ -270,7 +270,7 @@ RC LeafIndexNodeHandler::move_last_to_front(LeafIndexNodeHandler &other, DiskBuf */ RC LeafIndexNodeHandler::move_to(LeafIndexNodeHandler &other, DiskBufferPool *bp) { - memcpy(other.__item_at(other.size()), this->__item_at(0), this->size() * item_size()); + memcpy(other.__item_at(other.size()), this->__item_at(0), static_cast(this->size()) * item_size()); other.increase_size(this->size()); this->increase_size(-this->size()); @@ -287,7 +287,7 @@ void LeafIndexNodeHandler::append(const char *item) void LeafIndexNodeHandler::preappend(const char *item) { if (size() > 0) { - memmove(__item_at(1), __item_at(0), size() * item_size()); + memmove(__item_at(1), __item_at(0), static_cast(size()) * item_size()); } memcpy(__item_at(0), item, item_size()); increase_size(1); @@ -424,7 +424,7 @@ void InternalIndexNodeHandler::insert(const char *key, PageNum page_num, const K int insert_position = -1; lookup(comparator, key, nullptr, &insert_position); if (insert_position < size()) { - memmove(__item_at(insert_position + 1), __item_at(insert_position), (size() - insert_position) * item_size()); + memmove(__item_at(insert_position + 1), __item_at(insert_position), (static_cast(size()) - insert_position) * item_size()); } memcpy(__item_at(insert_position), key, key_size()); memcpy(__value_at(insert_position), &page_num, value_size()); @@ -510,7 +510,7 @@ void InternalIndexNodeHandler::remove(int index) { assert(index >= 0 && index < size()); if (index < size() - 1) { - memmove(__item_at(index), __item_at(index + 1), (size() - index - 1) * item_size()); + memmove(__item_at(index), __item_at(index + 1), (static_cast(size()) - index - 1) * item_size()); } increase_size(-1); } @@ -536,7 +536,7 @@ RC InternalIndexNodeHandler::move_first_to_end(InternalIndexNodeHandler &other, } if (size() >= 1) { - memmove(__item_at(0), __item_at(1), (size() - 1) * item_size()); + memmove(__item_at(0), __item_at(1), (static_cast(size()) - 1) * item_size()); } increase_size(-1); return rc; @@ -558,7 +558,7 @@ RC InternalIndexNodeHandler::move_last_to_front(InternalIndexNodeHandler &other, */ RC InternalIndexNodeHandler::copy_from(const char *items, int num, DiskBufferPool *disk_buffer_pool) { - memcpy(__item_at(this->size()), items, num * item_size()); + memcpy(__item_at(this->size()), items, static_cast(num) * item_size()); RC rc = RC::SUCCESS; PageNum this_page_num = this->page_num(); @@ -602,7 +602,7 @@ RC InternalIndexNodeHandler::preappend(const char *item, DiskBufferPool *bp) bp->unpin_page(frame); if (this->size() > 0) { - memmove(__item_at(1), __item_at(0), this->size() * item_size()); + memmove(__item_at(1), __item_at(0), static_cast(this->size()) * item_size()); } memcpy(__item_at(0), item, item_size()); diff --git a/src/observer/storage/persist/persist.cpp b/src/observer/storage/persist/persist.cpp index fe4fbfb..2e9a96c 100644 --- a/src/observer/storage/persist/persist.cpp +++ b/src/observer/storage/persist/persist.cpp @@ -35,9 +35,6 @@ RC PersistHandler::create_file(const char *file_name) } else if (!file_name_.empty()) { LOG_ERROR("Failed to create %s, because a file is already bound.", file_name); rc = RC::FILE_BOUND; - } else if (access(file_name, F_OK) != -1) { - LOG_WARN("Failed to create %s, because file already exist.", file_name); - rc = RC::FILE_EXIST; } else { int fd; fd = open(file_name, O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE); diff --git a/src/observer/storage/record/record_manager.cpp b/src/observer/storage/record/record_manager.cpp index f53accb..2064869 100644 --- a/src/observer/storage/record/record_manager.cpp +++ b/src/observer/storage/record/record_manager.cpp @@ -156,7 +156,7 @@ RC RecordPageHandler::init_empty_page(DiskBufferPool &buffer_pool, PageNum page_ memset(bitmap_, 0, page_bitmap_size(page_header_->record_capacity)); if ((ret = buffer_pool.flush_page(*frame_)) != RC::SUCCESS) { - LOG_ERROR("Failed to flush page header %d:%d.", page_num); + LOG_ERROR("Failed to flush page header %d:%d.", buffer_pool.file_desc(), page_num); return ret; } @@ -183,7 +183,7 @@ RC RecordPageHandler::insert_record(const char *data, RID *rid) ASSERT(readonly_ == false, "cannot insert record into page while the page is readonly"); if (page_header_->record_num == page_header_->record_capacity) { - LOG_WARN("Page is full, page_num %d:%d.", frame_->page_num()); + LOG_WARN("Page is full, page_num %d:%d.", disk_buffer_pool_->file_desc(), frame_->page_num()); return RC::RECORD_NOMEM; } diff --git a/unittest/persist_test.cpp b/unittest/persist_test.cpp index f165885..880eb52 100644 --- a/unittest/persist_test.cpp +++ b/unittest/persist_test.cpp @@ -41,9 +41,6 @@ TEST(test_persist, test_persist_file_io) rc = persist_handler.create_file(file_name_2.c_str()); ASSERT_EQ(rc, RC::FILE_BOUND); - rc = persist_handler_2.create_file(file_name_1.c_str()); - ASSERT_EQ(rc, RC::FILE_EXIST); - rc = persist_handler_2.create_file(""); ASSERT_EQ(rc, RC::FILE_CREATE); -- GitLab