Thursday, September 11, 2008

Pointers = Fail

Yesterday, Squawk stopped working on our cRIO. Today, we fixed it. The problem? This block:
char** fn;
symFindByName(sysSymTbl, (char*)symbol, fn, &ptype);
Notice a problem? No, it's not that sysSymTbl, symbol, or ptype are undefined (they are, i just left them out). See the char** fn? Yeah, a pointer to a pointer to a char (it's being used as a pointer to a void*, but the symFindByName prototype is stupid). What's the problem with that? I'm allocating fn on the stack. Nowhere do I allocate the memory that fn will point to. What I meant to do was this:
char* fn;
symFindByName(sysSymTbl, (char*)symbol, &fn, &ptype);
What did that fix? Now I'm allocating fn on the stack, and giving the address of the stack memory to symFindByName. So when symFindByName writes the address of the requested symbol to *fn, it doesn't write to random memory.

The worst part of all this is that it was working before. I don't know why, and I'm not sure I want to know. I spent the better part of two days looking for this bug. I hate pointers.

No comments:

Post a Comment