2

I have a a single error when I run my program through valgrind. The problem is that it wont tell me where the uninitialised bytes were allocated:

==22141== Syscall param write(buf) points to uninitialised byte(s)
==22141==    at 0x5B68900: __write_nocancel (syscall-template.S:82)
==22141==    by 0x5AFB882: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1289)
==22141==    by 0x5AFB749: new_do_write (fileops.c:543)
==22141==    by 0x5AFCEB4: _IO_do_write@@GLIBC_2.2.5 (fileops.c:516)
==22141==    by 0x5AFDD3E: _IO_switch_to_get_mode (genops.c:189)
==22141==    by 0x5AFBA96: _IO_file_seekoff@@GLIBC_2.2.5 (fileops.c:999)
==22141==    by 0x5AF4F25: rewind (rewind.c:37)
==22141==    by 0x567D149: CBFileAppend (CBFileEC.c:69)
==22141==    by 0x5473AFA: CBDatabaseCreateDeletionIndex (CBDatabase.c:270)
==22141==    by 0x5473195: CBInitDatabase (CBDatabase.c:112)
==22141==    by 0x54721A1: CBNewAddressStorage (CBAddressStorage.c:37)
==22141==    by 0x401F67: main (testCBAddressManager.c:226)
==22141==  Address 0x402a009 is not stack'd, malloc'd or (recently) free'd
==22141==  Uninitialised value was created by a stack allocation
==22141==    at 0x546F750: ??? (in /media/sf_BitEagle_Projects/cbitcoin/bin/libcbitcoin-storage.2.0.so)

Would I be correct in assuming this means some sort of invalid pointer to the stack? This happens in rewind() and I do not understand why rewind would be like this. I tried attaching the valgrind process to gdb and I tried to print the result of the ftell() on the file pointer. This caused valgrind to exit with this:

valgrind: m_syswrap/syswrap-main.c:1296 (vgPlain_client_syscall): Assertion 'sci->status.what == SsIdle' failed.
==22938==    at 0x3804CA36: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==22938==    by 0x3804CBDC: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==22938==    by 0x38091F55: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==22938==    by 0x3808E5DF: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==22938==    by 0x3808F739: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==22938==    by 0x3809F7D5: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)

sched status:
  running_tid=1

Thread 1: status = VgTs_Runnable
==22938==    at 0x5B68900: __write_nocancel (syscall-template.S:82)
==22938==    by 0x5AFB882: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1289)
==22938==    by 0x5AFB749: new_do_write (fileops.c:543)
==22938==    by 0x5AFCEB4: _IO_do_write@@GLIBC_2.2.5 (fileops.c:516)
==22938==    by 0x5AFDD3E: _IO_switch_to_get_mode (genops.c:189)
==22938==    by 0x5AFBA96: _IO_file_seekoff@@GLIBC_2.2.5 (fileops.c:999)
==22938==    by 0x5AF1AA5: ftell (ioftell.c:41)
==22938==    by 0x40133F: ??? (in /media/sf_BitEagle_Projects/cbitcoin/bin/testCBAddressManager)
==22938==    by 0xF75E467: ???
==22938==    by 0x7FEFFF3BF: ???
==22938==    by 0xF75E467: ???
==22938==    by 0x546DE87: ??? (in /media/sf_BitEagle_Projects/cbitcoin/bin/libcbitcoin-storage.2.0.so)
==22938==    by 0x7FEFFF3DF: ???

How would I go about determining the cause of this error?

Edit: I fixed the other issue I was having but this persists.

This is from the code here: https://github.com/MatthewLM/cbitcoin/blob/master/test/testCBAddressManager.c

The file IO code is here: https://github.com/MatthewLM/cbitcoin/tree/master/dependencies/storage

Thank you.

Matthew Mitchell
  • 5,293
  • 14
  • 70
  • 122

1 Answers1

2

Syscall param write(buf) points to uninitialised byte(s)

There isn't necessarily anything wrong with that. Consider:

int main() {
  struct Foo { int a; int b; int c; } x;
  x.a = 1; x.c = 3;
  write(1, &x, sizeof(x));  // part of x is not initialized
  return 0;
}

If later you read the data back in, and only use .a and .c members, then your program is well defined.

Would I be correct in assuming this means some sort of invalid pointer to the stack?

No.

If you really wanted to find out which part of data is uninitialized, use Valgrind'd built-in gdbserver, and issue monitor check_memory defined command.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • OK thank you. I understand what this means now. It was just the `???` that thew me out. – Matthew Mitchell Feb 04 '13 at 13:23
  • 1
    This advice is terrible and error prone. Always initialize data structures. – alecco Aug 30 '13 at 16:41
  • 1
    @alecco I *didn't* advise to use uninitialized data; I only explained what the Vaglrind error means, and that's it doesn't necessarily indicate a bug in the program. "Always initialize data structures." -- you have a very simplistic view of the world. It is indeed prudent to initialize all data, except in cases where doing so hurts performance too much, and one knows what one is doing. – Employed Russian Aug 30 '13 at 20:16