Skip to content

Commit

Permalink
Remove syntax shugar: mem_set(), mem_bzero(), zalloc(), zallocarray()…
Browse files Browse the repository at this point in the history
…, mem_new(), mem_znew(), mallocarray().
  • Loading branch information
rozhuk-im committed Apr 28, 2024
1 parent bde84f2 commit abf5e88
Show file tree
Hide file tree
Showing 43 changed files with 141 additions and 166 deletions.
2 changes: 1 addition & 1 deletion include/al/hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

#include <inttypes.h>
#include <errno.h>
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
#include <string.h> /* memcpy, memmove, memset, strerror... */
#include <unistd.h> /* close, write, sysconf */
#include <fcntl.h> /* open, fcntl */
#include <cpuid.h>
Expand Down
2 changes: 1 addition & 1 deletion include/al/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include <errno.h>
#include <time.h>
#include <fcntl.h> /* open, fcntl */
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strnlen, strerror... */
#include <string.h> /* memcpy, memmove, memset, strnlen, strerror... */
#include <strings.h> /* explicit_bzero */
#include <unistd.h> /* close, write, sysconf */
#include <stdlib.h> /* malloc, realloc */
Expand Down
2 changes: 1 addition & 1 deletion include/crypto/hash/gost3411-2012.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

#include <sys/param.h>
#include <sys/types.h>
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
#include <string.h> /* memcpy, memmove, memset, strerror... */
#include <inttypes.h>
#ifdef __SSE2__
# include <cpuid.h>
Expand Down
2 changes: 1 addition & 1 deletion include/crypto/hash/md5.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#include <sys/param.h>
#include <sys/types.h>
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
#include <string.h> /* memcpy, memmove, memset, strerror... */
#include <inttypes.h>

#ifndef nitems /* SIZEOF() */
Expand Down
2 changes: 1 addition & 1 deletion include/crypto/hash/sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

#include <sys/param.h>
#include <sys/types.h>
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
#include <string.h> /* memcpy, memmove, memset, strerror... */
#include <inttypes.h>
#ifdef __SSE2__
# include <cpuid.h>
Expand Down
2 changes: 1 addition & 1 deletion include/crypto/hash/sha2.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#include <sys/param.h>
#include <sys/types.h>
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
#include <string.h> /* memcpy, memmove, memset, strerror... */
#include <inttypes.h>
#if defined(__SHA__) && defined(__SSSE3__) && defined(__SSE4_1__)
# include <cpuid.h>
Expand Down
2 changes: 1 addition & 1 deletion include/math/big_num.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#else
# include <sys/types.h>
# include <inttypes.h>
# include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strnlen, strerror... */
# include <string.h> /* memcpy, memmove, memset, strnlen, strerror... */
# include <stdio.h> /* snprintf, fprintf */
# ifndef __unused
# define __unused __attribute__((__unused__))
Expand Down
10 changes: 5 additions & 5 deletions include/net/host_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <inttypes.h>
#include <netdb.h>
#include <errno.h>
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
#include <string.h> /* memcpy, memmove, memset, strerror... */
#include <stdlib.h> /* malloc, exit */

#include "utils/macro.h"
Expand Down Expand Up @@ -75,7 +75,7 @@ host_addr_alloc(const uint8_t *name, size_t name_size, uint16_t def_port) {
name_size = (size_t)(ptm_end - name - 1);
}

