]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
Fix C-style casts
authorKonstantinos Margaritis <konstantinos@vectorcamp.gr>
Wed, 15 May 2024 20:22:39 +0000 (23:22 +0300)
committerKonstantinos Margaritis <konstantinos@vectorcamp.gr>
Thu, 16 May 2024 09:03:42 +0000 (12:03 +0300)
20 files changed:
examples/pcapscan.cc
src/compiler/compiler.cpp
src/compiler/error.cpp
src/fdr/fdr_compile.cpp
src/fdr/fdr_confirm_compile.cpp
src/fdr/flood_compile.cpp
src/fdr/teddy_compile.cpp
src/hs.cpp
src/hwlm/hwlm_build.cpp
src/hwlm/noodle_build.cpp
src/nfa/accel_dfa_build_strat.cpp
src/nfa/accelcompile.cpp
src/nfa/castlecompile.cpp
src/nfa/goughcompile.cpp
src/nfa/goughcompile_reg.cpp
src/nfa/shufti_simd.hpp
src/parser/Parser.rl
src/util/alloc.cpp
src/util/supervector/arch/arm/impl.cpp
src/util/supervector/arch/x86/impl.cpp

index 5c2c7a9d7cfe7e79ec82d4d8ff852ea526e4a06f..92db5cdf1041d4671dd6b30201b120883e953089 100644 (file)
@@ -106,8 +106,7 @@ struct FiveTuple {
         dstAddr = iphdr->ip_dst.s_addr;
 
         // UDP/TCP ports
-        const struct udphdr *uh =
-            (const struct udphdr *)(((const char *)iphdr) + (iphdr->ip_hl * 4));
+        const struct udphdr *uh = reinterpret_cast<const struct udphdr *>(iphdr) + (iphdr->ip_hl * 4);
         srcPort = uh->uh_sport;
         dstPort = uh->uh_dport;
     }
@@ -136,7 +135,7 @@ static
 int onMatch(unsigned int id, unsigned long long from, unsigned long long to,
             unsigned int flags, void *ctx) {
     // Our context points to a size_t storing the match count
-    size_t *matches = (size_t *)ctx;
+    size_t *matches = static_cast<size_t *>(ctx);
     (*matches)++;
     return 0; // continue matching
 }
@@ -232,9 +231,8 @@ public:
             }
 
             // Valid TCP or UDP packet
-            const struct ip *iphdr = (const struct ip *)(pktData
-                    + sizeof(struct ether_header));
-            const char *payload = (const char *)pktData + offset;
+            const struct ip *iphdr = reinterpret_cast<const struct ip *>(pktData) + sizeof(struct ether_header);
+            const char *payload = reinterpret_cast<const char *>(pktData) + offset;
 
             size_t id = stream_map.insert(std::make_pair(FiveTuple(iphdr),
                                           stream_map.size())).first->second;
