Hello all,
I am curious if I am doing this correctly as per valgrind checking for leaks and other errors.
My program basically, allocates a set amount of space for X objects and then performs a search upon them. However, valgrind posts the following (after the code errors).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#define NO_ERROR 0
#define DEFAULT_ERROR -1
typedef struct mystruct_s {
int fd;
} mystruct_t;
int current_elements = 0;
int max_elements = 0;
int init(int size, mystruct_t ** conn_list)
{
// Quick param check
if (size <= 0) {
return DEFAULT_ERROR;
}
current_elements = 0;
*conn_list = (mystruct_t*)malloc(sizeof(mystruct_t) * size);
// Check if malloc failed
if (*conn_list == NULL) {
return -1;
}
memset(*conn_list, 0, sizeof(mystruct_t) * size);
return 0;
}
void insert(int fd, mystruct_t * conn_list)
{
mystruct_t *tmp = NULL;
size_t size = 0;
if (current_elements == (max_elements)) {
size= sizeof(mystruct_t) * current_elements;
tmp = conn_list + size;
tmp->fd = fd;
current_elements = 0;
} else {
size = sizeof(mystruct_t) * current_elements;
tmp = conn_list + size;
tmp->fd = fd;
current_elements++;
}
}
mystruct_t *search(int fd, mystruct_t * conn_list)
{
int i = 0;
mystruct_t *tmp = conn_list;
for (i = 0; i < max_elements; i++) {
printf("%d\n",tmp->fd );
if (tmp->fd == fd) {
//*ret = conn_list + (sizeof(mystruct_t) * i);
return (tmp);
}
tmp += sizeof(mystruct_t);
}
return (NULL);
}
#define MAX_DEVS 8
int main(int argc, char **argv)
{
int size = MAX_DEVS;
mystruct_t conn_array[MAX_DEVS] = { 0 };
mystruct_t *conn_list = NULL;
max_elements = MAX_DEVS;
if (init(size, &conn_list) < 0) {
return (-1);
}
mystruct_t *ptr = NULL;
int i = 0;
for (i = 0; i < max_elements; i++) {
conn_array[i].fd = i;
insert(conn_array[i].fd, conn_list);
}
if ((ptr = search(conn_array[5].fd, conn_list)) != NULL) {
printf("found: %d\n", ptr->fd);
} else {
printf("NOT found: %d\n", conn_array[5].fd);
}
free(conn_list);
}
Compile, and valgrind command:
Code:
gcc -Wall -O2 -g -o test test2.c ;./test;valgrind --tool=memcheck --leak-check=yes ./test
Note that the -02 or -O0 do nothing regardless....
Code:
Code:
0
1
2
3
4
5
found: 5
==5448== Memcheck, a memory error detector
==5448== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==5448== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==5448== Command: ./test
==5448==
==5448== Invalid write of size 4
==5448== at 0x400504: insert (test2.c:58)
==5448== by 0x400504: main (test2.c:100)
==5448== Address 0x5202060 is 0 bytes after a block of size 32 alloc'd
==5448== at 0x4C2DB95: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5448== by 0x4006B2: init (test2.c:32)
==5448== by 0x4004DA: main (test2.c:90)
==5448==
0
1
==5448== Invalid read of size 4
==5448== at 0x40074F: search (test2.c:69)
==5448== by 0x40054A: main (test2.c:103)
==5448== Address 0x5202060 is 0 bytes after a block of size 32 alloc'd
==5448== at 0x4C2DB95: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5448== by 0x4006B2: init (test2.c:32)
==5448== by 0x4004DA: main (test2.c:90)
==5448==
2
==5448== Invalid read of size 4
==5448== at 0x40075D: search (test2.c:70)
==5448== by 0x40054A: main (test2.c:103)
==5448== Address 0x5202060 is 0 bytes after a block of size 32 alloc'd
==5448== at 0x4C2DB95: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5448== by 0x4006B2: init (test2.c:32)
==5448== by 0x4004DA: main (test2.c:90)
==5448==
3
4
5
valgrind: m_mallocfree.c:303 (get_bszB_as_is): Assertion 'bszB_lo == bszB_hi' failed.
valgrind: Heap block lo/hi size mismatch: lo = 4, hi = 17179869184.
This is probably caused by your program erroneously writing past the
end of a heap block and corrupting heap metadata. If you fix any
invalid writes reported by Memcheck, this assertion failure will
probably go away. Please try that before reporting this as a bug.
host stacktrace:
==5448== at 0x38083F98: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==5448== by 0x380840B4: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==5448== by 0x38084241: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==5448== by 0x38091AEC: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==5448== by 0x3807D653: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==5448== by 0x3807BEE3: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==5448== by 0x380800BA: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==5448== by 0x3807B47A: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==5448== by 0x380593E1: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==5448== by 0x802D40CC7: ???
==5448== by 0x802CA9F2F: ???
==5448== by 0x80200830F: ???
==5448== by 0x40054A: main (test2.c:103)
sched status:
running_tid=1
Thread 1: status = VgTs_Runnable (lwpid 5448)
==5448== at 0x400550: main (test2.c:104)
Note: see also the FAQ in the source distribution.
It contains workarounds to several common problems.
In particular, if Valgrind aborted or crashed after
identifying problems in your program, there's a good chance
that fixing those problems will prevent Valgrind aborting or
crashing, especially if it happened in m_mallocfree.c.
If that doesn't help, please report this bug to: www.valgrind.org
In the bug report, send all the above text, the valgrind
version, and what OS and version you are using. Thanks.
I'm also on x64 15.10 ubuntu with GCC 5.3
Code:
gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/bin/gcc-5.3/libexec/gcc/x86_64-unknown-linux-gnu/5.3.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ./configure --disable-checking --enable-languages=c,c++ --disable-multilib --with-system-zlib prefix=/usr/bin/gcc-5.3
Thread model: posix
gcc version 5.3.0 (GCC)