From f31839772687528b123d8031d92c2638f426d16a Mon Sep 17 00:00:00 2001 From: bol-van Date: Wed, 7 Jan 2026 13:43:14 +0300 Subject: [PATCH] AI inspired fixes --- ip2net/ip2net.c | 4 ++-- mdig/mdig.c | 2 +- nfq2/helpers.c | 33 ++------------------------------- nfq2/helpers.h | 12 ------------ nfq2/nfqws.c | 1 - 5 files changed, 5 insertions(+), 47 deletions(-) diff --git a/ip2net/ip2net.c b/ip2net/ip2net.c index 77d7139..9badb08 100644 --- a/ip2net/ip2net.c +++ b/ip2net/ip2net.c @@ -49,7 +49,7 @@ static int ucmp(const void * a, const void * b, void *arg) } static uint32_t mask_from_bitcount(uint32_t zct) { - return zct<32 ? ~((1 << zct) - 1) : 0; + return zct<32 ? ~((1u << zct) - 1) : 0; } // make presorted array unique. return number of unique items. // 1,1,2,3,3,0,0,0 (ct=8) => 1,2,3,0 (ct=4) @@ -138,7 +138,7 @@ static void mask_from_bitcount6_make(uint32_t zct, struct in6_addr *a) int32_t n = (127 - zct) >> 3; memset(a->s6_addr,0xFF,n); memset(a->s6_addr+n,0x00,16-n); - a->s6_addr[n] = ~((1 << (zct & 7)) - 1); + a->s6_addr[n] = ~((1u << (zct & 7)) - 1); } } static struct in6_addr ip6_mask[129]; diff --git a/mdig/mdig.c b/mdig/mdig.c index b2447f6..847f283 100644 --- a/mdig/mdig.c +++ b/mdig/mdig.c @@ -87,7 +87,7 @@ static bool dom_valid(char *dom) static void invalid_domain_beautify(char *dom) { for (int i = 0; *dom && i < 64; i++, dom++) - if (*dom < 0x20 || *dom>0x7F) *dom = '?'; + if (*dom < 0x20 || *dom<0) *dom = '?'; if (*dom) *dom = 0; } diff --git a/nfq2/helpers.c b/nfq2/helpers.c index bd7aa5a..09e4a1c 100644 --- a/nfq2/helpers.c +++ b/nfq2/helpers.c @@ -12,7 +12,7 @@ #define UNIQ_SORT \ { \ - int i, j, u; \ + size_t i, j, u; \ for (i = j = 0; j < ct; i++) \ { \ u = pu[j++]; \ @@ -642,42 +642,13 @@ bool set_env_exedir(const char *argv0) if ((s = strdup(argv0))) { if ((d = dirname(s))) - setenv("EXEDIR",s,1); + bOK = !setenv("EXEDIR",d,1); free(s); } return bOK; } -static void mask_from_preflen6_make(uint8_t plen, struct in6_addr *a) -{ - if (plen >= 128) - memset(a->s6_addr,0xFF,16); - else - { - uint8_t n = plen >> 3; - memset(a->s6_addr,0xFF,n); - memset(a->s6_addr+n,0x00,16-n); - a->s6_addr[n] = (uint8_t)(0xFF00 >> (plen & 7)); - } -} -struct in6_addr ip6_mask[129]; -void mask_from_preflen6_prepare(void) -{ - for (int plen=0;plen<=128;plen++) mask_from_preflen6_make(plen, ip6_mask+plen); -} - -#if defined(__GNUC__) && !defined(__llvm__) -__attribute__((optimize ("no-strict-aliasing"))) -#endif -void ip6_and(const struct in6_addr * restrict a, const struct in6_addr * restrict b, struct in6_addr * restrict result) -{ - // int128 requires 16-bit alignment. in struct sockaddr_in6.sin6_addr is 8-byte aligned. - // it causes segfault on x64 arch with latest compiler. it can cause misalign slowdown on other archs - // use 64-bit AND - ((uint64_t*)result->s6_addr)[0] = ((uint64_t*)a->s6_addr)[0] & ((uint64_t*)b->s6_addr)[0]; - ((uint64_t*)result->s6_addr)[1] = ((uint64_t*)a->s6_addr)[1] & ((uint64_t*)b->s6_addr)[1]; -} void str_cidr4(char *s, size_t s_len, const struct cidr4 *cidr) { diff --git a/nfq2/helpers.h b/nfq2/helpers.h index 254e9b6..2012403 100644 --- a/nfq2/helpers.h +++ b/nfq2/helpers.h @@ -139,15 +139,3 @@ bool parse_cidr4(char *s, struct cidr4 *cidr); bool parse_cidr6(char *s, struct cidr6 *cidr); bool parse_int16(const char *p, int16_t *v); - -static inline uint32_t mask_from_preflen(uint32_t preflen) -{ - return preflen ? preflen<32 ? ~((1 << (32-preflen)) - 1) : 0xFFFFFFFF : 0; -} -void ip6_and(const struct in6_addr * restrict a, const struct in6_addr * restrict b, struct in6_addr * restrict result); -extern struct in6_addr ip6_mask[129]; -void mask_from_preflen6_prepare(void); -static inline const struct in6_addr *mask_from_preflen6(uint8_t preflen) -{ - return ip6_mask+preflen; -} diff --git a/nfq2/nfqws.c b/nfq2/nfqws.c index 4507f01..8706ec7 100644 --- a/nfq2/nfqws.c +++ b/nfq2/nfqws.c @@ -1732,7 +1732,6 @@ int main(int argc, char **argv) srandom(time(NULL)); aes_init_keygen_tables(); // required for aes - mask_from_preflen6_prepare(); set_env_exedir(argv[0]); set_console_io_buffering(); #ifdef __CYGWIN__