From 80812ee5b31e57f715e48bcbf491c5d92d1f6aee Mon Sep 17 00:00:00 2001 From: Konstantinos Margaritis Date: Sat, 18 May 2024 14:56:30 +0300 Subject: [PATCH 1/7] Fix wrong casts in pcapscan.cc --- examples/pcapscan.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/pcapscan.cc b/examples/pcapscan.cc index 92db5cdf..3ad20b1a 100644 --- a/examples/pcapscan.cc +++ b/examples/pcapscan.cc @@ -106,7 +106,8 @@ struct FiveTuple { dstAddr = iphdr->ip_dst.s_addr; // UDP/TCP ports - const struct udphdr *uh = reinterpret_cast(iphdr) + (iphdr->ip_hl * 4); + const char * iphdr_base = reinterpret_cast(iphdr); + const struct udphdr *uh = reinterpret_cast(iphdr_base + (iphdr->ip_hl * 4)); srcPort = uh->uh_sport; dstPort = uh->uh_dport; } @@ -231,7 +232,7 @@ public: } // Valid TCP or UDP packet - const struct ip *iphdr = reinterpret_cast(pktData) + sizeof(struct ether_header); + const struct ip *iphdr = reinterpret_cast(pktData + sizeof(struct ether_header)); const char *payload = reinterpret_cast(pktData) + offset; size_t id = stream_map.insert(std::make_pair(FiveTuple(iphdr), @@ -572,7 +573,8 @@ int main(int argc, char **argv) { */ static bool payloadOffset(const unsigned char *pkt_data, unsigned int *offset, unsigned int *length) { - const ip *iph = reinterpret_cast(pkt_data) + sizeof(ether_header); + const ip *iph = reinterpret_cast(pkt_data + sizeof(ether_header)); + const char *iph_base = reinterpret_cast(iph); const tcphdr *th = nullptr; // Ignore packets that aren't IPv4 @@ -591,7 +593,7 @@ static bool payloadOffset(const unsigned char *pkt_data, unsigned int *offset, switch (iph->ip_p) { case IPPROTO_TCP: - th = reinterpret_cast(iph) + ihlen; + th = reinterpret_cast(iph_base + ihlen); thlen = th->th_off * 4; break; case IPPROTO_UDP: From 14deb313c1252259bdf04efc46197183e7b00e78 Mon Sep 17 00:00:00 2001 From: Konstantinos Margaritis Date: Sat, 18 May 2024 14:57:44 +0300 Subject: [PATCH 2/7] Fix wrong comparison --- src/rose/rose_build_bytecode.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rose/rose_build_bytecode.cpp b/src/rose/rose_build_bytecode.cpp index 69628319..d1a49aba 100644 --- a/src/rose/rose_build_bytecode.cpp +++ b/src/rose/rose_build_bytecode.cpp @@ -2966,7 +2966,7 @@ void buildFragmentPrograms(const RoseBuildImpl &build, !lit_prog.empty()) { const auto &cfrag = fragments[pfrag.included_frag_id]; assert(pfrag.s.length() >= cfrag.s.length() && - !pfrag.s.any_nocase() == !cfrag.s.any_nocase()); + !pfrag.s.any_nocase() != !cfrag.s.any_nocase()); /** !pfrag.s.any_nocase() >= !cfrag.s.any_nocase()); **/ u32 child_offset = cfrag.lit_program_offset; DEBUG_PRINTF("child %u offset %u\n", cfrag.fragment_id, From 06339e65adfceafe70755c454c70af588da5fd83 Mon Sep 17 00:00:00 2001 From: Konstantinos Margaritis Date: Sat, 18 May 2024 14:58:11 +0300 Subject: [PATCH 3/7] Fix casts --- src/nfa/castlecompile.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/nfa/castlecompile.cpp b/src/nfa/castlecompile.cpp index 28f1aed9..115646da 100644 --- a/src/nfa/castlecompile.cpp +++ b/src/nfa/castlecompile.cpp @@ -632,11 +632,12 @@ buildCastle(const CastleProto &proto, if (patchSize[i]) { RepeatInfo *info = reinterpret_cast(ptr); - u64a *table = reinterpret_cast(ROUNDUP_PTR(info + + char *info_base = reinterpret_cast(info); + u64a *table = reinterpret_cast(ROUNDUP_PTR(info_base + sizeof(*info), alignof(u64a))); copy(tables.begin() + tableIdx, tables.begin() + tableIdx + patchSize[i], table); - u32 diff = reinterpret_cast(table) - reinterpret_cast(info) + + u32 diff = reinterpret_cast(table) - info_base + sizeof(u64a) * patchSize[i]; info->length = diff; length += diff; From 28adc0782483cadc428f400a369376a73c23417d Mon Sep 17 00:00:00 2001 From: Konstantinos Margaritis Date: Sat, 18 May 2024 15:06:10 +0300 Subject: [PATCH 4/7] Fix more C-style casts --- src/nfa/limex_compile.cpp | 41 +++++++++-------- src/nfa/mcclellancompile.cpp | 86 ++++++++++++++++++------------------ src/nfa/mpvcompile.cpp | 30 +++++++------ src/nfa/nfa_build_util.cpp | 14 +++--- 4 files changed, 86 insertions(+), 85 deletions(-) diff --git a/src/nfa/limex_compile.cpp b/src/nfa/limex_compile.cpp index 43793573..5b28e732 100644 --- a/src/nfa/limex_compile.cpp +++ b/src/nfa/limex_compile.cpp @@ -269,7 +269,7 @@ void maskClear(Mask &m) { template u8 *maskGetByte(Mask &m, u32 bit) { assert(bit < sizeof(m)*8); - u8 *m8 = (u8 *)&m; + u8 *m8 = reinterpret_cast(&m); return m8 + bit/8; } @@ -290,7 +290,7 @@ void maskSetBits(Mask &m, const NFAStateSet &bits) { template bool isMaskZero(Mask &m) { - const u8 *m8 = (u8 *)&m; + const u8 *m8 = reinterpret_cast(&m); for (u32 i = 0; i < sizeof(m); i++) { if (m8[i]) { return false; @@ -303,7 +303,7 @@ bool isMaskZero(Mask &m) { template void maskSetByte(Mask &m, const unsigned int idx, const char val) { assert(idx < sizeof(m)); - char *m8 = (char *)&m; + char *m8 = reinterpret_cast(&m); char &byte = m8[idx]; byte = val; } @@ -1702,7 +1702,7 @@ struct Factory { static void allocState(NFA *nfa, u32 repeatscratchStateSize, u32 repeatStreamState) { - const implNFA_t *limex = (implNFA_t *)getMutableImplNfa(nfa); + const implNFA_t *limex = reinterpret_cast(getMutableImplNfa(nfa)); // LimEx NFAs now store the following in state: // 1. state bitvector (always present) @@ -1768,7 +1768,7 @@ struct Factory { u32 tableOffset, tugMaskOffset; size_t len = repeatAllocSize(br, &tableOffset, &tugMaskOffset); auto info = make_zeroed_bytecode_ptr(len); - char *info_ptr = (char *)info.get(); + char *info_ptr = reinterpret_cast(info.get()); // Collect state space info. RepeatStateInfo rsi(br.type, br.repeatMin, br.repeatMax, br.minPeriod); @@ -1783,8 +1783,7 @@ struct Factory { info->tugMaskOffset = tugMaskOffset; // Fill the RepeatInfo structure. - RepeatInfo *repeat = - (RepeatInfo *)(info_ptr + sizeof(NFARepeatInfo)); + RepeatInfo *repeat = reinterpret_cast(info_ptr + sizeof(NFARepeatInfo)); repeat->type = br.type; repeat->repeatMin = depth_to_u32(br.repeatMin); repeat->repeatMax = depth_to_u32(br.repeatMax); @@ -1810,7 +1809,7 @@ struct Factory { } // Fill the tug mask. - tableRow_t *tugMask = (tableRow_t *)(info_ptr + tugMaskOffset); + tableRow_t *tugMask = reinterpret_cast(info_ptr + tugMaskOffset); for (auto v : br.tug_triggers) { u32 state_id = args.state_ids.at(v); assert(state_id != NO_STATE); @@ -1931,7 +1930,7 @@ struct Factory { const u32 reportListOffset) { DEBUG_PRINTF("exceptionsOffset=%u\n", exceptionsOffset); - exception_t *etable = (exception_t *)((char *)limex + exceptionsOffset); + exception_t *etable = reinterpret_cast(reinterpret_cast(limex) + exceptionsOffset); assert(ISALIGNED(etable)); map exception_by_state; @@ -1979,10 +1978,10 @@ struct Factory { limex->exceptionCount = ecount; if (args.num_states > 64 && args.cc.target_info.has_avx512vbmi()) { - const u8 *exceptionMask = (const u8 *)(&limex->exceptionMask); - u8 *shufMask = (u8 *)&limex->exceptionShufMask; - u8 *bitMask = (u8 *)&limex->exceptionBitMask; - u8 *andMask = (u8 *)&limex->exceptionAndMask; + const u8 *exceptionMask = reinterpret_cast(&limex->exceptionMask); + u8 *shufMask = reinterpret_cast(&limex->exceptionShufMask); + u8 *bitMask = reinterpret_cast(&limex->exceptionBitMask); + u8 *andMask = reinterpret_cast(&limex->exceptionAndMask); u32 tot_cnt = 0; u32 pos = 0; @@ -2042,7 +2041,7 @@ struct Factory { copy(reachMap.begin(), reachMap.end(), &limex->reachMap[0]); // Reach table is right after the LimEx structure. - tableRow_t *reachMask = (tableRow_t *)((char *)limex + reachOffset); + tableRow_t *reachMask = reinterpret_cast(reinterpret_cast(limex) + reachOffset); assert(ISALIGNED(reachMask)); for (size_t i = 0, end = reach.size(); i < end; i++) { maskSetBits(reachMask[i], reach[i]); @@ -2056,7 +2055,7 @@ struct Factory { DEBUG_PRINTF("topsOffset=%u\n", topsOffset); limex->topOffset = topsOffset; - tableRow_t *topMasks = (tableRow_t *)((char *)limex + topsOffset); + tableRow_t *topMasks = reinterpret_cast(reinterpret_cast(limex) + topsOffset); assert(ISALIGNED(topMasks)); for (size_t i = 0, end = tops.size(); i < end; i++) { @@ -2068,8 +2067,8 @@ struct Factory { static void writeAccelSsse3Masks(const NFAStateSet &accelMask, implNFA_t *limex) { - char *perm_base = (char *)&limex->accelPermute; - char *comp_base = (char *)&limex->accelCompare; + char *perm_base = reinterpret_cast(&limex->accelPermute); + char *comp_base = reinterpret_cast(&limex->accelCompare); u32 num = 0; // index in accel table. for (size_t i = accelMask.find_first(); i != accelMask.npos; @@ -2080,8 +2079,8 @@ struct Factory { // PSHUFB permute and compare masks size_t mask_idx = sizeof(u_128) * (state_id / 128U); DEBUG_PRINTF("mask_idx=%zu\n", mask_idx); - u_128 *perm = (u_128 *)(perm_base + mask_idx); - u_128 *comp = (u_128 *)(comp_base + mask_idx); + u_128 *perm = reinterpret_cast(perm_base + mask_idx); + u_128 *comp = reinterpret_cast(comp_base + mask_idx); maskSetByte(*perm, num, ((state_id % 128U) / 8U)); maskSetByte(*comp, num, ~(1U << (state_id % 8U))); } @@ -2099,11 +2098,11 @@ struct Factory { // Write accel lookup table. limex->accelTableOffset = accelTableOffset; copy(accelTable.begin(), accelTable.end(), - (u8 *)((char *)limex + accelTableOffset)); + reinterpret_cast(reinterpret_cast(limex) + accelTableOffset)); // Write accel aux structures. limex->accelAuxOffset = accelAuxOffset; - AccelAux *auxTable = (AccelAux *)((char *)limex + accelAuxOffset); + AccelAux *auxTable = reinterpret_cast(reinterpret_cast(limex) + accelAuxOffset); assert(ISALIGNED(auxTable)); copy(accelAux.begin(), accelAux.end(), auxTable); diff --git a/src/nfa/mcclellancompile.cpp b/src/nfa/mcclellancompile.cpp index bf9b407a..26e22627 100644 --- a/src/nfa/mcclellancompile.cpp +++ b/src/nfa/mcclellancompile.cpp @@ -176,11 +176,11 @@ static mstate_aux *getAux(NFA *n, dstate_id_t i) { assert(isMcClellanType(n->type)); - const mcclellan *m = (mcclellan *)getMutableImplNfa(n); - mstate_aux *aux_base = (mstate_aux *)((char *)n + m->aux_offset); + const mcclellan *m = reinterpret_cast(getImplNfa(n)); + mstate_aux *aux_base = reinterpret_cast(reinterpret_cast(n) + m->aux_offset); mstate_aux *aux = aux_base + i; - assert((const char *)aux < (const char *)n + m->length); + assert(reinterpret_cast(aux) < reinterpret_cast(n) + m->length); return aux; } @@ -190,7 +190,7 @@ void markEdges(NFA *n, u16 *succ_table, const dfa_info &info) { assert(n->type == MCCLELLAN_NFA_16); u8 alphaShift = info.getAlphaShift(); u16 alphaSize = info.impl_alpha_size; - mcclellan *m = (mcclellan *)getMutableImplNfa(n); + mcclellan *m = reinterpret_cast(getMutableImplNfa(n)); /* handle the normal states */ for (u32 i = 0; i < m->sherman_limit; i++) { @@ -215,17 +215,17 @@ void markEdges(NFA *n, u16 *succ_table, const dfa_info &info) { } /* handle the sherman states */ - char *sherman_base_offset = (char *)n + m->sherman_offset; + char *sherman_base_offset = reinterpret_cast(n) + m->sherman_offset; u16 sherman_ceil = m->has_wide == 1 ? m->wide_limit : m->state_count; for (u16 j = m->sherman_limit; j < sherman_ceil; j++) { char *sherman_cur = findMutableShermanState(sherman_base_offset, m->sherman_limit, j); assert(*(sherman_cur + SHERMAN_TYPE_OFFSET) == SHERMAN_STATE); - u8 len = *(u8 *)(sherman_cur + SHERMAN_LEN_OFFSET); - u16 *succs = (u16 *)(sherman_cur + SHERMAN_STATES_OFFSET(len)); + u8 len = *(reinterpret_cast(sherman_cur + SHERMAN_LEN_OFFSET)); + u16 *succs = reinterpret_cast(sherman_cur + SHERMAN_STATES_OFFSET(len)); for (u8 i = 0; i < len; i++) { - u16 succ_i = unaligned_load_u16((u8 *)&succs[i]); + u16 succ_i = unaligned_load_u16(reinterpret_cast(&succs[i])); // wide state has no aux structure. if (m->has_wide && succ_i >= m->wide_limit) { continue; @@ -241,25 +241,25 @@ void markEdges(NFA *n, u16 *succ_table, const dfa_info &info) { succ_i |= ACCEL_FLAG; } - unaligned_store_u16((u8 *)&succs[i], succ_i); + unaligned_store_u16(reinterpret_cast(&succs[i]), succ_i); } } /* handle the wide states */ if (m->has_wide) { u32 wide_limit = m->wide_limit; - char *wide_base = (char *)n + m->wide_offset; + char *wide_base = reinterpret_cast(n) + m->wide_offset; assert(*wide_base == WIDE_STATE); u16 wide_number = verify_u16(info.wide_symbol_chain.size()); // traverse over wide head states. for (u16 j = wide_limit; j < wide_limit + wide_number; j++) { char *wide_cur = findMutableWideEntry16(wide_base, wide_limit, j); - u16 width = *(const u16 *)(wide_cur + WIDE_WIDTH_OFFSET); - u16 *trans = (u16 *)(wide_cur + WIDE_TRANSITION_OFFSET16(width)); + u16 width = *(reinterpret_cast(wide_cur + WIDE_WIDTH_OFFSET)); + u16 *trans = reinterpret_cast(wide_cur + WIDE_TRANSITION_OFFSET16(width)); // check successful transition - u16 next = unaligned_load_u16((u8 *)trans); + u16 next = unaligned_load_u16(reinterpret_cast(trans)); if (next < wide_limit) { const mstate_aux *aux = getAux(n, next); if (aux->accept) { @@ -268,13 +268,13 @@ void markEdges(NFA *n, u16 *succ_table, const dfa_info &info) { if (aux->accel_offset) { next |= ACCEL_FLAG; } - unaligned_store_u16((u8 *)trans, next); + unaligned_store_u16(reinterpret_cast(trans), next); } trans++; // check failure transition for (symbol_t k = 0; k < alphaSize; k++) { - u16 next_k = unaligned_load_u16((u8 *)&trans[k]); + u16 next_k = unaligned_load_u16(reinterpret_cast(&trans[k])); if (next_k >= wide_limit) { continue; } @@ -285,7 +285,7 @@ void markEdges(NFA *n, u16 *succ_table, const dfa_info &info) { if (aux_k->accel_offset) { next_k |= ACCEL_FLAG; } - unaligned_store_u16((u8 *)&trans[k], next_k); + unaligned_store_u16(reinterpret_cast(&trans[k]), next_k); } } } @@ -321,7 +321,7 @@ void populateBasicInfo(size_t state_size, const dfa_info &info, nfa->type = MCCLELLAN_NFA_16; } - mcclellan *m = (mcclellan *)getMutableImplNfa(nfa); + mcclellan *m = reinterpret_cast(getMutableImplNfa(nfa)); for (u32 i = 0; i < 256; i++) { m->remap[i] = verify_u8(info.alpha_remap[i]); } @@ -485,7 +485,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) { @@ -665,7 +665,7 @@ bytecode_ptr mcclellanCompile16(dfa_info &info, const CompileContext &cc, DEBUG_PRINTF("total_size %zu\n", total_size); auto nfa = make_zeroed_bytecode_ptr(total_size); - char *nfa_base = (char *)nfa.get(); + char *nfa_base = reinterpret_cast(nfa.get()); populateBasicInfo(sizeof(u16), info, total_size, aux_offset, accel_offset, accel_escape_info.size(), arb, single, nfa.get()); @@ -674,9 +674,9 @@ bytecode_ptr mcclellanCompile16(dfa_info &info, const CompileContext &cc, ri->fillReportLists(nfa.get(), aux_offset + aux_size, reportOffsets); - u16 *succ_table = (u16 *)(nfa_base + sizeof(NFA) + sizeof(mcclellan)); - mstate_aux *aux = (mstate_aux *)(nfa_base + aux_offset); - mcclellan *m = (mcclellan *)getMutableImplNfa(nfa.get()); + u16 *succ_table = reinterpret_cast(nfa_base + sizeof(NFA) + sizeof(mcclellan)); + mstate_aux *aux = reinterpret_cast(nfa_base + aux_offset); + mcclellan *m = reinterpret_cast(getMutableImplNfa(nfa.get())); m->wide_limit = wide_limit; m->wide_offset = wide_offset; @@ -710,7 +710,7 @@ bytecode_ptr mcclellanCompile16(dfa_info &info, const CompileContext &cc, assert(accel_offset + sizeof(NFA) <= sherman_offset); assert(ISALIGNED_N(accel_offset, alignof(union AccelAux))); info.strat.buildAccel(i, accel_escape_info.at(i), - (void *)((char *)m + this_aux->accel_offset)); + reinterpret_cast(reinterpret_cast(m) + this_aux->accel_offset)); } } @@ -740,17 +740,17 @@ bytecode_ptr mcclellanCompile16(dfa_info &info, const CompileContext &cc, assert(accel_offset + sizeof(NFA) <= sherman_offset); assert(ISALIGNED_N(accel_offset, alignof(union AccelAux))); info.strat.buildAccel(i, accel_escape_info.at(i), - (void *)((char *)m + this_aux->accel_offset)); + reinterpret_cast(reinterpret_cast(m) + this_aux->accel_offset)); } u8 len = verify_u8(info.impl_alpha_size - info.extra[i].daddytaken); assert(len <= 9); dstate_id_t d = info.states[i].daddy; - *(u8 *)(curr_sherman_entry + SHERMAN_TYPE_OFFSET) = SHERMAN_STATE; - *(u8 *)(curr_sherman_entry + SHERMAN_LEN_OFFSET) = len; - *(u16 *)(curr_sherman_entry + SHERMAN_DADDY_OFFSET) = info.implId(d); - u8 *chars = (u8 *)(curr_sherman_entry + SHERMAN_CHARS_OFFSET); + *(reinterpret_cast(curr_sherman_entry + SHERMAN_TYPE_OFFSET)) = SHERMAN_STATE; + *(reinterpret_cast(curr_sherman_entry + SHERMAN_LEN_OFFSET)) = len; + *(reinterpret_cast(curr_sherman_entry + SHERMAN_DADDY_OFFSET)) = info.implId(d); + u8 *chars = reinterpret_cast(curr_sherman_entry + SHERMAN_CHARS_OFFSET); for (u16 s = 0; s < info.impl_alpha_size; s++) { if (info.states[i].next[s] != info.states[d].next[s]) { @@ -758,13 +758,13 @@ bytecode_ptr mcclellanCompile16(dfa_info &info, const CompileContext &cc, } } - u16 *states = (u16 *)(curr_sherman_entry + SHERMAN_STATES_OFFSET(len)); + u16 *states = reinterpret_cast(curr_sherman_entry + SHERMAN_STATES_OFFSET(len)); for (u16 s = 0; s < info.impl_alpha_size; s++) { if (info.states[i].next[s] != info.states[d].next[s]) { DEBUG_PRINTF("s overrider %hu dad %hu char next %hu\n", fs, info.implId(d), info.implId(info.states[i].next[s])); - unaligned_store_u16((u8 *)states++, + unaligned_store_u16(reinterpret_cast(states++), info.implId(info.states[i].next[s])); } } @@ -777,13 +777,13 @@ bytecode_ptr mcclellanCompile16(dfa_info &info, const CompileContext &cc, assert(ISALIGNED_16(wide_base)); char *wide_top = wide_base; - *(u8 *)(wide_top++) = WIDE_STATE; + *(reinterpret_cast(wide_top++)) = WIDE_STATE; wide_top = ROUNDUP_PTR(wide_top, 2); - *(u16 *)(wide_top) = wide_number; + *(reinterpret_cast(wide_top)) = wide_number; wide_top += 2; char *curr_wide_entry = wide_top + wide_number * sizeof(u32); - u32 *wide_offset_list = (u32 *)wide_top; + u32 *wide_offset_list = reinterpret_cast(wide_top); /* get the order of writing wide states */ vector order(wide_number); @@ -798,8 +798,8 @@ bytecode_ptr mcclellanCompile16(dfa_info &info, const CompileContext &cc, const vector &symbol_chain = info.wide_symbol_chain[i]; u16 width = verify_u16(symbol_chain.size()); - *(u16 *)(curr_wide_entry + WIDE_WIDTH_OFFSET) = width; - u8 *chars = (u8 *)(curr_wide_entry + WIDE_SYMBOL_OFFSET16); + *(reinterpret_cast(curr_wide_entry + WIDE_WIDTH_OFFSET)) = width; + u8 *chars = reinterpret_cast(curr_wide_entry + WIDE_SYMBOL_OFFSET16); // store wide state symbol chain for (size_t j = 0; j < width; j++) { @@ -807,7 +807,7 @@ bytecode_ptr mcclellanCompile16(dfa_info &info, const CompileContext &cc, } // store wide state transition table - u16 *trans = (u16 *)(curr_wide_entry + u16 *trans = reinterpret_cast(curr_wide_entry + WIDE_TRANSITION_OFFSET16(width)); dstate_id_t tail = state_chain[width - 1]; symbol_t last = symbol_chain[width -1]; @@ -831,7 +831,7 @@ bytecode_ptr mcclellanCompile16(dfa_info &info, const CompileContext &cc, *wide_offset_list++ = verify_u32(curr_wide_entry - wide_base); - curr_wide_entry = (char *)trans; + curr_wide_entry = reinterpret_cast(trans); } } @@ -953,9 +953,9 @@ bytecode_ptr mcclellanCompile8(dfa_info &info, const CompileContext &cc, assert(ISALIGNED_N(accel_offset, alignof(union AccelAux))); auto nfa = make_zeroed_bytecode_ptr(total_size); - char *nfa_base = (char *)nfa.get(); + char *nfa_base = reinterpret_cast(nfa.get()); - mcclellan *m = (mcclellan *)getMutableImplNfa(nfa.get()); + mcclellan *m = reinterpret_cast(getMutableImplNfa(nfa.get())); allocateFSN8(info, accel_escape_info, &m->accel_limit_8, &m->accept_limit_8); @@ -967,8 +967,8 @@ bytecode_ptr mcclellanCompile8(dfa_info &info, const CompileContext &cc, ri->fillReportLists(nfa.get(), aux_offset + aux_size, reportOffsets); /* copy in the state information */ - u8 *succ_table = (u8 *)(nfa_base + sizeof(NFA) + sizeof(mcclellan)); - mstate_aux *aux = (mstate_aux *)(nfa_base + aux_offset); + u8 *succ_table = reinterpret_cast(nfa_base + sizeof(NFA) + sizeof(mcclellan)); + mstate_aux *aux = reinterpret_cast(nfa_base + aux_offset); for (size_t i = 0; i < info.size(); i++) { if (contains(accel_escape_info, i)) { @@ -978,7 +978,7 @@ bytecode_ptr mcclellanCompile8(dfa_info &info, const CompileContext &cc, accel_offset += info.strat.accelSize(); info.strat.buildAccel(i, accel_escape_info.at(i), - (void *)((char *)m + aux[j].accel_offset)); + reinterpret_cast(reinterpret_cast(m) + aux[j].accel_offset)); } fillInBasicState8(info, aux, succ_table, reportOffsets, reports, @@ -1550,7 +1550,7 @@ u32 mcclellanStartReachSize(const raw_dfa *raw) { } bool has_accel_mcclellan(const NFA *nfa) { - const mcclellan *m = (const mcclellan *)getImplNfa(nfa); + const mcclellan *m = reinterpret_cast(getImplNfa(nfa)); return m->has_accel; } diff --git a/src/nfa/mpvcompile.cpp b/src/nfa/mpvcompile.cpp index d85c90b0..69ddfdcb 100644 --- a/src/nfa/mpvcompile.cpp +++ b/src/nfa/mpvcompile.cpp @@ -180,25 +180,27 @@ void writeKiloPuff(const map>::const_iterator &it, #ifdef HAVE_SVE2 } else if (reach.count() >= 240) { kp->type = MPV_VERM16; - vermicelli16Build(~reach, (u8 *)&kp->u.verm16.mask); + vermicelli16Build(~reach, reinterpret_casT(&kp->u.verm16.mask)); } else if (reach.count() <= 16) { kp->type = MPV_NVERM16; - vermicelli16Build(reach, (u8 *)&kp->u.verm16.mask); + vermicelli16Build(reach, reinterpret_cast(&kp->u.verm16.mask)); #endif // HAVE_SVE2 - } else if (shuftiBuildMasks(~reach, (u8 *)&kp->u.shuf.mask_lo, - (u8 *)&kp->u.shuf.mask_hi) != -1) { + } else if (shuftiBuildMasks(~reach, + reinterpret_cast(&kp->u.shuf.mask_lo), + reinterpret_cast(&kp->u.shuf.mask_hi)) != -1) { kp->type = MPV_SHUFTI; } else { kp->type = MPV_TRUFFLE; - truffleBuildMasks(~reach, (u8 *)&kp->u.truffle.mask1, - (u8 *)&kp->u.truffle.mask2); + truffleBuildMasks(~reach, + reinterpret_cast(&kp->u.truffle.mask1), + reinterpret_cast(&kp->u.truffle.mask2)); } kp->count = verify_u32(puffs.size()); kp->counter_offset = counter_offset; /* start of real puffette array */ - kp->puffette_offset = verify_u32((char *)*pa - (char *)m); + kp->puffette_offset = verify_u32(reinterpret_cast(*pa) - reinterpret_cast(m)); for (size_t i = 0; i < puffs.size(); i++) { assert(!it->first.auto_restart || puffs[i].unbounded); writePuffette(*pa + i, puffs[i], rm); @@ -355,8 +357,8 @@ bytecode_ptr mpvCompile(const vector &puffs_in, auto nfa = make_zeroed_bytecode_ptr(len); - mpv_puffette *pa_base = (mpv_puffette *) - ((char *)nfa.get() + sizeof(NFA) + sizeof(mpv) + char *nfa_base = reinterpret_cast(nfa.get()); + mpv_puffette *pa_base = reinterpret_cast(nfa_base + sizeof(NFA) + sizeof(mpv) + sizeof(mpv_kilopuff) * puff_clusters.size() + sizeof(mpv_counter_info) * counters.size()); mpv_puffette *pa = pa_base; @@ -373,7 +375,7 @@ bytecode_ptr mpvCompile(const vector &puffs_in, min_repeat = min(min_repeat, puffs.front().repeats); } - mpv *m = (mpv *)getMutableImplNfa(nfa.get()); + mpv *m = reinterpret_cast(getMutableImplNfa(nfa.get())); m->kilo_count = verify_u32(puff_clusters.size()); m->counter_count = verify_u32(counters.size()); m->puffette_count = puffette_count; @@ -384,7 +386,7 @@ bytecode_ptr mpvCompile(const vector &puffs_in, m->top_kilo_begin = verify_u32(triggered_puffs.size()); m->top_kilo_end = verify_u32(puff_clusters.size()); - mpv_kilopuff *kp_begin = (mpv_kilopuff *)(m + 1); + mpv_kilopuff *kp_begin = reinterpret_cast(m + 1); mpv_kilopuff *kp = kp_begin; for (auto it = puff_clusters.begin(); it != puff_clusters.end(); ++it) { writeKiloPuff(it, rm, @@ -392,14 +394,14 @@ bytecode_ptr mpvCompile(const vector &puffs_in, kp, &pa); ++kp; } - assert((char *)pa == (char *)nfa.get() + len); + assert(reinterpret_cast(pa) == nfa_base + len); - mpv_counter_info *out_ci = (mpv_counter_info *)kp; + mpv_counter_info *out_ci = reinterpret_cast(kp); for (const auto &counter : counters) { *out_ci = counter; ++out_ci; } - assert((char *)out_ci == (char *)pa_base); + assert(reinterpret_cast(out_ci) == reinterpret_cast(pa_base)); writeCoreNfa(nfa.get(), len, min_repeat, max_counter, curr_comp_offset, curr_decomp_offset); diff --git a/src/nfa/nfa_build_util.cpp b/src/nfa/nfa_build_util.cpp index 522ab231..7139ce99 100644 --- a/src/nfa/nfa_build_util.cpp +++ b/src/nfa/nfa_build_util.cpp @@ -86,14 +86,14 @@ typedef bool (*nfa_dispatch_fn)(const NFA *nfa); template static bool has_accel_limex(const NFA *nfa) { - const T *limex = (const T *)getImplNfa(nfa); + const T *limex = reinterpret_cast(getImplNfa(nfa)); return limex->accelCount; } template static bool has_repeats_limex(const NFA *nfa) { - const T *limex = (const T *)getImplNfa(nfa); + const T *limex = reinterpret_cast(getImplNfa(nfa)); return limex->repeatCount; } @@ -101,16 +101,16 @@ bool has_repeats_limex(const NFA *nfa) { template static bool has_repeats_other_than_firsts_limex(const NFA *nfa) { - const T *limex = (const T *)getImplNfa(nfa); - const char *ptr = (const char *)limex; + const T *limex = reinterpret_cast(getImplNfa(nfa)); + const char *ptr = reinterpret_cast(limex); - const u32 *repeatOffset = (const u32 *)(ptr + limex->repeatOffset); + const u32 *repeatOffset = reinterpret_cast(ptr + limex->repeatOffset); for (u32 i = 0; i < limex->repeatCount; i++) { u32 offset = repeatOffset[i]; - const NFARepeatInfo *info = (const NFARepeatInfo *)(ptr + offset); + const NFARepeatInfo *info = reinterpret_cast(ptr + offset); const RepeatInfo *repeat = - (const RepeatInfo *)((const char *)info + sizeof(*info)); + reinterpret_cast(reinterpret_cast(info) + sizeof(*info)); if (repeat->type != REPEAT_FIRST) { return true; } From 92d5db503e0548e92bb0dd5bd254f97208cc8719 Mon Sep 17 00:00:00 2001 From: Konstantinos Margaritis Date: Sat, 18 May 2024 21:47:47 +0300 Subject: [PATCH 5/7] remove unneeded suppression --- src/nfa/goughcompile.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/nfa/goughcompile.cpp b/src/nfa/goughcompile.cpp index 6366c76f..e9408b51 100644 --- a/src/nfa/goughcompile.cpp +++ b/src/nfa/goughcompile.cpp @@ -1077,7 +1077,6 @@ bytecode_ptr goughCompile(raw_som_dfa &raw, u8 somPrecision, return bytecode_ptr(nullptr); } - // cppcheck-suppress cstyleCast const auto nfa = static_cast(getImplNfa(basic_dfa.get())); u8 alphaShift = nfa->alphaShift; u32 edge_count = (1U << alphaShift) * raw.states.size(); @@ -1134,7 +1133,6 @@ bytecode_ptr 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)); - // cppcheck-suppress cstyleCast auto *m = reinterpret_cast(getMutableImplNfa(gough_dfa.get())); m->haig_offset = haig_offset; From 40da067b4f9e1bdb21dc5e5180262889006af02e Mon Sep 17 00:00:00 2001 From: Konstantinos Margaritis Date: Sat, 18 May 2024 21:48:40 +0300 Subject: [PATCH 6/7] 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) { From dc18a14663d4aa36907bc1427574c986b0f8ccee Mon Sep 17 00:00:00 2001 From: Konstantinos Margaritis Date: Sat, 18 May 2024 22:52:17 +0300 Subject: [PATCH 7/7] Fix typo --- src/nfa/mpvcompile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nfa/mpvcompile.cpp b/src/nfa/mpvcompile.cpp index 69ddfdcb..7c06da9d 100644 --- a/src/nfa/mpvcompile.cpp +++ b/src/nfa/mpvcompile.cpp @@ -180,7 +180,7 @@ void writeKiloPuff(const map>::const_iterator &it, #ifdef HAVE_SVE2 } else if (reach.count() >= 240) { kp->type = MPV_VERM16; - vermicelli16Build(~reach, reinterpret_casT(&kp->u.verm16.mask)); + vermicelli16Build(~reach, reinterpret_cast(&kp->u.verm16.mask)); } else if (reach.count() <= 16) { kp->type = MPV_NVERM16; vermicelli16Build(reach, reinterpret_cast(&kp->u.verm16.mask));