0

at first excuse me for not providing any code, but it's hard to just C+P an excerpt, since the errors are caused somehow randomly.

I am encountering a very strange error when compiling my C source with GCC. I am developing a linked-in driver for Erlang, and I do not understand what is causing the error. The error goes like this:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xffffffffb012aae8
[Switching to process 7316 thread 0x1503]
ktqk_exec (query=0x13e0af00, table=0x13e00ea0) at ktqk.c:215
215   clock_t start = clock();

I am running the Erlang virtual machine wrapped with GDB, so I can access the memory sections. To me, the high address 0xffffffffb012aae8 looks very suspicious. However, with Clang everything works as expected, no errors, no segfaults. I tried to investigate:

(gdb) p clock
$1 = {<text variable, no debug info>} 0x7fff85c29fd0 <clock>
(gdb) p start
$2 = 2954013712

So the value was obviously not initialized, it crashed before. When I set breakpoints in the same file, they are simply skipped. Why does everything work with Clang, but not with GCC?

Since Clang uses C99 and GCC C89 by default, I had to included the -std=c99 flag for compilation on GCC. May this be a potential source? However, when I comment out the code above, it fails at the next function call. So it seems somehow related to function calls. Nevertheless, all function calls before this line are fine.

A very strange error. Does anybody have any ideas? Sorry for this rather fuzzy explanation, I am simply not understanding the error.

All the best, Martin

squidfunk
  • 391
  • 3
  • 17
  • I'm voting to close this, since it's too localized. The error turned out to be something completely unrelated to the text in the question itself, just a negative array index. – unwind Nov 13 '12 at 10:53
  • Yeah I am okay with this to be closed, sorry for bothering! – squidfunk Nov 13 '12 at 10:58

1 Answers1

1

I can answer my own question: the code that was causing the error can be found below:

...
int select = -1;

for (int p = 0; p < SIZE_KEYS; p++)
  if (parts[p] == query->count && (select == -1 || sizes[p] < sizes[select]))
    select = p;

int *index[lists[select]];
if (select != -1) {
  ...
}
...

So select was initialized to -1 and, if something would have been found, it would have been > 0. Now, in my example nothing was found, so select = -1. Putting the -1 in lists, the result was also lists[select] = -1, so obviously the same memory region as select. However, now we're initializing a list of integer pointers of size -1. And that is clearly wrong.

Why is Clang not complaining about this severe error!?

squidfunk
  • 391
  • 3
  • 17
  • As far as I can tell, you're not initializing an array to a negative size, you're accessing a negative index element, which is perfectly well-defined if *(p-1), where p is a pointer to an array element, is well defined. See for example http://stackoverflow.com/questions/3473675/negative-array-indexes-in-c – rubenvb Nov 13 '12 at 10:52
  • I don't think `int *index[-1]` is a valid initialization of a stack variable. Or am I wrong? – squidfunk Nov 13 '12 at 10:58
  • Either I misread the first time or I saw an earlier version. I fear the former. Sorry. – rubenvb Nov 13 '12 at 17:38