I'll do my best to explain the problem since I can't share a lot of the code. So I have this C program on one hand and a java program on the other. The C program is a command line program that has a function that that can be called by the java program (which is a gui application) to provide a visual result. Here is the chain of methods that are called from the main or java gui:
c code - main / JNI method
|-> first method
|-> second method
|-> third method
|-> fourth method
|-> more methods (not the source of problem though)
The c program by itself compiles and runs perfectly, performing the function that it was intended for. The first method called returns a char*, which we wanted to turn into a jstring. Through the whole process of debugging and figuring our where the segmentation fault occurs, we have at least verified that the jstring conversion works as intended. The seg fault only happens when I call the JNI function through java.
So here is the block of code that causes the seg fault (generic code but as close and accurate as possible), which is located in the fourth method just 9 lines in and was found by putting a return statement just after it:
for(i = 0; i < LENGTH; i++){
printf("[%2x]", <unsigned char array>[i]);
}
The weird (and possibly related part) is that if I put the return statement before the for-loop and call the JNI function we get a different error. An undefined symbol error for EVP_CIPHER_CTX, specifically it says that it is undefined in our .so file.
I did find this question but it doesn't help because at the point of the error no variables were ever set to null:
What can cause a Java native function (in C) to segfault upon entry?
EDIT: APPENDING MORE INFO
So I used Valgrind (fantastic tool BTW, can't believe I was never told about this), but it only pointed out supposed write errors (they all had to do with sprintf), but they were fine, as well as conditions that were using uninitialized variables, but they were also fine because there was always an if statement that checked to make sure they were not null before using them.
GDB (also a fantastic tool, wish I was taught how to use this when I learned C in school) showed that the error was not happening where I believed it was happening. This is odd since the only time the error came up was when I place the return statement after the above code. The true location of the error was the code two line before the for-loop:
memcpy(unsgned_char_array, value, value_size);
I put printf statements before this line to check that key and key_size did in fact of values, and they did. As for the unsigned char array, space was allocated to it and it was initialized with this line of code:
memset(unsgned_char_array, 0, size_of_array);
So I'm still not sure what is going on.
This is the most detail I can remember at the moment off the top of my head, since this is from work. I will provide more detail where I am able to and when asked for more info, but it will probably have to wait until after work the next day.