haddr = zalloc((sizeof(host_addr_t) + name_size + sizeof(void*)));
haddr = calloc(1, (sizeof(host_addr_t) + name_size + sizeof(void*)));
if (NULL == haddr)
return (haddr);
haddr->name = (uint8_t*)(haddr + 1);
Expand All @@ -94,10 +94,10 @@ host_addr_clone(host_addr_p src) {
if (NULL == src)
return (NULL);

haddr = zallocarray(src->allocated, sizeof(sockaddr_storage_t));
haddr = calloc(src->allocated, sizeof(sockaddr_storage_t));
if (NULL == haddr)
return (haddr);
haddr->addrs = zalloc((sizeof(host_addr_t) + src->name_size + sizeof(void*)));
haddr->addrs = calloc(1, (sizeof(host_addr_t) + src->name_size + sizeof(void*)));
if (NULL == haddr->addrs) {
free(haddr);
return (NULL);
Expand Down Expand Up @@ -200,7 +200,7 @@ host_addr_resolv(host_addr_p haddr) {
if (NULL == haddr)
return (EINVAL);

mem_bzero(&hints, sizeof(hints));
memset(&hints, 0x00, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_flags = AI_NUMERICSERV;
u162str(haddr->port, servname, sizeof(servname), NULL); /* Should not fail. */
Expand Down
8 changes: 5 additions & 3 deletions include/net/hostname_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#include <sys/types.h>
#include <inttypes.h>
#include <string.h> /* memcpy, memmove, memset, strerror... */
#include <strings.h> /* explicit_bzero */
#include "utils/mem_utils.h"

#define HOSTNAME_PREALLOC 8
Expand All @@ -56,7 +58,7 @@ hostname_list_init(hostname_list_p hn_lst) {

if (NULL == hn_lst)
return (EINVAL);
mem_bzero(hn_lst, sizeof(hostname_list_t));
explicit_bzero(hn_lst, sizeof(hostname_list_t));

return (0);
}
Expand Down Expand Up @@ -84,7 +86,7 @@ static inline hostname_list_p
hostname_list_alloc(void) {
hostname_list_p hn_lst;

hn_lst = mem_znew(hostname_list_t);
hn_lst = calloc(1, sizeof(hostname_list_t));
if (NULL == hn_lst)
return (hn_lst);
if (0 != hostname_list_init(hn_lst)) {
Expand All @@ -103,7 +105,7 @@ hostname_list_clone(hostname_list_p hn_lst) {
hn_new = mem_dup(hn_lst, sizeof(hostname_list_t));
if (NULL == hn_new)
return (hn_new);
hn_new->names = zallocarray(hn_new->allocated, sizeof(hostname_p));
hn_new->names = calloc(hn_new->allocated, sizeof(hostname_p));
if (NULL == hn_new->names) {
free(hn_new);
return (NULL);
Expand Down
2 changes: 1 addition & 1 deletion include/proto/dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include <sys/types.h>
#include <inttypes.h>
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strnlen, strerror... */
#include <string.h> /* memcpy, memmove, memset, strnlen, strerror... */
#include <netinet/in.h> /* ntohs(), htons(), ntohl(), htonl() */
#include "utils/mem_utils.h"

Expand Down
2 changes: 1 addition & 1 deletion include/proto/radius.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

#include <sys/types.h>
#include <inttypes.h>
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strnlen, strerror... */
#include <string.h> /* memcpy, memmove, memset, strnlen, strerror... */
#include <sys/socket.h>
#include <netinet/in.h> /* ntohs(), htons() */
#include <netinet/tcp.h>
Expand Down
2 changes: 1 addition & 1 deletion include/utils/hash_bucket.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#else /* User space */
# include <pthread.h>
# include <errno.h>
# include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
# include <string.h> /* memcpy, memmove, memset, strerror... */
# include <stdlib.h> /* malloc, exit */

# define HB_ALLOC(size) malloc(size)
Expand Down
29 changes: 4 additions & 25 deletions include/utils/mem_utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2004 - 2020 Rozhuk Ivan <[email protected]>
* Copyright (c) 2004-2024 Rozhuk Ivan <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -478,18 +478,6 @@ mem_cmpin(const void *buf1, const size_t buf1_size,
////////////////////////////////////////////////////////////////////////
/////////////////// Memory management wrappers. ////////////////////////
////////////////////////////////////////////////////////////////////////
/* Secure version of memset(). */
static inline void *
mem_set(void *buf, const size_t size, const uint8_t c) {

if (NULL == buf || 0 == size)
return (buf);
return (memset_volatile(buf, c, size));
}

#define mem_bzero(__buf, __size) mem_set((__buf), (size_t)(__size), 0x00)


static inline void *
mem_dup2(const void *buf, const size_t size, const size_t pad_size) {
void *ret;
Expand All @@ -499,23 +487,14 @@ mem_dup2(const void *buf, const size_t size, const size_t pad_size) {
if (NULL == ret)
return (ret);
memcpy(ret, buf, size);
mem_bzero((((uint8_t*)ret) + size), (alloc_sz - size));
memset((((uint8_t*)ret) + size), 0x00, (alloc_sz - size));

return (ret);
}

#define mem_dup(__buf, __size) mem_dup2((__buf), (__size), 0)


/* Allocate and zero memory. */
#define zalloc(__size) calloc(1, (size_t)(__size))
#define zallocarray(__nmemb, __size) calloc((__nmemb), (size_t)(__size))

#define mem_new(__type) (__type*)malloc(sizeof(__type))
#define mem_znew(__type) (__type*)zalloc(sizeof(__type))

#define mallocarray(__nmemb, __size) reallocarray(NULL, (__nmemb), (size_t)(__size))

static inline int
realloc_items(void **items, const size_t item_size,
size_t *allocated, const size_t alloc_blk_cnt, const size_t count) {
Expand All @@ -534,7 +513,7 @@ realloc_items(void **items, const size_t item_size,
if (NULL == items_new) /* Realloc fail! */
return (ENOMEM);
if (allocated_new > allocated_prev) { /* Init new mem. */
mem_bzero((items_new + (allocated_prev * item_size)),
memset((items_new + (allocated_prev * item_size)), 0x00,
((allocated_new - allocated_prev) * item_size));
}
(*items) = items_new;
Expand Down Expand Up @@ -591,7 +570,7 @@ mapalloc_fd(uintptr_t fd, const size_t size) {
//munmap(mem, size);
//return (NULL);
}
mem_bzero(buf, size);
memset(buf, 0x00, size);

return (buf);
}
Expand Down
2 changes: 0 additions & 2 deletions lib.project
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
<File Name="src/utils/buf_str.c"/>
</VirtualDirectory>
<VirtualDirectory Name="proto">
<File Name="src/proto/http_server_auth.c"/>
<File Name="src/proto/http_client.c"/>
<File Name="src/proto/http.c"/>
<File Name="src/proto/bt_tracker.c"/>
<File Name="src/proto/dns_resolv.c"/>
Expand Down
17 changes: 8 additions & 9 deletions src/net/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@

#include <inttypes.h>
#include <unistd.h> /* close, write, sysconf */
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
#include <string.h> /* memcpy, memmove, memset, strerror... */
#include <stdio.h> /* snprintf, fprintf */
#include <errno.h>

#include "utils/macro.h"
#include "utils/mem_utils.h"
#include "utils/num2str.h"

#include "al/os.h"
Expand Down Expand Up @@ -188,7 +187,7 @@ skt_get_addr_family(uintptr_t skt, sa_family_t *family) {
if (NULL == family)
return (EINVAL);

mem_bzero(&ssaddr, sizeof(ssaddr));
memset(&ssaddr, 0x00, sizeof(ssaddr));
addrlen = sizeof(ssaddr);
if (0 != getsockname((int)skt, (sockaddr_p)&ssaddr, &addrlen))
return (errno);
Expand Down Expand Up @@ -260,7 +259,7 @@ skt_mc_join(uintptr_t skt, int join, uint32_t if_index,
return (EAFNOSUPPORT);

/* Join/leave to multicast group. */
mem_bzero(&mc_group, sizeof(mc_group));
memset(&mc_group, 0x00, sizeof(mc_group));
mc_group.gr_interface = if_index;
sa_copy(mc_addr, &mc_group.gr_group);
if (0 != setsockopt((int)skt,
Expand Down Expand Up @@ -288,7 +287,7 @@ skt_mc_join_ifname(uintptr_t skt, int join, const char *ifname,
}

#ifdef SIOCGIFINDEX
mem_bzero(&ifr, sizeof(ifr));
memset(&ifr, 0x00, sizeof(ifr));
memcpy(ifr.ifr_name, ifname, ifname_size);
ifr.ifr_name[ifname_size] = 0;
if (-1 == ioctl((int)skt, SIOCGIFINDEX, &ifr))
Expand Down Expand Up @@ -544,7 +543,7 @@ skt_recvfrom(uintptr_t skt, void *buf, size_t buf_size, int flags,
];

/* Initialize msghdr for receiving packets. */
//mem_bzero(&rcvcmsgbuf, sizeof(struct cmsghdr));
//memset(&rcvcmsgbuf, 0x00, sizeof(struct cmsghdr));
rcviov[0].iov_base = buf;
rcviov[0].iov_len = buf_size;
mhdr.msg_name = from; /* dst addr. */
Expand Down Expand Up @@ -721,7 +720,7 @@ skt_sync_resolv(const char *hname, uint16_t port, int ai_family,
if (NULL == hname)
return (EINVAL);

mem_bzero(&hints, sizeof(hints));
memset(&hints, 0x00, sizeof(hints));
hints.ai_family = ai_family;
hints.ai_flags = AI_NUMERICSERV;
u162str(port, servname, sizeof(servname), NULL); /* Should not fail. */
Expand Down Expand Up @@ -753,7 +752,7 @@ skt_sync_resolv_connect(const char *hname, uint16_t port,
if (NULL == hname || NULL == skt_ret)
return (EINVAL);

mem_bzero(&hints, sizeof(hints));
memset(&hints, 0x00, sizeof(hints));
hints.ai_family = domain;
hints.ai_flags = AI_NUMERICSERV;
hints.ai_socktype = type;
Expand Down Expand Up @@ -846,7 +845,7 @@ skt_tcp_stat_text(uintptr_t skt, const char *tabs,
return (EINVAL);

optlen = sizeof(info);
mem_bzero(&info, sizeof(info));
memset(&info, 0x00, sizeof(info));
if (0 != getsockopt((int)skt, IPPROTO_TCP, TCP_INFO, &info, &optlen))
return (errno);
if (10 < info.tcpi_state) {
Expand Down
8 changes: 4 additions & 4 deletions src/net/socket_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
#include <string.h> /* memcpy, memmove, memset, strerror... */

#include "utils/macro.h"
#include "utils/mem_utils.h"
Expand Down Expand Up @@ -69,15 +69,15 @@ sa_init(sockaddr_storage_p addr, const sa_family_t family,

switch (family) {
case AF_UNIX:
mem_bzero(addr, sizeof(sockaddr_un_t));
memset(addr, 0x00, sizeof(sockaddr_un_t));
#ifdef BSD /* BSD specific code. */
((sockaddr_un_p)addr)->sun_len = sizeof(sockaddr_un_t);
#endif
((sockaddr_un_p)addr)->sun_family = AF_UNIX;
//addr->sun_path[] = 0;
break;
case AF_INET:
mem_bzero(addr, sizeof(sockaddr_in_t));
memset(addr, 0x00, sizeof(sockaddr_in_t));
#ifdef BSD /* BSD specific code. */
((sockaddr_in_p)addr)->sin_len = sizeof(sockaddr_in_t);
#endif
Expand All @@ -86,7 +86,7 @@ sa_init(sockaddr_storage_p addr, const sa_family_t family,
//addr->sin_addr.s_addr = 0;
break;
case AF_INET6:
mem_bzero(addr, sizeof(sockaddr_in6_t));
memset(addr, 0x00, sizeof(sockaddr_in6_t));
#ifdef BSD /* BSD specific code. */
((sockaddr_in6_p)addr)->sin6_len = sizeof(sockaddr_in6_t);
#endif
Expand Down
10 changes: 4 additions & 6 deletions src/net/socket_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@

#include <inttypes.h>
#include <unistd.h> /* close, write, sysconf */
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
#include <string.h> /* memcpy, memmove, memset, strerror... */
#include <stdio.h> /* snprintf, fprintf */
#include <errno.h>

#include "utils/macro.h"
#include "utils/mem_utils.h"

#include "utils/sys.h"
#include "al/os.h"
#include "utils/sys.h"
#include "net/socket_address.h"
#include "net/socket.h"
#include "net/socket_options.h"
Expand Down Expand Up @@ -226,7 +224,7 @@ skt_opts_xml_load(const uint8_t *buf, const size_t buf_size,
(const uint8_t*)"AcceptFilterName", NULL)) {
if (0 != data_size &&
sizeof(opts->tcp_acc_filter.af_name) > data_size) {
mem_bzero(&opts->tcp_acc_filter,
memset(&opts->tcp_acc_filter, 0x00,
sizeof(struct accept_filter_arg));
memcpy(opts->tcp_acc_filter.af_name, data, data_size);
opts->mask |= SO_F_ACC_FILTER;
Expand Down Expand Up @@ -321,7 +319,7 @@ skt_opts_init(const uint32_t mask, const uint32_t bit_vals,

if (NULL == opts)
return;
mem_bzero(opts, sizeof(skt_opts_t));
memset(opts, 0x00, sizeof(skt_opts_t));
opts->mask = (SO_F_BIT_VALS_MASK & mask);
opts->bit_vals = bit_vals;
opts->backlog = INT_MAX;
Expand Down
Loading

0 comments on commit abf5e88

Please sign in to comment.