gdb does not detect segmentation fault

I have kind of a weird problem. I want to implement the boyer-moore string searching algorithm which I did and already works fine on windows. However it does not on Ubuntu 10.10 and some other platform of which I don't know which unix version it runs on.

Whenever I try to run the app normally I get a 'segmentation fault', 'illegal instruction error' or sometimes it just runs normally but gives the wrong results. I suspect that has to do with what arguments I use (longer strings always give errors). The odd thing is that when I run the app in gdb, It never gives a segmentation fault or an illegal instruction error, but It does give the wrong result (always being 0 matches upto now) though - which it does not on windows. I suspect that I am somewhere writing in memory that contains instructions or something but I do not seem to be able to figure out where.
I tried compiling it with the -g option and than using gdb but it never detects an error then.
So... any help?

Edit: I'm looking into valgrind now and in valgrind the app workst perfecly. I only have one invalid read, which I am trying to fix now. However, both gdb and normal execution dont find any match...

Speed is appreciated since my deadline is coming up.

Thanks.
Last edited on
You have answered yourself - you have an invalid read.
If you have a single invalid read in the application, the behaviour is undefined.
Undefined = it may crash, it may give wrong results, it may run correctly or it may jump or dance.
It may also behave differently in different situations e.g. work correctly under gdb, and crash when run normally.
Last edited on
I changed a lot of my code and i think I made 'progress', altough i'm not sure.
Now I'm getting a SIGABRT. which seems to be on the rule:
'occ = (int*) malloc(256*sizeof(int));'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int charsread, doubles, cursor, results, tlength,i;
	char * t;
	int *occ, *s;


	FILE* file = fopen(fname, "rb");
        if (file == NULL) {
			printf("%s\n", "Unable to open file");
			return -1;
		}
	tlength = zlength * TSIZE;
	results = 0;
	cursor = 1;
	charsread =0;
		
        t = (char*) malloc(tlength * sizeof(char));
        if(t == NULL){
                printf("%s\n", "Memory error");
                return -1;
        }
	
        
        s = (int*) malloc(zlength+1*sizeof(int));
        //alles 0 initialiseren
	for(i =0;i<zlength+1; i++){
		s[i] = 0;
	}
        occ = (int*) malloc(256*sizeof(int));
        for(i = 0; i<256;i++){
		occ[i] =-1;
	}
        


gdb gives:


Breakpoint 1, processfile (fname=0x7fffffffe57c "search3/t.txt", z=0x603250 "needle", zlength=6) at search3/search3.c:117
117	        occ = (int*) malloc(256*sizeof(int));
(gdb) s
*__GI___libc_malloc (bytes=1024) at malloc.c:3615
3615	malloc.c: No such file or directory.
	in malloc.c
(gdb)

When I let it run normally in gdb:
Starting program: /home/kv/Dropbox/Ugent/Algoritmen en Datastructuren III/a.out search3/z.txt search3/t.txt
a.out: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)
((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.

Program received signal SIGABRT, Aborted.
0x00007ffff7a8cba5 in *__GI_raise (sig=<value optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
64	../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
	in ../nptl/sysdeps/unix/sysv/linux/raise.c


Now, I really have no Idea what the problem could be now... Valgrind does not detect any problems it seems (?):

==5672== Memcheck, a memory error detector
==5672== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==5672== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==5672== Command: ./a.out search3/z.txt /search3/t.txt
==5672== 
--5672-- Valgrind options:
--5672--    --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--5672--    --leak-check=full
--5672--    -v
--5672--    --tool=memcheck
--5672-- Contents of /proc/version:
--5672--   Linux version 2.6.35-22-generic (buildd@yellow) (gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5) ) #35-Ubuntu SMP Sat Oct 16 20:45:36 UTC 2010
--5672-- Arch and hwcaps: AMD64, amd64-sse3-cx16
--5672-- Page sizes: currently 4096, max supported 4096
--5672-- Valgrind library directory: /usr/lib/valgrind
--5672-- Reading syms from /home/kv/Dropbox/Ugent/Algoritmen en Datastructuren III/a.out (0x400000)
--5672-- Reading syms from /lib/ld-2.12.1.so (0x4000000)
--5672--   Considering /lib/ld-2.12.1.so ..
--5672--   .. CRC mismatch (computed 7a804908 wanted b62d6d8e)
--5672--   Considering /usr/lib/debug/lib/ld-2.12.1.so ..
--5672--   .. CRC is valid
--5672-- Reading syms from /usr/lib/valgrind/memcheck-amd64-linux (0x38000000)
--5672--    object doesn't have a dynamic symbol table
--5672-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
--5672-- Reading suppressions file: /usr/lib/valgrind/default.supp
--5672-- REDIR: 0x4018620 (strlen) redirected to 0x3803ee1f (vgPlain_amd64_linux_REDIR_FOR_strlen)
--5672-- Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so (0x4a23000)
--5672-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so (0x4c25000)
==5672== WARNING: new redirection conflicts with existing -- ignoring it
--5672--     new: 0x04018620 (strlen              ) R-> 0x04c292cf strlen
--5672-- REDIR: 0x4018490 (index) redirected to 0x4c28f71 (index)
--5672-- REDIR: 0x4018510 (strcmp) redirected to 0x4c2979b (strcmp)
--5672-- Reading syms from /lib/libc-2.12.1.so (0x4e2e000)
--5672--   Considering /lib/libc-2.12.1.so ..
--5672--   .. CRC mismatch (computed cc0d5ab1 wanted 70d47059)
--5672--   Considering /usr/lib/debug/lib/libc-2.12.1.so ..
--5672--   .. CRC is valid
--5672-- REDIR: 0x4eb2900 (__GI_strrchr) redirected to 0x4c28dde (__GI_strrchr)
--5672-- REDIR: 0x4eaa320 (malloc) redirected to 0x4c280da (malloc)
--5672-- REDIR: 0x4eb4a90 (memcpy) redirected to 0x4c2985d (memcpy)
--5672-- REDIR: 0x4eabc10 (free) redirected to 0x4c27cea (free)
--5672-- REDIR: 0x4eb0e40 (__GI_strlen) redirected to 0x4c2929b (__GI_strlen)
Unable to open file
--5672-- REDIR: 0x4eb6050 (strchrnul) redirected to 0x4c2a441 (strchrnul)
--5672-- REDIR: 0x4eb41b0 (mempcpy) redirected to 0x4c2a4b2 (mempcpy)
Total matches found: -1.
==5672== 
==5672== HEAP SUMMARY:
==5672==     in use at exit: 0 bytes in 0 blocks
==5672==   total heap usage: 3 allocs, 3 frees, 1,142 bytes allocated
==5672== 
==5672== All heap blocks were freed -- no leaks are possible
==5672== 
==5672== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
--5672-- 
--5672-- used_suppression:      2 dl-hack3-cond-1
--5672-- used_suppression:      2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a
==5672== 
==5672== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

Google didn't give any interesting results so far... does anyone have an idea what this could be?
Last edited on
Nevermind. it was a wrong malloc caused by the previous malloc statement:
s = (int*) malloc(zlength+1*sizeof(int));
which ofcourse should be:
s = (int*) malloc((zlength+1)*sizeof(int));
Topic archived. No new replies allowed.