@@ -574,7 +572,7 @@ int main(int argc, char **argv) {
  */
 static bool payloadOffset(const unsigned char *pkt_data, unsigned int *offset,
                           unsigned int *length) {
-    const ip *iph = (const ip *)(pkt_data + sizeof(ether_header));
+    const ip *iph = reinterpret_cast<const ip *>(pkt_data) + sizeof(ether_header);
     const tcphdr *th = nullptr;
 
     // Ignore packets that aren't IPv4
@@ -593,7 +591,7 @@ static bool payloadOffset(const unsigned char *pkt_data, unsigned int *offset,
 
     switch (iph->ip_p) {
     case IPPROTO_TCP:
-        th = (const tcphdr *)((const char *)iph + ihlen);
+        th = reinterpret_cast<const tcphdr *>(iph) + ihlen;
         thlen = th->th_off * 4;
         break;
     case IPPROTO_UDP:
index c4d74738bd9d453a77aad9342163a9cb710d0158..aa8de4ba2c02514097779b8be2d8612d452ab4be 100644 (file)
@@ -478,7 +478,7 @@ hs_database_t *dbCreate(const char *in_bytecode, size_t len, u64a platform) {
     DEBUG_PRINTF("db size %zu\n", db_len);
     DEBUG_PRINTF("db platform %llx\n", platform);
 
-    struct hs_database *db = (struct hs_database *)hs_database_alloc(db_len);
+    struct hs_database *db = static_cast<struct hs_database *>(hs_database_alloc(db_len));
     if (hs_check_alloc(db) != HS_SUCCESS) {
         hs_database_free(db);
         return nullptr;
@@ -492,7 +492,7 @@ hs_database_t *dbCreate(const char *in_bytecode, size_t len, u64a platform) {
     DEBUG_PRINTF("shift is %zu\n", shift);
 
     db->bytecode = offsetof(struct hs_database, bytes) - shift;
-    char *bytecode = (char *)db + db->bytecode;
+    char *bytecode = reinterpret_cast<char *>(db) + db->bytecode;
     assert(ISALIGNED_CL(bytecode));
 
     db->magic = HS_DB_MAGIC;
@@ -525,7 +525,7 @@ struct hs_database *build(NG &ng, unsigned int *length, u8 pureFlag) {
         throw CompileError("Internal error.");
     }
 
-    const char *bytecode = (const char *)(rose.get());
+    const char *bytecode = reinterpret_cast<const char *>(rose.get());
     const platform_t p = target_to_platform(ng.cc.target_info);
     struct hs_database *db = dbCreate(bytecode, *length, p);
     if (!db) {
index 07db98192dc88672fcb43d355beb8e74b9c91a57..c4252f7c0ceb6a83ad738ee3b579428f647cd108 100644 (file)
@@ -57,15 +57,14 @@ extern const hs_compile_error_t hs_badalloc = {
 namespace ue2 {
 
 hs_compile_error_t *generateCompileError(const string &err, int expression) {
-    hs_compile_error_t *ret =
-        (struct hs_compile_error *)hs_misc_alloc(sizeof(hs_compile_error_t));
+    hs_compile_error_t *ret = static_cast<struct hs_compile_error *>(hs_misc_alloc(sizeof(hs_compile_error_t)));
     if (ret) {
         hs_error_t e = hs_check_alloc(ret);
         if (e != HS_SUCCESS) {
             hs_misc_free(ret);
             return const_cast<hs_compile_error_t *>(&hs_badalloc);
         }
-        char *msg = (char *)hs_misc_alloc(err.size() + 1);
+        char *msg = static_cast<char *>(hs_misc_alloc(err.size() + 1));
         if (msg) {
             e = hs_check_alloc(msg);
             if (e != HS_SUCCESS) {
index d15e4537b40063780aeb9743e816105b5f8d2af6..8127740a4da4daf035b0f55731a90cff58297fdf 100644 (file)
@@ -127,7 +127,7 @@ void andMask(u8 *dest, const u8 *a, const u8 *b, u32 num_bytes) {
 }
 
 void FDRCompiler::createInitialState(FDR *fdr) {
-    u8 *start = (u8 *)&fdr->start;
+    u8 *start = reinterpret_cast<u8 *>(&fdr->start);
 
     /* initial state should to be 1 in each slot in the bucket up to bucket
      * minlen - 1, and 0 thereafter */
@@ -175,7 +175,7 @@ bytecode_ptr<FDR> FDRCompiler::setupFDR() {
     auto fdr = make_zeroed_bytecode_ptr<FDR>(size, 64);
     assert(fdr); // otherwise would have thrown std::bad_alloc
 
-    u8 *fdr_base = (u8 *)fdr.get();
+    u8 *fdr_base = reinterpret_cast<u8 *>(fdr.get());
 
     // Write header.
     fdr->size = size;
index 625d1b14a0432ed52ebee3c133bbc1c7b39b7ef6..cfbd23fe0206b69a55668bb3c986ff5ff7140e65 100644 (file)
@@ -58,7 +58,7 @@ u64a make_u64a_mask(const vector<u8> &v) {
     u64a mask = 0;
     size_t vlen = v.size();
     size_t len = std::min(vlen, sizeof(mask));
-    unsigned char *m = (unsigned char *)&mask;
+    u8 *m = reinterpret_cast<u8 *>(&mask);
     memcpy(m + sizeof(mask) - len, &v[vlen - len], len);
     return mask;
 }
@@ -245,10 +245,10 @@ bytecode_ptr<FDRConfirm> getFDRConfirm(const vector<hwlmLiteral> &lits,
     fdrc->groups = gm;
 
     // After the FDRConfirm, we have the lit index array.
-    u8 *fdrc_base = (u8 *)fdrc.get();
+    u8 *fdrc_base = reinterpret_cast<u8 *>(fdrc.get());
     u8 *ptr = fdrc_base + sizeof(*fdrc);
     ptr = ROUNDUP_PTR(ptr, alignof(u32));
-    u32 *bitsToLitIndex = (u32 *)ptr;
+    u32 *bitsToLitIndex = reinterpret_cast<u32 *>(ptr);
     ptr += bitsToLitIndexSize;
 
     // After the lit index array, we have the LitInfo structures themselves,
@@ -265,7 +265,7 @@ bytecode_ptr<FDRConfirm> getFDRConfirm(const vector<hwlmLiteral> &lits,
             LiteralIndex litIdx = *i;
 
             // Write LitInfo header.
-            LitInfo &finalLI = *(LitInfo *)ptr;
+            LitInfo &finalLI = *(reinterpret_cast<LitInfo *>(ptr));
             finalLI = tmpLitInfo[litIdx];
 
             ptr += sizeof(LitInfo); // String starts directly after LitInfo.
@@ -317,7 +317,7 @@ setupFullConfs(const vector<hwlmLiteral> &lits,
     auto buf = make_zeroed_bytecode_ptr<u8>(totalSize, 64);
     assert(buf); // otherwise would have thrown std::bad_alloc
 
-    u32 *confBase = (u32 *)buf.get();
+    u32 *confBase = reinterpret_cast<u32 *>(buf.get());
     u8 *ptr = buf.get() + totalConfSwitchSize;
     assert(ISALIGNED_CL(ptr));
 
index ff805ca399a4fc548d402bd0dba34aeb71ab0aa8..6811fc958e43447d099eed93ce2a34fd21417f37 100644 (file)
@@ -208,8 +208,8 @@ bytecode_ptr<u8> setupFDRFloodControl(const vector<hwlmLiteral> &lits,
     auto buf = make_zeroed_bytecode_ptr<u8>(totalSize, 16);
     assert(buf); // otherwise would have thrown std::bad_alloc
 
-    u32 *floodHeader = (u32 *)buf.get();
-    FDRFlood *layoutFlood = (FDRFlood *)(buf.get() + floodHeaderSize);
+    u32 *floodHeader = reinterpret_cast<u32 *>(buf.get());
+    FDRFlood *layoutFlood = reinterpret_cast<FDRFlood *>(buf.get() + floodHeaderSize);
 
     u32 currentFloodIndex = 0;
     for (const auto &m : flood2chars) {
index 23b70bb770f7deaab708902253b52ccec42310b8..821a69e2a9552139d613b1b3bc11f1fa788cab3a 100644 (file)
@@ -328,7 +328,7 @@ bool pack(const vector<hwlmLiteral> &lits,
 
 static
 void initReinforcedTable(u8 *rmsk) {
-    u64a *mask = (u64a *)rmsk;
+    u64a *mask = reinterpret_cast<u64a *>(rmsk);
     fill_n(mask, N_CHARS, 0x00ffffffffffffffULL);
 }
 
@@ -576,8 +576,8 @@ bytecode_ptr<FDR> TeddyCompiler::build() {
 
     auto fdr = make_zeroed_bytecode_ptr<FDR>(size, 64);
     assert(fdr); // otherwise would have thrown std::bad_alloc
-    Teddy *teddy = (Teddy *)fdr.get(); // ugly
-    u8 *teddy_base = (u8 *)teddy;
+    Teddy *teddy = reinterpret_cast<Teddy *>(fdr.get()); // ugly
+    u8 *teddy_base = reinterpret_cast<u8 *>(teddy);
 
     // Write header.
     teddy->size = size;
index 61e46148c78c639b9b7e10c395dd7ecf014e9e81..22a9043b5300d0180417999f88e7aa9ce204ceea 100644 (file)
@@ -589,7 +589,7 @@ hs_error_t hs_expression_info_int(const char *expression, unsigned int flags,
         return HS_COMPILER_ERROR;
     }
 
-    hs_expr_info *rv = (hs_expr_info *)hs_misc_alloc(sizeof(*rv));
+    hs_expr_info *rv = static_cast<hs_expr_info *>(hs_misc_alloc(sizeof(*rv)));
     if (!rv) {
         *error = const_cast<hs_compile_error_t *>(&hs_enomem);
         return HS_COMPILER_ERROR;
index 73f05921d178b32696e87c7344afacac7e1cf849..bb83849b69450390edee3490d3b034a6f9694195 100644 (file)
@@ -155,6 +155,7 @@ bytecode_ptr<HWLM> hwlmBuild(const HWLMProto &proto, const CompileContext &cc,
     auto h = make_zeroed_bytecode_ptr<HWLM>(hwlm_len, 64);
 
     h->type = proto.engType;
+    // cppcheck-suppress cstyleCast
     memcpy(HWLM_DATA(h.get()), eng.get(), engSize);
 
     return h;
@@ -218,10 +219,12 @@ size_t hwlmSize(const HWLM *h) {
 
     switch (h->type) {
     case HWLM_ENGINE_NOOD:
-        engSize = noodSize((const noodTable *)HWLM_C_DATA(h));
+       // cppcheck-suppress cstyleCast
+        engSize = noodSize(reinterpret_cast<const noodTable *>(HWLM_C_DATA(h)));
         break;
     case HWLM_ENGINE_FDR:
-        engSize = fdrSize((const FDR *)HWLM_C_DATA(h));
+       // cppcheck-suppress cstyleCast
+        engSize = fdrSize(reinterpret_cast<const FDR *>(HWLM_C_DATA(h)));
         break;
     }
 
index a0128d0ad78345a6f1bf265a95061bccd592074f..74dfbd2c963aee9b98c6a207da4cbf250a25ea1f 100644 (file)
@@ -56,7 +56,7 @@ u64a make_u64a_mask(const vector<u8> &v) {
 
     u64a mask = 0;
     size_t len = v.size();
-    unsigned char *m = (unsigned char *)&mask;
+    u8 *m = reinterpret_cast<u8 *>(&mask);
     DEBUG_PRINTF("making mask len %zu\n", len);
     memcpy(m, &v[0], len);
     return mask;
index 7139d5bea7092fa6038df7a90705ffa57e065939..249d39c94a3f41f4d3e9c6ec755bd3d8cb759e81 100644 (file)
@@ -426,7 +426,7 @@ void
 accel_dfa_build_strat::buildAccel(UNUSED dstate_id_t this_idx,
                                   const AccelScheme &info,
                                   void *accel_out) {
-    AccelAux *accel = (AccelAux *)accel_out;
+    AccelAux *accel = reinterpret_cast<AccelAux *>(accel_out);
 
     DEBUG_PRINTF("accelerations scheme has offset s%u/d%u\n", info.offset,
                  info.double_offset);
@@ -473,7 +473,8 @@ accel_dfa_build_strat::buildAccel(UNUSED dstate_id_t this_idx,
                 u8 c1 = info.double_byte.begin()->first & m1;
                 u8 c2 = info.double_byte.begin()->second & m2;
 #ifdef HAVE_SVE2
-                if (vermicelliDoubleMasked16Build(c1, c2, m1, m2, (u8 *)&accel->mdverm16.mask)) {
+                if (vermicelliDoubleMasked16Build(c1, c2, m1, m2,
+                                                  reinterpret_cast<u8 *>(&accel->mdverm16.mask))) {
                     accel->accel_type = ACCEL_DVERM16_MASKED;
                     accel->mdverm16.offset = verify_u8(info.double_offset);
                     accel->mdverm16.c1 = c1;
@@ -482,8 +483,9 @@ accel_dfa_build_strat::buildAccel(UNUSED dstate_id_t this_idx,
                                 c1, c2);
                     return;
                 } else if (info.double_byte.size() <= 8 &&
-                        vermicelliDouble16Build(info.double_byte, (u8 *)&accel->dverm16.mask,
-                                                (u8 *)&accel->dverm16.firsts)) {
+                        vermicelliDouble16Build(info.double_byte,
+                                                reinterpret_cast<u8 *>(&accel->dverm16.mask),
+                                                reinterpret_cast<u8 *>(&accel->dverm16.firsts))) {
                     accel->accel_type = ACCEL_DVERM16;
                     accel->dverm16.offset = verify_u8(info.double_offset);
                     DEBUG_PRINTF("building double16-vermicelli\n");
@@ -503,8 +505,9 @@ accel_dfa_build_strat::buildAccel(UNUSED dstate_id_t this_idx,
         }
 #ifdef HAVE_SVE2
         if (info.double_byte.size() <= 8 &&
-            vermicelliDouble16Build(info.double_byte, (u8 *)&accel->dverm16.mask,
-                                    (u8 *)&accel->dverm16.firsts)) {
+            vermicelliDouble16Build(info.double_byte,
+                                    reinterpret_cast<u8 *>(&accel->dverm16.mask),
+                                    reinterpret_cast<u8 *>(&accel->dverm16.firsts))) {
             accel->accel_type = ACCEL_DVERM16;
             accel->dverm16.offset = verify_u8(info.double_offset);
             DEBUG_PRINTF("building double16-vermicelli\n");
@@ -515,9 +518,11 @@ accel_dfa_build_strat::buildAccel(UNUSED dstate_id_t this_idx,
 
     if (double_byte_ok(info) &&
         shuftiBuildDoubleMasks(
-            info.double_cr, info.double_byte, (u8 *)&accel->dshufti.lo1,
-            (u8 *)&accel->dshufti.hi1, (u8 *)&accel->dshufti.lo2,
-            (u8 *)&accel->dshufti.hi2)) {
+            info.double_cr, info.double_byte,
+            reinterpret_cast<u8 *>(&accel->dshufti.lo1),
+            reinterpret_cast<u8 *>(&accel->dshufti.hi1),
+            reinterpret_cast<u8 *>(&accel->dshufti.lo2),
+            reinterpret_cast<u8 *>(&accel->dshufti.hi2))) {
         accel->accel_type = ACCEL_DSHUFTI;
         accel->dshufti.offset = verify_u8(info.double_offset);
         DEBUG_PRINTF("state %hu is double shufti\n", this_idx);
@@ -549,7 +554,7 @@ accel_dfa_build_strat::buildAccel(UNUSED dstate_id_t this_idx,
 #ifdef HAVE_SVE2
     if (info.cr.count() <= 16) {
         accel->accel_type = ACCEL_VERM16;
-        vermicelli16Build(info.cr, (u8 *)&accel->verm16.mask);
+        vermicelli16Build(info.cr, reinterpret_cast<u8 *>(&accel->verm16.mask));
         DEBUG_PRINTF("state %hu is vermicelli16\n", this_idx);
         return;
     }
@@ -562,16 +567,18 @@ accel_dfa_build_strat::buildAccel(UNUSED dstate_id_t this_idx,
     }
 
     accel->accel_type = ACCEL_SHUFTI;
-    if (-1 != shuftiBuildMasks(info.cr, (u8 *)&accel->shufti.lo,
-                               (u8 *)&accel->shufti.hi)) {
+    if (-1 != shuftiBuildMasks(info.cr,
+                               reinterpret_cast<u8 *>(&accel->shufti.lo),
+                               reinterpret_cast<u8 *>(&accel->shufti.hi))) {
         DEBUG_PRINTF("state %hu is shufti\n", this_idx);
         return;
     }
 
     assert(!info.cr.none());
     accel->accel_type = ACCEL_TRUFFLE;
-    truffleBuildMasks(info.cr, (u8 *)&accel->truffle.mask1,
-                      (u8 *)&accel->truffle.mask2);
+    truffleBuildMasks(info.cr,
+                      reinterpret_cast<u8 *>(&accel->truffle.mask1),
+                      reinterpret_cast<u8 *>(&accel->truffle.mask2));
     DEBUG_PRINTF("state %hu is truffle\n", this_idx);
 }
 
index e0be910d8812e8c99f17c215c56a26d97f771a6b..5da0df82efd0eeb86a637e1242d9bc6b6fc02c33 100644 (file)
@@ -84,8 +84,9 @@ void buildAccelSingle(const AccelInfo &info, AccelAux *aux) {
 #endif
 
     DEBUG_PRINTF("attempting shufti for %zu chars\n", outs);
-    if (-1 != shuftiBuildMasks(info.single_stops, (u8 *)&aux->shufti.lo,
-                               (u8 *)&aux->shufti.hi)) {
+    if (-1 != shuftiBuildMasks(info.single_stops,
+                               reinterpret_cast<u8 *>(&aux->shufti.lo),
+                               reinterpret_cast<u8 *>(&aux->shufti.hi))) {
         aux->accel_type = ACCEL_SHUFTI;
         aux->shufti.offset = offset;
         DEBUG_PRINTF("shufti built OK\n");
@@ -98,8 +99,9 @@ void buildAccelSingle(const AccelInfo &info, AccelAux *aux) {
         DEBUG_PRINTF("building Truffle for %zu chars\n", outs);
         aux->accel_type = ACCEL_TRUFFLE;
         aux->truffle.offset = offset;
-        truffleBuildMasks(info.single_stops, (u8 *)&aux->truffle.mask1,
-                          (u8 *)&aux->truffle.mask2);
+        truffleBuildMasks(info.single_stops,
+                          reinterpret_cast<u8 *>(&aux->truffle.mask1),
+                          reinterpret_cast<u8 *>(&aux->truffle.mask2));
         return;
     }
 
@@ -219,8 +221,9 @@ void buildAccelDouble(const AccelInfo &info, AccelAux *aux) {
                              c1, c2);
                 return;
             } else if (outs2 <= 8 &&
-                       vermicelliDouble16Build(info.double_stop2, (u8 *)&aux->dverm16.mask,
-                                               (u8 *)&aux->dverm16.firsts)) {
+                       vermicelliDouble16Build(info.double_stop2,
+                                               reinterpret_cast<u8 *>(&aux->dverm16.mask),
+                                               reinterpret_cast<u8 *>(&aux->dverm16.firsts))) {
                 aux->accel_type = ACCEL_DVERM16;
                 aux->dverm16.offset = offset;
                 DEBUG_PRINTF("building double16-vermicelli\n");
@@ -254,9 +257,11 @@ void buildAccelDouble(const AccelInfo &info, AccelAux *aux) {
         aux->accel_type = ACCEL_DSHUFTI;
         aux->dshufti.offset = offset;
         if (shuftiBuildDoubleMasks(
-                info.double_stop1, info.double_stop2, (u8 *)&aux->dshufti.lo1,
-                (u8 *)&aux->dshufti.hi1, (u8 *)&aux->dshufti.lo2,
-                (u8 *)&aux->dshufti.hi2)) {
+                info.double_stop1, info.double_stop2,
+                reinterpret_cast<u8 *>(&aux->dshufti.lo1),
+                reinterpret_cast<u8 *>(&aux->dshufti.hi1),
+                reinterpret_cast<u8 *>(&aux->dshufti.lo2),
+                reinterpret_cast<u8 *>(&aux->dshufti.hi2))) {
             return;
         }
     }
index 9667413c33466d1c6938b63097373b3e05ef87fc..28f1aed910d9144249aacd729af788c40a17ef9d 100644 (file)
@@ -106,25 +106,27 @@ void writeCastleScanEngine(const CharReach &cr, Castle *c) {
 #ifdef HAVE_SVE2
     if (cr.count() <= 16) {
         c->type = CASTLE_NVERM16;
-        vermicelli16Build(cr, (u8 *)&c->u.verm16.mask);
+        vermicelli16Build(cr, reinterpret_cast<u8 *>(&c->u.verm16.mask));
         return;
     }
     if (negated.count() <= 16) {
         c->type = CASTLE_VERM16;
-        vermicelli16Build(negated, (u8 *)&c->u.verm16.mask);
+        vermicelli16Build(negated, reinterpret_cast<u8 *>(&c->u.verm16.mask));
         return;
     }
 #endif // HAVE_SVE2
 
-    if (shuftiBuildMasks(negated, (u8 *)&c->u.shuf.mask_lo,
-                         (u8 *)&c->u.shuf.mask_hi) != -1) {
+    if (shuftiBuildMasks(negated,
+                         reinterpret_cast<u8 *>(&c->u.shuf.mask_lo),
+                         reinterpret_cast<u8 *>(&c->u.shuf.mask_hi)) != -1) {
         c->type = CASTLE_SHUFTI;
         return;
     }
 
     c->type = CASTLE_TRUFFLE;
-    truffleBuildMasks(negated, (u8 *)(u8 *)&c->u.truffle.mask1,
-                      (u8 *)&c->u.truffle.mask2);
+    truffleBuildMasks(negated,
+                      reinterpret_cast<u8 *>(&c->u.truffle.mask1),
+                      reinterpret_cast<u8 *>(&c->u.truffle.mask2));
 }
 
 static
@@ -602,9 +604,9 @@ buildCastle(const CastleProto &proto,
     nfa->minWidth = verify_u32(minWidth);
     nfa->maxWidth = maxWidth.is_finite() ? verify_u32(maxWidth) : 0;
 
-    char * const base_ptr = (char *)nfa.get() + sizeof(NFA);
+    char * const base_ptr = reinterpret_cast<char *>(nfa.get()) + sizeof(NFA);
     char *ptr = base_ptr;
-    Castle *c = (Castle *)ptr;
+    Castle *c = reinterpret_cast<Castle *>(ptr);
     c->numRepeats = verify_u32(subs.size());
     c->numGroups = exclusiveInfo.numGroups;
     c->exclusive = verify_s8(exclusive);
@@ -615,7 +617,7 @@ buildCastle(const CastleProto &proto,
     writeCastleScanEngine(cr, c);
 
     ptr += sizeof(Castle);
-    SubCastle *subCastles = ((SubCastle *)(ROUNDUP_PTR(ptr, alignof(u32))));
+    SubCastle *subCastles = reinterpret_cast<SubCastle *>(ROUNDUP_PTR(ptr, alignof(u32)));
     copy(subs.begin(), subs.end(), subCastles);
 
     u32 length = 0;
@@ -625,16 +627,16 @@ buildCastle(const CastleProto &proto,
         SubCastle *sub = &subCastles[i];
         sub->repeatInfoOffset = offset;
 
-        ptr = (char *)sub + offset;
+        ptr = reinterpret_cast<char *>(sub) + offset;
         memcpy(ptr, &infos[i], sizeof(RepeatInfo));
 
         if (patchSize[i]) {
-            RepeatInfo *info = (RepeatInfo *)ptr;
-            u64a *table = ((u64a *)(ROUNDUP_PTR(((char *)(info) +
-                                    sizeof(*info)), alignof(u64a))));
+            RepeatInfo *info = reinterpret_cast<RepeatInfo *>(ptr);
+            u64a *table = reinterpret_cast<u64a *>(ROUNDUP_PTR(info +
+                                    sizeof(*info), alignof(u64a)));
             copy(tables.begin() + tableIdx,
                  tables.begin() + tableIdx + patchSize[i], table);
-            u32 diff = (char *)table - (char *)info +
+            u32 diff = reinterpret_cast<ptrdiff_t>(table) - reinterpret_cast<ptrdiff_t>(info) +
                        sizeof(u64a) * patchSize[i];
             info->length = diff;
             length += diff;
@@ -657,8 +659,6 @@ buildCastle(const CastleProto &proto,
     if (!stale_iter.empty()) {
         c->staleIterOffset = verify_u32(ptr - base_ptr);
         copy_bytes(ptr, stale_iter);
-        // Removed unused increment operation
-        // ptr += byte_length(stale_iter);
     }
 
     return nfa;
index e481e15cb019e540593affab8c374c19aac02763..6366c76f07f2db85e91abd0bf0d30a8f501acf1d 100644 (file)
@@ -1077,8 +1077,9 @@ bytecode_ptr<NFA> goughCompile(raw_som_dfa &raw, u8 somPrecision,
         return bytecode_ptr<NFA>(nullptr);
     }
 
-    u8 alphaShift
-        = ((const mcclellan *)getImplNfa(basic_dfa.get()))->alphaShift;
+    // cppcheck-suppress cstyleCast
+    const auto nfa = static_cast<const mcclellan *>(getImplNfa(basic_dfa.get()));
+    u8 alphaShift = nfa->alphaShift;
     u32 edge_count = (1U << alphaShift) * raw.states.size();
 
     u32 curr_offset = ROUNDUP_N(basic_dfa->length, 4);
@@ -1119,8 +1120,8 @@ bytecode_ptr<NFA> goughCompile(raw_som_dfa &raw, u8 somPrecision,
     u32 gough_size = ROUNDUP_N(curr_offset, 16);
     auto gough_dfa = make_zeroed_bytecode_ptr<NFA>(gough_size);
 
-    memcpy(gough_dfa.get(), basic_dfa.get(), basic_dfa->length);
-    memcpy((char *)gough_dfa.get() + haig_offset, &gi, sizeof(gi));
+    memcpy(reinterpret_cast<char *>(gough_dfa.get()), basic_dfa.get(), basic_dfa->length);
+    memcpy(reinterpret_cast<char *>(gough_dfa.get()) + haig_offset, &gi, sizeof(gi));
     if (gough_dfa->type == MCCLELLAN_NFA_16) {
         gough_dfa->type = GOUGH_NFA_16;
     } else {
@@ -1133,18 +1134,19 @@ bytecode_ptr<NFA> goughCompile(raw_som_dfa &raw, u8 somPrecision,
     gough_dfa->streamStateSize = base_state_size + slot_count * somPrecision;
     gough_dfa->scratchStateSize = (u32)(16 + scratch_slot_count * sizeof(u64a));
 
-    mcclellan *m = (mcclellan *)getMutableImplNfa(gough_dfa.get());
+    // cppcheck-suppress cstyleCast
+    auto *m = reinterpret_cast<mcclellan *>(getMutableImplNfa(gough_dfa.get()));
     m->haig_offset = haig_offset;
 
     /* update nfa length, haig_info offset (leave mcclellan length alone) */
     gough_dfa->length = gough_size;
 
     /* copy in blocks */
-    copy_bytes((u8 *)gough_dfa.get() + edge_prog_offset, edge_blocks);
+    copy_bytes(reinterpret_cast<u8 *>(gough_dfa.get()) + edge_prog_offset, edge_blocks);
     if (top_prog_offset) {
-        copy_bytes((u8 *)gough_dfa.get() + top_prog_offset, top_blocks);
+        copy_bytes(reinterpret_cast<u8 *>(gough_dfa.get()) + top_prog_offset, top_blocks);
     }
-    copy_bytes((u8 *)gough_dfa.get() + prog_base_offset, temp_blocks);
+    copy_bytes(reinterpret_cast<u8 *>(gough_dfa.get()) + prog_base_offset, temp_blocks);
 
     return gough_dfa;
 }
@@ -1177,7 +1179,7 @@ AccelScheme gough_build_strat::find_escape_strings(dstate_id_t this_idx) const {
 void gough_build_strat::buildAccel(dstate_id_t this_idx, const AccelScheme &info,
                                    void *accel_out) {
     assert(mcclellan_build_strat::accelSize() == sizeof(AccelAux));
-    gough_accel *accel = (gough_accel *)accel_out;
+    gough_accel *accel = reinterpret_cast<gough_accel *>(accel_out);
     /* build a plain accelaux so we can work out where we can get to */
     mcclellan_build_strat::buildAccel(this_idx, info, &accel->accel);
     DEBUG_PRINTF("state %hu is accel with type %hhu\n", this_idx,
@@ -1315,7 +1317,8 @@ void raw_gough_report_info_impl::fillReportLists(NFA *n, size_t base_offset,
     for (const raw_gough_report_list &r : rl) {
         ro.emplace_back(base_offset);
 
-        gough_report_list *p = (gough_report_list *)((char *)n + base_offset);
+        u8 * n_ptr = reinterpret_cast<u8 *>(n);
+        gough_report_list *p = reinterpret_cast<gough_report_list *>(n_ptr + base_offset);
         u32 i = 0;
 
         for (const som_report &sr : r.reports) {
index 4b0c90f1ee178084c9208523037f7feb42a879da..83d0c4cc8f00d24abe1f333fbb4af4982a6e51ff 100644 (file)
@@ -194,7 +194,7 @@ void handle_pending_vars(GoughSSAVar *def, const GoughGraph &g,
         if (contains(aux.containing_v, var)) {
             /* def is used by join vertex, value only needs to be live on some
              * incoming edges */
-            const GoughSSAVarJoin *vj = (GoughSSAVarJoin *)var;
+            const GoughSSAVarJoin *vj = reinterpret_cast<const GoughSSAVarJoin *>(var);
             const flat_set<GoughEdge> &live_edges
                 = vj->get_edges_for_input(def);
             for (const auto &e : live_edges) {
index feeb54abd4402ec0e83cd4dadfb05b34fc041fca..bdb0ff9fecd3d7b390e6174292ac4eb0bdb97586 100644 (file)
@@ -264,7 +264,7 @@ const u8 *shuftiDoubleExecReal(m128 mask1_lo, m128 mask1_hi, m128 mask2_lo, m128
 const u8 *shuftiExec(m128 mask_lo, m128 mask_hi, const u8 *buf,
                       const u8 *buf_end) {
   if (buf_end - buf < VECTORSIZE) {
-    return shuftiFwdSlow((const u8 *)&mask_lo, (const u8 *)&mask_hi, buf, buf_end);
+    return shuftiFwdSlow(reinterpret_cast<const u8 *>(&mask_lo), reinterpret_cast<const u8 *>(&mask_hi), buf, buf_end);
   }
   return shuftiExecReal<VECTORSIZE>(mask_lo, mask_hi, buf, buf_end);
 }
@@ -272,7 +272,7 @@ const u8 *shuftiExec(m128 mask_lo, m128 mask_hi, const u8 *buf,
 const u8 *rshuftiExec(m128 mask_lo, m128 mask_hi, const u8 *buf,
                        const u8 *buf_end) {
     if (buf_end - buf < VECTORSIZE) {
-      return shuftiRevSlow((const u8 *)&mask_lo, (const u8 *)&mask_hi, buf, buf_end);
+      return shuftiRevSlow(reinterpret_cast<const u8 *>(&mask_lo), reinterpret_cast<const u8 *>(&mask_hi), buf, buf_end);
     }
     return rshuftiExecReal<VECTORSIZE>(mask_lo, mask_hi, buf, buf_end);
 }
index e5cbfe2b92664428ff0de0aed1814c76867a0c98..ae419ec84d57dcee0a402c0dee2327ea23a8877e 100644 (file)
@@ -224,7 +224,7 @@ u8 decodeCtrl(char raw) {
 
 static
 unichar readUtf8CodePoint2c(const char *s) {
-    auto *ts = (const u8 *)s;
+    auto *ts = reinterpret_cast<const u8 *>(s);
     assert(ts[0] >= 0xc0 && ts[0] < 0xe0);
     assert(ts[1] >= 0x80 && ts[1] < 0xc0);
     unichar val = ts[0] & 0x1f;
index fb20f3d315e42588ab6a324ae2cf3681904ba150..4563d4df2c77eba88f2834882be51fead5a85303 100644 (file)
@@ -68,6 +68,7 @@ namespace ue2 {
 #endif
 
 void *aligned_malloc_internal(size_t size, size_t align) {
+    // cppcheck-suppress cstyleCast
     void *mem= nullptr;;
     int rv = posix_memalign(&mem, align, size);
     if (rv != 0) {
@@ -104,17 +105,17 @@ void *aligned_zmalloc(size_t size) {
 
     const size_t alloc_size = size + HACK_OFFSET;
 
-    void *mem = aligned_malloc_internal(alloc_size, 64);
+    char *mem = static_cast<char *>(aligned_malloc_internal(alloc_size, 64));
     if (!mem) {
         DEBUG_PRINTF("unable to allocate %zu bytes\n", alloc_size);
         throw std::bad_alloc();
     }
 
-    DEBUG_PRINTF("alloced %p reporting %p\n", mem, (char *)mem + HACK_OFFSET);
+    DEBUG_PRINTF("alloced %p reporting %p\n", mem, mem + HACK_OFFSET);
     assert(ISALIGNED_N(mem, 64));
 
     memset(mem, 0, alloc_size);
-    return (void *)((char *)mem + HACK_OFFSET);
+    return reinterpret_cast<void *>(mem + HACK_OFFSET);
 }
 
 /** \brief Free a pointer allocated with \ref aligned_zmalloc. */
@@ -123,7 +124,7 @@ void aligned_free(void *ptr) {
         return;
     }
 
-    void *addr = (void *)((char *)ptr - HACK_OFFSET);
+    ptrdiff_t *addr = static_cast<ptrdiff_t *>(ptr) - HACK_OFFSET;
     DEBUG_PRINTF("asked to free %p freeing %p\n", ptr, addr);
 
     assert(ISALIGNED_N(addr, 64));
index 845ccea0a52802794543e3c9d16a8e907a00c275..17859d61cc24f3a85e862b6fcda9ddd360e6978e 100644 (file)
@@ -511,7 +511,7 @@ really_inline SuperVector<16> SuperVector<16>::Ones_vshl(uint8_t const N)
 template <>
 really_inline SuperVector<16> SuperVector<16>::loadu(void const *ptr)
 {
-    return {SuperVector<16>(vld1q_s32((const int32_t *)ptr))};
+    return {SuperVector<16>(vld1q_s32(reinterpret_cast<const int32_t *>(ptr)))};
 }
 
 template <>
@@ -519,7 +519,7 @@ really_inline SuperVector<16> SuperVector<16>::load(void const *ptr)
 {
     assert(ISALIGNED_N(ptr, alignof(SuperVector::size)));
     ptr = vectorscan_assume_aligned(ptr, SuperVector::size);
-    return {SuperVector<16>(vld1q_s32((const int32_t *)ptr))};
+    return {SuperVector<16>(vld1q_s32(reinterpret_cast<const int32_t *>(ptr)))};
 }
 
 template <>
index 6a7dfa3dc330f6bf05b31cfd61dc77bedd30ba3f..0323d5e539885b6230add054964fb0c92f0b3c44 100644 (file)
@@ -508,7 +508,7 @@ really_inline SuperVector<16> SuperVector<16>::Ones_vshl(uint8_t const N)
 template <>
 really_inline SuperVector<16> SuperVector<16>::loadu(void const *ptr)
 {
-    return SuperVector<16>(_mm_loadu_si128((const m128 *)ptr));
+    return SuperVector<16>(_mm_loadu_si128(reinterpret_cast<const m128 *>(ptr)));
 }
 
 template <>
@@ -516,14 +516,14 @@ really_inline SuperVector<16> SuperVector<16>::load(void const *ptr)
 {
     assert(ISALIGNED_N(ptr, alignof(SuperVector::size)));
     ptr = vectorscan_assume_aligned(ptr, SuperVector::size);
-    return SuperVector<16>(_mm_load_si128((const m128 *)ptr));
+    return SuperVector<16>(_mm_load_si128(reinterpret_cast<const m128 *>(ptr)));
 }
 
 template <>
 really_inline SuperVector<16> SuperVector<16>::loadu_maskz(void const *ptr, uint8_t const len)
 {
     SuperVector mask = Ones_vshr(16 -len);
-    SuperVector v = SuperVector<16>(_mm_loadu_si128((const m128 *)ptr));
+    SuperVector v = SuperVector<16>(_mm_loadu_si128(reinterpret_cast<const m128 *>(ptr)));
     return mask & v;
 }