From 40da067b4f9e1bdb21dc5e5180262889006af02e Mon Sep 17 00:00:00 2001 From: Konstantinos Margaritis Date: Sat, 18 May 2024 21:48:40 +0300 Subject: [PATCH] Add more C style casts fixes and suppressions --- src/nfa/limex_compile.cpp | 20 ++++++++++---------- src/nfa/mcclellan_internal.h | 21 +++++++++++++-------- src/nfa/shengcompile.cpp | 35 ++++++++++++++++++----------------- src/nfa/tamaramacompile.cpp | 8 ++++---- src/nfagraph/ng_lbr.cpp | 2 +- src/parser/utf8_validate.cpp | 2 +- src/util/alloc.cpp | 2 +- src/util/uniform_ops.h | 29 +++++++++++++++++++++++++---- unit/hyperscan/extparam.cpp | 2 +- unit/hyperscan/som.cpp | 2 +- unit/internal/lbr.cpp | 2 +- util/database_util.cpp | 2 +- util/string_util.h | 2 +- 13 files changed, 78 insertions(+), 51 deletions(-) diff --git a/src/nfa/limex_compile.cpp b/src/nfa/limex_compile.cpp index 5b28e732..68b91e1a 100644 --- a/src/nfa/limex_compile.cpp +++ b/src/nfa/limex_compile.cpp @@ -2132,7 +2132,7 @@ struct Factory { const vector &squash, implNFA_t *limex, const u32 acceptsOffset, const u32 acceptsEodOffset, const u32 squashOffset, const u32 reportListOffset) { - char *limex_base = (char *)limex; + char *limex_base = reinterpret_cast(limex); DEBUG_PRINTF("acceptsOffset=%u, acceptsEodOffset=%u, squashOffset=%u\n", acceptsOffset, acceptsEodOffset, squashOffset); @@ -2155,7 +2155,7 @@ struct Factory { limex->acceptOffset = acceptsOffset; limex->acceptCount = verify_u32(accepts.size()); DEBUG_PRINTF("NFA has %zu accepts\n", accepts.size()); - NFAAccept *acceptsTable = (NFAAccept *)(limex_base + acceptsOffset); + NFAAccept *acceptsTable = reinterpret_cast(limex_base + acceptsOffset); assert(ISALIGNED(acceptsTable)); transform(accepts.begin(), accepts.end(), acceptsTable, transform_offset_fn); @@ -2164,7 +2164,7 @@ struct Factory { limex->acceptEodOffset = acceptsEodOffset; limex->acceptEodCount = verify_u32(acceptsEod.size()); DEBUG_PRINTF("NFA has %zu EOD accepts\n", acceptsEod.size()); - NFAAccept *acceptsEodTable = (NFAAccept *)(limex_base + acceptsEodOffset); + NFAAccept *acceptsEodTable = reinterpret_cast(limex_base + acceptsEodOffset); assert(ISALIGNED(acceptsEodTable)); transform(acceptsEod.begin(), acceptsEod.end(), acceptsEodTable, transform_offset_fn); @@ -2173,7 +2173,7 @@ struct Factory { limex->squashCount = verify_u32(squash.size()); limex->squashOffset = squashOffset; DEBUG_PRINTF("NFA has %zu report squash masks\n", squash.size()); - tableRow_t *mask = (tableRow_t *)(limex_base + squashOffset); + tableRow_t *mask = reinterpret_cast(limex_base + squashOffset); assert(ISALIGNED(mask)); for (size_t i = 0, end = squash.size(); i < end; i++) { maskSetBits(mask[i], squash[i]); @@ -2195,13 +2195,13 @@ struct Factory { for (u32 i = 0; i < num_repeats; i++) { repeatOffsets[i] = offset; assert(repeats[i]); - memcpy((char *)limex + offset, repeats[i].get(), repeats[i].size()); + memcpy(reinterpret_cast(limex) + offset, repeats[i].get(), repeats[i].size()); offset += repeats[i].size(); } // Write repeat offset lookup table. - assert(ISALIGNED_N((char *)limex + repeatOffsetsOffset, alignof(u32))); - copy_bytes((char *)limex + repeatOffsetsOffset, repeatOffsets); + assert(ISALIGNED_N(reinterpret_cast(limex) + repeatOffsetsOffset, alignof(u32))); + copy_bytes(reinterpret_cast(limex) + repeatOffsetsOffset, repeatOffsets); limex->repeatOffset = repeatOffsetsOffset; limex->repeatCount = num_repeats; @@ -2211,9 +2211,9 @@ struct Factory { void writeReportList(const vector &reports, implNFA_t *limex, const u32 reportListOffset) { DEBUG_PRINTF("reportListOffset=%u\n", reportListOffset); - assert(ISALIGNED_N((char *)limex + reportListOffset, + assert(ISALIGNED_N(reinterpret_cast(limex) + reportListOffset, alignof(ReportID))); - copy_bytes((char *)limex + reportListOffset, reports); + copy_bytes(reinterpret_cast(limex) + reportListOffset, reports); } static @@ -2322,7 +2322,7 @@ struct Factory { auto nfa = make_zeroed_bytecode_ptr(nfaSize); assert(nfa); // otherwise we would have thrown std::bad_alloc - implNFA_t *limex = (implNFA_t *)getMutableImplNfa(nfa.get()); + implNFA_t *limex = reinterpret_cast(getMutableImplNfa(nfa.get())); assert(ISALIGNED(limex)); writeReachMapping(reach, reachMap, limex, reachOffset); diff --git a/src/nfa/mcclellan_internal.h b/src/nfa/mcclellan_internal.h index 482fdb1b..61b87200 100644 --- a/src/nfa/mcclellan_internal.h +++ b/src/nfa/mcclellan_internal.h @@ -106,9 +106,10 @@ static really_inline const char *findShermanState(UNUSED const struct mcclellan *m, const char *sherman_base_offset, u32 sherman_base, u32 s) { - const char *rv - = sherman_base_offset + SHERMAN_FIXED_SIZE * (s - sherman_base); + const char *rv = sherman_base_offset + SHERMAN_FIXED_SIZE * (s - sherman_base); + // cppcheck-suppress cstyleCast assert(rv < (const char *)m + m->length - sizeof(struct NFA)); + // cppcheck-suppress cstyleCast UNUSED u8 type = *(const u8 *)(rv + SHERMAN_TYPE_OFFSET); assert(type == SHERMAN_STATE); return rv; @@ -123,13 +124,15 @@ char *findMutableShermanState(char *sherman_base_offset, u16 sherman_base, static really_inline const char *findWideEntry8(UNUSED const struct mcclellan *m, const char *wide_base, u32 wide_limit, u32 s) { + // cppcheck-suppress cstyleCast UNUSED u8 type = *(const u8 *)wide_base; assert(type == WIDE_STATE); - const u32 entry_offset - = *(const u32 *)(wide_base + // cppcheck-suppress cstyleCast + const u32 entry_offset = *(const u32 *)(wide_base + WIDE_ENTRY_OFFSET8((s - wide_limit) * sizeof(u32))); const char *rv = wide_base + entry_offset; + // cppcheck-suppress cstyleCast assert(rv < (const char *)m + m->length - sizeof(struct NFA)); return rv; } @@ -137,21 +140,23 @@ const char *findWideEntry8(UNUSED const struct mcclellan *m, static really_inline const char *findWideEntry16(UNUSED const struct mcclellan *m, const char *wide_base, u32 wide_limit, u32 s) { + // cppcheck-suppress cstyleCast UNUSED u8 type = *(const u8 *)wide_base; assert(type == WIDE_STATE); - const u32 entry_offset - = *(const u32 *)(wide_base + // cppcheck-suppress cstyleCast + const u32 entry_offset = *(const u32 *)(wide_base + WIDE_ENTRY_OFFSET16((s - wide_limit) * sizeof(u32))); const char *rv = wide_base + entry_offset; + // cppcheck-suppress cstyleCast assert(rv < (const char *)m + m->length - sizeof(struct NFA)); return rv; } static really_inline char *findMutableWideEntry16(char *wide_base, u32 wide_limit, u32 s) { - u32 entry_offset - = *(const u32 *)(wide_base + // cppcheck-suppress cstyleCast + u32 entry_offset = *(const u32 *)(wide_base + WIDE_ENTRY_OFFSET16((s - wide_limit) * sizeof(u32))); return wide_base + entry_offset; diff --git a/src/nfa/shengcompile.cpp b/src/nfa/shengcompile.cpp index 2c45229a..2fbda9a5 100644 --- a/src/nfa/shengcompile.cpp +++ b/src/nfa/shengcompile.cpp @@ -180,7 +180,7 @@ void raw_report_info_impl::fillReportLists(NFA *n, size_t base_offset, for (const auto &reps : rl) { ro.emplace_back(base_offset); - report_list *p = (report_list *)((char *)n + base_offset); + report_list *p = reinterpret_cast(reinterpret_cast(n) + base_offset); u32 i = 0; for (const ReportID report : reps.reports) { @@ -389,17 +389,17 @@ static void fillAccelAux(struct NFA *n, dfa_info &info, map &accelInfo) { DEBUG_PRINTF("Filling accel aux structures\n"); - T *s = (T *)getMutableImplNfa(n); + T *s = reinterpret_cast(getMutableImplNfa(n)); u32 offset = s->accel_offset; for (dstate_id_t i = 0; i < info.size(); i++) { dstate_id_t state_id = info.raw_id(i); if (accelInfo.find(state_id) != accelInfo.end()) { s->flags |= SHENG_FLAG_HAS_ACCEL; - AccelAux *aux = (AccelAux *)((char *)n + offset); + AccelAux *aux = reinterpret_cast(reinterpret_cast(n) + offset); info.strat.buildAccel(state_id, accelInfo[state_id], aux); sstate_aux *saux = - (sstate_aux *)((char *)n + s->aux_offset) + state_id; + reinterpret_cast(reinterpret_cast(n) + s->aux_offset) + state_id; saux->accel = offset; DEBUG_PRINTF("Accel offset: %u\n", offset); offset += ROUNDUP_N(sizeof(AccelAux), alignof(AccelAux)); @@ -429,7 +429,7 @@ void populateBasicInfo(struct NFA *n, dfa_info &info, n->type = SHENG_NFA; n->flags |= info.raw.hasEodReports() ? NFA_ACCEPTS_EOD : 0; - sheng *s = (sheng *)getMutableImplNfa(n); + sheng *s = reinterpret_cast(getMutableImplNfa(n)); s->aux_offset = aux_offset; s->report_offset = report_offset; s->accel_offset = accel_offset; @@ -454,7 +454,7 @@ void populateBasicInfo(struct NFA *n, dfa_info &info, n->type = SHENG_NFA_32; n->flags |= info.raw.hasEodReports() ? NFA_ACCEPTS_EOD : 0; - sheng32 *s = (sheng32 *)getMutableImplNfa(n); + sheng32 *s = reinterpret_cast(getMutableImplNfa(n)); s->aux_offset = aux_offset; s->report_offset = report_offset; s->accel_offset = accel_offset; @@ -479,7 +479,7 @@ void populateBasicInfo(struct NFA *n, dfa_info &info, n->type = SHENG_NFA_64; n->flags |= info.raw.hasEodReports() ? NFA_ACCEPTS_EOD : 0; - sheng64 *s = (sheng64 *)getMutableImplNfa(n); + sheng64 *s = reinterpret_cast(getMutableImplNfa(n)); s->aux_offset = aux_offset; s->report_offset = report_offset; s->accel_offset = accel_offset; @@ -495,15 +495,15 @@ template static void fillTops(NFA *n, dfa_info &info, dstate_id_t id, map &accelInfo) { - T *s = (T *)getMutableImplNfa(n); + T *s = reinterpret_cast(getMutableImplNfa(n)); u32 aux_base = s->aux_offset; DEBUG_PRINTF("Filling tops for state %u\n", id); - sstate_aux *aux = (sstate_aux *)((char *)n + aux_base) + id; + sstate_aux *aux = reinterpret_cast(reinterpret_cast(n) + aux_base) + id; DEBUG_PRINTF("Aux structure for state %u, offset %zd\n", id, - (char *)aux - (char *)n); + reinterpret_cast(aux) - reinterpret_cast(n)); /* we could conceivably end up in an accept/dead state on a top event, * so mark top as accept/dead state if it indeed is. @@ -519,13 +519,13 @@ template static void fillAux(NFA *n, dfa_info &info, dstate_id_t id, vector &reports, vector &reports_eod, vector &report_offsets) { - T *s = (T *)getMutableImplNfa(n); + T *s = reinterpret_cast(getMutableImplNfa(n)); u32 aux_base = s->aux_offset; auto raw_id = info.raw_id(id); auto &state = info[id]; - sstate_aux *aux = (sstate_aux *)((char *)n + aux_base) + id; + sstate_aux *aux = reinterpret_cast(reinterpret_cast(n) + aux_base) + id; DEBUG_PRINTF("Filling aux and report structures for state %u\n", id); DEBUG_PRINTF("Aux structure for state %u, offset %zd\n", id, @@ -542,7 +542,7 @@ void fillAux(NFA *n, dfa_info &info, dstate_id_t id, vector &reports, template static void fillSingleReport(NFA *n, ReportID r_id) { - T *s = (T *)getMutableImplNfa(n); + T *s = reinterpret_cast(getMutableImplNfa(n)); DEBUG_PRINTF("Single report ID: %u\n", r_id); s->report = r_id; @@ -689,7 +689,8 @@ bytecode_ptr shengCompile_int(raw_dfa &raw, const CompileContext &cc, fillAccelOut(accelInfo, accel_states); } - if (!createShuffleMasks((T *)getMutableImplNfa(nfa.get()), info, accelInfo)) { + T *s = reinterpret_cast(getMutableImplNfa(nfa.get())); + if (!createShuffleMasks(s, info, accelInfo)) { return bytecode_ptr(nullptr); } @@ -727,18 +728,18 @@ bytecode_ptr sheng32Compile(raw_dfa &raw, const CompileContext &cc, set *accel_states) { if (!cc.grey.allowSheng) { DEBUG_PRINTF("Sheng is not allowed!\n"); - bytecode_ptr(nullptr); + return bytecode_ptr(nullptr); } #ifdef HAVE_SVE if (svcntb()<32) { DEBUG_PRINTF("Sheng32 failed, SVE width is too small!\n"); - bytecode_ptr(nullptr); + return bytecode_ptr(nullptr); } #else if (!cc.target_info.has_avx512vbmi()) { DEBUG_PRINTF("Sheng32 failed, no HS_CPU_FEATURES_AVX512VBMI!\n"); - bytecode_ptr(nullptr); + return bytecode_ptr(nullptr); } #endif diff --git a/src/nfa/tamaramacompile.cpp b/src/nfa/tamaramacompile.cpp index 57164908..d9ae70b6 100644 --- a/src/nfa/tamaramacompile.cpp +++ b/src/nfa/tamaramacompile.cpp @@ -142,9 +142,9 @@ buildTamarama(const TamaInfo &tamaInfo, const u32 queue, nfa->length = verify_u32(total_size); nfa->queueIndex = queue; - char *ptr = (char *)nfa.get() + sizeof(NFA); + char *ptr = reinterpret_cast(nfa.get()) + sizeof(NFA); char *base_offset = ptr; - Tamarama *t = (Tamarama *)ptr; + Tamarama *t = reinterpret_cast(ptr); t->numSubEngines = verify_u32(subSize); t->activeIdxSize = verify_u8(activeIdxSize); @@ -152,11 +152,11 @@ buildTamarama(const TamaInfo &tamaInfo, const u32 queue, copy_bytes(ptr, top_base); ptr += byte_length(top_base); - u32 *offsets = (u32 *)ptr; + u32 *offsets = reinterpret_cast(ptr); char *sub_nfa_offset = ptr + sizeof(u32) * subSize; copyInSubnfas(base_offset, *nfa, tamaInfo, offsets, sub_nfa_offset, activeIdxSize); - assert((size_t)(sub_nfa_offset - (char *)nfa.get()) <= total_size); + assert(static_cast(sub_nfa_offset - reinterpret_cast(nfa.get())) <= total_size); return nfa; } diff --git a/src/nfagraph/ng_lbr.cpp b/src/nfagraph/ng_lbr.cpp index 209afc05..fef0f439 100644 --- a/src/nfagraph/ng_lbr.cpp +++ b/src/nfagraph/ng_lbr.cpp @@ -182,7 +182,7 @@ bytecode_ptr buildLbrVerm(const CharReach &cr, const depth &repeatMin, enum RepeatType rtype = chooseRepeatType(repeatMin, repeatMax, minPeriod, is_reset); auto nfa = makeLbrNfa(LBR_NFA_VERM, rtype, repeatMax); - struct lbr_verm *lv = (struct lbr_verm *)getMutableImplNfa(nfa.get()); + struct lbr_verm *lv = reinterpret_cast(getMutableImplNfa(nfa.get())); lv->c = escapes.find_first(); fillNfa(nfa.get(), &lv->common, report, repeatMin, repeatMax, diff --git a/src/parser/utf8_validate.cpp b/src/parser/utf8_validate.cpp index 54c9755e..5a209c2c 100644 --- a/src/parser/utf8_validate.cpp +++ b/src/parser/utf8_validate.cpp @@ -65,7 +65,7 @@ bool isValidUtf8(const char *expression, const size_t len) { return true; } - const u8 *s = (const u8 *)expression; + const u8 *s = reinterpret_cast(expression); u32 val; size_t i = 0; diff --git a/src/util/alloc.cpp b/src/util/alloc.cpp index a6d30b33..651fb164 100644 --- a/src/util/alloc.cpp +++ b/src/util/alloc.cpp @@ -68,8 +68,8 @@ namespace ue2 { #endif void *aligned_malloc_internal(size_t size, size_t align) { - // cppcheck-suppress cstyleCast void *mem= nullptr;; + // cppcheck-suppress cstyleCast int rv = posix_memalign(&mem, align, size); if (rv != 0) { DEBUG_PRINTF("posix_memalign returned %d when asked for %zu bytes\n", diff --git a/src/util/uniform_ops.h b/src/util/uniform_ops.h index 1c39c936..fddc2253 100644 --- a/src/util/uniform_ops.h +++ b/src/util/uniform_ops.h @@ -41,37 +41,58 @@ #include "unaligned.h" // Aligned loads +#ifndef __cplusplus__ #define load_u8(a) (*(const u8 *)(a)) #define load_u16(a) (*(const u16 *)(a)) #define load_u32(a) (*(const u32 *)(a)) #define load_u64a(a) (*(const u64a *)(a)) +#else +#define load_u8(a) (*(reinterpret_cast(a)) +#define load_u16(a) (*(reinterpret_cast(a)) +#define load_u32(a) (*(reinterpret_cast(a)) +#define load_u64a(a) (*(reinterpret_cast(a)) +#endif // __cplusplus__ #define load_m128(a) load128(a) #define load_m256(a) load256(a) #define load_m384(a) load384(a) #define load_m512(a) load512(a) // Unaligned loads +#ifndef __cplusplus__ #define loadu_u8(a) (*(const u8 *)(a)) #define loadu_u16(a) unaligned_load_u16((const u8 *)(a)) #define loadu_u32(a) unaligned_load_u32((const u8 *)(a)) #define loadu_u64a(a) unaligned_load_u64a((const u8 *)(a)) +#else +#define loadu_u8(a) (*(reinterpret_cast(a)) +#define loadu_u16(a) unaligned_load_u16(reinterpret_cast(a)) +#define loadu_u32(a) unaligned_load_u32(reinterpret_cast(a)) +#define loadu_u64a(a) unaligned_load_u64a(reinterpret_cast(a)) +#endif // __cplusplus__ #define loadu_m128(a) loadu128(a) #define loadu_m256(a) loadu256(a) #define loadu_m384(a) loadu384(a) #define loadu_m512(a) loadu512(a) // Aligned stores -#define store_u8(ptr, a) do { *(u8 *)(ptr) = (a); } while(0) -#define store_u16(ptr, a) do { *(u16 *)(ptr) = (a); } while(0) -#define store_u32(ptr, a) do { *(u32 *)(ptr) = (a); } while(0) -#define store_u64a(ptr, a) do { *(u64a *)(ptr) = (a); } while(0) +#ifndef __cplusplus__ +#define store_u8(ptr, a) do { *(reinterpret_cast(ptr)) = (a); } while(0) +#define store_u16(ptr, a) do { *(reinterpret_cast(ptr)) = (a); } while(0) +#define store_u32(ptr, a) do { *(reinterpret_cast(ptr)) = (a); } while(0) +#define store_u64a(ptr, a) do { *(reinterpret_cast(ptr)) = (a); } while(0) +#else +#endif // __cplusplus__ #define store_m128(ptr, a) store128(ptr, a) #define store_m256(ptr, a) store256(ptr, a) #define store_m384(ptr, a) store384(ptr, a) #define store_m512(ptr, a) store512(ptr, a) // Unaligned stores +#ifndef __cplusplus__ #define storeu_u8(ptr, a) do { *(u8 *)(ptr) = (a); } while(0) +#else +#define storeu_u8(ptr, a) do { *(reinterpret_cast(ptr)) = (a); } while(0) +#endif // __cplusplus__ #define storeu_u16(ptr, a) unaligned_store_u16(ptr, a) #define storeu_u32(ptr, a) unaligned_store_u32(ptr, a) #define storeu_u64a(ptr, a) unaligned_store_u64a(ptr, a) diff --git a/unit/hyperscan/extparam.cpp b/unit/hyperscan/extparam.cpp index 8839f6b0..962218ca 100644 --- a/unit/hyperscan/extparam.cpp +++ b/unit/hyperscan/extparam.cpp @@ -104,7 +104,7 @@ TEST(ExtParam, LargeExactOffset) { // Try the exact match. corpus = "hatstand" + string(199983, '_') + "teakettle"; err = hs_scan(db, corpus.c_str(), corpus.length(), 0, scratch, record_cb, - (void *)&c); + reinterpret_cast(&c)); ASSERT_EQ(HS_SUCCESS, err); ASSERT_EQ(1U, c.matches.size()); ASSERT_EQ(MatchRecord(200000, 0), c.matches[0]); diff --git a/unit/hyperscan/som.cpp b/unit/hyperscan/som.cpp index bf2d7c42..261c41e6 100644 --- a/unit/hyperscan/som.cpp +++ b/unit/hyperscan/som.cpp @@ -52,7 +52,7 @@ static int vectorCallback(unsigned id, unsigned long long from, unsigned long long to, unsigned, void *ctx) { //printf("match id %u at (%llu,%llu)\n", id, from, to); - vector *matches = (vector *)ctx; + vector *matches = reinterpret_cast *>(ctx); matches->push_back(Match(id, from, to)); return 0; } diff --git a/unit/internal/lbr.cpp b/unit/internal/lbr.cpp index fafe31ea..c747c555 100644 --- a/unit/internal/lbr.cpp +++ b/unit/internal/lbr.cpp @@ -212,7 +212,7 @@ TEST_P(LbrTest, MatchMax) { const string corpus = matchingCorpus(params.max); initQueue(); - q.buffer = (const u8 *)corpus.c_str(); + q.buffer = reinterpret_cast(corpus.c_str()); q.length = corpus.length(); u64a end = corpus.length(); diff --git a/util/database_util.cpp b/util/database_util.cpp index 3df75e2a..dbad7561 100644 --- a/util/database_util.cpp +++ b/util/database_util.cpp @@ -94,7 +94,7 @@ hs_database_t * loadDatabase(const char *filename, bool verbose) { } size_t len = st.st_size; - bytes = (char *)mmap(nullptr, len, PROT_READ, MAP_SHARED, fd, 0); + bytes = reinterpret_cast(mmap(nullptr, len, PROT_READ, MAP_SHARED, fd, 0)); if (bytes == MAP_FAILED) { cout << "mmap failed" << endl; close(fd); diff --git a/util/string_util.h b/util/string_util.h index ab3751c1..04d778af 100644 --- a/util/string_util.h +++ b/util/string_util.h @@ -141,7 +141,7 @@ void prettyPrintRange(std::ostream &out, it_t begin, it_t end) { static really_inline char *makeHex(const unsigned char *pat, unsigned patlen) { size_t hexlen = patlen * 4; - char *hexbuf = (char *)malloc(hexlen + 1); + char *hexbuf = reinterpret_cast(malloc(hexlen + 1)); unsigned i; char *buf; for (i = 0, buf = hexbuf; i < patlen; i++, buf += 4) {