Search code examples
cmongodbvalgrindmongo-c-driver

valgrind - connectiong to mognodb with C api without causing SIGSEGV


I am trying to connect to mongodb with the C-API and my code seems to work fine. However, close inspection with Valgrind complains that I am doing illegal stuff.

My program accepts the parameter -h <hostname>, and then translates this string to a mongodb_uri and then tries to connect to mongodb:

*client_p = mongoc_client_new(host);

if (!*client_p) {
   log_die("Failed to parse URI!");
} 

My program runs as expected but when I inspect it with valgrind it the call to mongoc_client_new causes SIGSEGV:

==28775== Memcheck, a memory error detector
==28775== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==28775== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==28775== Command: ./coll2tde -h mongodb://localhost -d test -c testq
==28775== 
aTrying to connect to mongodb://localhost
2014/12/25 22:22:21.0255: [28775]:    DEBUG:      cluster: Client initialized in direct mode.
==28775== Jump to the invalid address stated on the next line
==28775==    at 0xFFFFFFFFFF600800: ???
==28775==    by 0x402722: get_cursor (mongo.c:26)
==28775==    by 0x402330: main (coll2tde.c:120)
==28775==  Address 0xffffffffff600800 is not stack'd, malloc'd or (recently) free'd
==28775== 
==28775== 
==28775== Process terminating with default action of signal 11 (SIGSEGV)
==28775==  Bad permissions for mapped region at address 0xFFFFFFFFFF600800
==28775==    at 0xFFFFFFFFFF600800: ???
==28775==    by 0x402722: get_cursor (mongo.c:26)
==28775==    by 0x402330: main (coll2tde.c:120)
==28775== 
==28775== HEAP SUMMARY:
==28775==     in use at exit: 114,751 bytes in 3,194 blocks
==28775==   total heap usage: 3,615 allocs, 421 frees, 177,386 bytes allocated
==28775== 
==28775== LEAK SUMMARY:
==28775==    definitely lost: 6,784 bytes in 1 blocks
==28775==    indirectly lost: 2,968 bytes in 11 blocks
==28775==      possibly lost: 1,462 bytes in 19 blocks
==28775==    still reachable: 103,537 bytes in 3,163 blocks
==28775==         suppressed: 0 bytes in 0 blocks
==28775== Rerun with --leak-check=full to see details of leaked memory
==28775== 
==28775== For counts of detected and suppressed errors, rerun with: -v
==28775== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 8 from 6)
Killed

At the beginning I thought it's a problem with my code, but then I compile the example from mogoc_client documentation and saw the same behavior. I would like to know if it is a bug or can this be safely ignored.


Solution

  • After spending a couple of hours trying to figuring this issue, it turns out this is a problem with older versions of Valgrind (3.7 on Debian Wheezy). I compiled Valgrind in version 3.10.1 from upstream sources and this issue disappeared.