[ENet-discuss] [PATCH] init ENetHost struct memory in enet_host_create

Lucas Beyer pompei2 at gmail.com
Thu Dec 8 03:48:02 PST 2011


Dear list,

I have come to the habit of running my small programs through valgrind. One
error being reported comes from enet.

This is the valgrind output:

==18627== Syscall param socketcall.sendmsg(msg.msg_iov[i]) points to
uninitialised byte(s)
==18627==    at 0x56816B0: __sendmsg_nocancel (in /lib64/libc-2.14.1.so)
==18627==    by 0x40D5C5: enet_socket_send (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==    by 0x40CBFF: enet_protocol_send_outgoing_commands (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==    by 0x40CEB6: enet_host_service (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==    by 0x4045E4: main (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==  Address 0x593614c is 156 bytes inside a block of size 10,976
alloc'd
==18627==    at 0x4C297CD: malloc (in
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==18627==    by 0x40D8B7: enet_malloc (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==    by 0x405C2D: enet_host_create (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==    by 0x405190: enet::Client::Client(std::string const&, unsigned
short, double, unsigned long, unsigned int, unsigned int) (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==    by 0x40455E: main (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==  Uninitialised value was created by a heap allocation
==18627==    at 0x4C297CD: malloc (in
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==18627==    by 0x40D8B7: enet_malloc (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==    by 0x405C2D: enet_host_create (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==    by 0x405190: enet::Client::Client(std::string const&, unsigned
short, double, unsigned long, unsigned int, unsigned int) (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)
==18627==    by 0x40455E: main (in
/windows/Xchange/fts/scm/git/fts/gamedbg/build-d/fts-gamedbg)

I have looked into it for a little time and am now pretty confident it's
not because of an usage error I made. My code is pretty simple and based on
the tutorial. Applying the following patch fixes it for me:

--- host.c.orig    2011-12-08 12:25:44.187125000 +0100
+++ host.c    2011-12-08 12:23:43.661363000 +0100
@@ -38,6 +38,7 @@
     host = (ENetHost *) enet_malloc (sizeof (ENetHost));
     if (host == NULL)
       return NULL;
+    memset (host, 0, sizeof (ENetHost));

     host -> peers = (ENetPeer *) enet_malloc (peerCount * sizeof
(ENetPeer));
     if (host -> peers == NULL)

I also attached the patch as a file to this email.

-- 
Kind regards, Lucas
http://arkana-fts.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20111208/ffd4340f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: uninitmem.patch
Type: text/x-patch
Size: 372 bytes
Desc: not available
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20111208/ffd4340f/attachment.bin>


More information about the ENet-discuss mailing list