From 44fe79fd424cf3f8fb46e7ca09ab2f8e613ca825 Mon Sep 17 00:00:00 2001 From: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Date: Sat, 11 Feb 2023 10:03:35 -0600 Subject: [PATCH] Run static analysis in CI (#129) * Run static analysis in CI * Remove analysis-report in make clean * Add remark * Remove the memory helper functions * Revert "Remove the memory helper functions" This reverts commit 364234aea02cca38ed40a5bce1bbc8f8eb02aee2. * Remove swap file --- .github/workflows/ckzg-test.yml | 4 ++++ src/Makefile | 7 +++++++ src/c_kzg_4844.c | 14 ++++++++------ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ckzg-test.yml b/.github/workflows/ckzg-test.yml index b25398d..af9e62e 100644 --- a/.github/workflows/ckzg-test.yml +++ b/.github/workflows/ckzg-test.yml @@ -23,6 +23,10 @@ jobs: run: | cd src make test + - name: Clang Static Analyzer + run: | + cd src + make analyze - name: Install LLVM uses: egor-tensin/setup-clang@v1 - name: Generate coverage report diff --git a/src/Makefile b/src/Makefile index 23e5d38..1662ad4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -91,10 +91,17 @@ profile: \ profile_compute_kzg_proof \ profile_compute_aggregate_kzg_proof +.PHONY: analyze +analyze: c_kzg_4844.c + @$(CC) --analyze -Xanalyzer -analyzer-output=html \ + -o analysis-report $(CFLAGS) -c $< + @[ -d analysis-report ] && exit 1 || exit 0 + .PHONY: clean clean: @rm -f *.o *.profraw *.profdata *.html xray-log.* *.prof *.pdf \ test_c_kzg_4844 test_c_kzg_4844_cov test_c_kzg_4844_prof + @rm -rf analysis-report .PHONY: format format: diff --git a/src/c_kzg_4844.c b/src/c_kzg_4844.c index d31ffda..9602751 100644 --- a/src/c_kzg_4844.c +++ b/src/c_kzg_4844.c @@ -145,16 +145,15 @@ static const fr_t FR_ONE = { /** * Wrapped `malloc()` that reports failures to allocate. * + * @remark Will return C_KZG_BADARGS if the requested size is zero. + * * @param[out] x Pointer to the allocated space * @param[in] n The number of bytes to be allocated */ static C_KZG_RET c_kzg_malloc(void **x, size_t n) { - if (n > 0) { - *x = malloc(n); - return *x != NULL ? C_KZG_OK : C_KZG_MALLOC; - } - *x = NULL; - return C_KZG_OK; + if (n == 0) return C_KZG_BADARGS; + *x = malloc(n); + return *x != NULL ? C_KZG_OK : C_KZG_MALLOC; } /** @@ -564,6 +563,7 @@ static C_KZG_RET bit_reversal_permutation( ) { CHECK(n >> 32 == 0); CHECK(is_power_of_two(n)); + CHECK(log2_pow2(n) != 0); // Pointer arithmetic on `void *` is naughty, so cast to something // definite @@ -1601,6 +1601,8 @@ C_KZG_RET load_trusted_setup( out->g1_values = NULL; out->g2_values = NULL; + CHECK(n1 > 0); + CHECK(n2 > 0); ret = new_g1_array(&out->g1_values, n1); if (ret != C_KZG_OK) goto out_error; ret = new_g2_array(&out->g2_values, n2);