AI inspired fixes

This commit is contained in:
bol-van
2026-01-07 13:41:17 +03:00
parent 3e35fb9025
commit 6ac8ed4c3f
9 changed files with 8 additions and 92 deletions

View File

@@ -87,7 +87,7 @@ static bool dom_valid(char *dom)
static void invalid_domain_beautify(char *dom) static void invalid_domain_beautify(char *dom)
{ {
for (int i = 0; *dom && i < 64; i++, 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; if (*dom) *dom = 0;
} }

View File

@@ -13,7 +13,7 @@
int unique_size_t(size_t *pu, int ct) int unique_size_t(size_t *pu, int ct)
{ {
int i, j, u; size_t i, j, u;
for (i = j = 0; j < ct; i++) for (i = j = 0; j < ct; i++)
{ {
u = pu[j++]; u = pu[j++];
@@ -477,43 +477,13 @@ bool set_env_exedir(const char *argv0)
if ((s = strdup(argv0))) if ((s = strdup(argv0)))
{ {
if ((d = dirname(s))) if ((d = dirname(s)))
setenv("EXEDIR",s,1); bOK = !setenv("EXEDIR",d,1);
free(s); free(s);
} }
return bOK; 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) void str_cidr4(char *s, size_t s_len, const struct cidr4 *cidr)
{ {
char s_ip[16]; char s_ip[16];

View File

@@ -118,15 +118,3 @@ void str_cidr6(char *s, size_t s_len, const struct cidr6 *cidr);
void print_cidr6(const struct cidr6 *cidr); void print_cidr6(const struct cidr6 *cidr);
bool parse_cidr4(char *s, struct cidr4 *cidr); bool parse_cidr4(char *s, struct cidr4 *cidr);
bool parse_cidr6(char *s, struct cidr6 *cidr); bool parse_cidr6(char *s, struct cidr6 *cidr);
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;
}

View File

@@ -2304,7 +2304,6 @@ int main(int argc, char **argv)
#endif #endif
srandom(time(NULL)); srandom(time(NULL));
mask_from_preflen6_prepare();
PRINT_VER; PRINT_VER;

View File

@@ -289,14 +289,14 @@ struct desync_profile_list *dp_list_add(struct desync_profile_list_head *head)
dp_init(&entry->dp); dp_init(&entry->dp);
// add to the tail // add to the tail
struct desync_profile_list *dpn,*dpl=LIST_FIRST(&params.desync_profiles); struct desync_profile_list *dpn,*dpl=LIST_FIRST(head);
if (dpl) if (dpl)
{ {
while ((dpn=LIST_NEXT(dpl,next))) dpl = dpn; while ((dpn=LIST_NEXT(dpl,next))) dpl = dpn;
LIST_INSERT_AFTER(dpl, entry, next); LIST_INSERT_AFTER(dpl, entry, next);
} }
else else
LIST_INSERT_HEAD(&params.desync_profiles, entry, next); LIST_INSERT_HEAD(head, entry, next);
return entry; return entry;
} }

View File

@@ -27,7 +27,7 @@
int unique_size_t(size_t *pu, int ct) int unique_size_t(size_t *pu, int ct)
{ {
int i, j, u; size_t i, j, u;
for (i = j = 0; j < ct; i++) for (i = j = 0; j < ct; i++)
{ {
u = pu[j++]; u = pu[j++];
@@ -476,34 +476,6 @@ bool set_env_exedir(const char *argv0)
} }
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)
{
// int 128 can cause alignment segfaults because sin6_addr in struct sockaddr_in6 is 8-byte aligned, not 16-byte
((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) void str_cidr4(char *s, size_t s_len, const struct cidr4 *cidr)
{ {
char s_ip[16]; char s_ip[16];

View File

@@ -124,18 +124,6 @@ void print_cidr6(const struct cidr6 *cidr);
bool parse_cidr4(char *s, struct cidr4 *cidr); bool parse_cidr4(char *s, struct cidr4 *cidr);
bool parse_cidr6(char *s, struct cidr6 *cidr); bool parse_cidr6(char *s, struct cidr6 *cidr);
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;
}
void msleep(unsigned int ms); void msleep(unsigned int ms);
#ifdef __linux__ #ifdef __linux__
bool socket_supports_notsent(); bool socket_supports_notsent();

View File

@@ -219,14 +219,14 @@ struct desync_profile_list *dp_list_add(struct desync_profile_list_head *head)
dp_init(&entry->dp); dp_init(&entry->dp);
// add to the tail // add to the tail
struct desync_profile_list *dpn,*dpl=LIST_FIRST(&params.desync_profiles); struct desync_profile_list *dpn,*dpl=LIST_FIRST(head);
if (dpl) if (dpl)
{ {
while ((dpn=LIST_NEXT(dpl,next))) dpl = dpn; while ((dpn=LIST_NEXT(dpl,next))) dpl = dpn;
LIST_INSERT_AFTER(dpl, entry, next); LIST_INSERT_AFTER(dpl, entry, next);
} }
else else
LIST_INSERT_HEAD(&params.desync_profiles, entry, next); LIST_INSERT_HEAD(head, entry, next);
return entry; return entry;
} }

View File

@@ -1897,7 +1897,6 @@ int main(int argc, char *argv[])
set_console_io_buffering(); set_console_io_buffering();
set_env_exedir(argv[0]); set_env_exedir(argv[0]);
srand(time(NULL)); srand(time(NULL));
mask_from_preflen6_prepare();
PRINT_VER; PRINT_VER;