Always memset C structures and check memory with Valgrind

send pid to kernel failed No buffer space available
valgrind --track-origins=yes -s /home/keyrnk/Projects/linux_system_dev/kernel_module_netlink_client_server_example/kernel_client/build/kernel_module_client
==80279== 1 errors in context 1 of 2:==80279== Syscall param sendmsg(msg) points to uninitialised byte(s)==80279==    at 0x4CD5B87: sendmsg (in /usr/lib64/libc-2.30.so)==80279==    by 0x4014D3: KernelModuleClient::Run(std::atomic<bool>&) (in /home/keyrnk/Projects/linux_system_dev/kernel_module_netlink_client_server_example/kernel_client/build/kernel_module_client)==80279==    by 0x4012E7: main (in /home/keyrnk/Projects/linux_system_dev/kernel_module_netlink_client_server_example/kernel_client/build/kernel_module_client)==80279==  Address 0x1ffefffc60 is on thread 1's stack==80279==  in frame #1, created by KernelModuleClient::Run(std::atomic<bool>&) (???:)==80279==  Uninitialised value was created by a stack allocation==80279==    at 0x40136C: KernelModuleClient::Run(std::atomic<bool>&) (in /home/keyrnk/Projects/linux_system_dev/kernel_module_netlink_client_server_example/kernel_client/build/kernel_module_client)
struct msghdr msg;
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
struct msghdr {
void *msg_name; /* optional address */
socklen_t msg_namelen; /* size of address */
struct iovec *msg_iov; /* scatter/gather array */
size_t msg_iovlen; /* # elements in msg_iov */
void *msg_control; /* ancillary data, see below */
size_t msg_controllen; /* ancillary data buffer len */
int msg_flags; /* flags on received message */
};
struct msghdr msg;
memset(&msg, 0, sizeof(msg));

msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
==80502== HEAP SUMMARY:==80502==     in use at exit: 0 bytes in 0 blocks==80502==   total heap usage: 1 allocs, 1 frees, 72,704 bytes allocated==80502== All heap blocks were freed -- no leaks are possible==80502== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store