This isn't really related, but compiling this code with GCC 4.0.1 on
Darwin/x86 introduces a potentially serious problem. In the
`localBind' JNI function, I am doing this:
char *addr_path;
...
addr_path = (char *) (*env)->GetStringUTFChars (env, (jstring)
path, &copied);
if (local_bind (fd, addr_path))
{
_throw (env, "java/io/IOException", local_error ());
}
(*env)->ReleaseStringUTFChars (env, (jstring) path, addr_path);
When it gets to the `ReleaseStringUTFChars' call, `addr_path' has
changed! And, since `ReleaseStringUTFChars' calls free(3) on jamvm,
this means that I'm freeing a pointer that was never malloc'd!
It looks like the least significant byte of the pointer is being set
to zero. If I add a `printf' of the path to `local_bind,' the problem
disappears.
So, my question is, is this a known problem with GCC? If so, is there
any way to work around it?
`local_bind' is an extremely simple function:
int
local_bind (int fd, char *addr)
{
struct sockaddr_un saddr;
strncpy (saddr.sun_path, (char *) addr, sizeof (saddr.sun_path));
saddr.sun_path[sizeof (saddr.sun_path)] = '\0';
saddr.sun_family = AF_LOCAL;
return bind (fd, (struct sockaddr *) &saddr, SUN_LEN (&saddr));
}
Does anyone know how to prevent GCC from breaking my program like
this? Am I doing something wrong without realizing it?