fu-tyan / java-simple-serial-connector

Automatically exported from code.google.com/p/java-simple-serial-connector
0 stars 0 forks source link

JVM Crash (Windows XP) When trying to open serial port #32

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Call serialPortInstance.openPort()

What is the expected output? What do you see instead?
It should work, but instead the JVM crashes.  The real output is attached - 
it's the standard EXCEPTION_ACCESS_VIOLATION.

What version of the product are you using? On what operating system?
Using jssc-0.9 on Windows XP

It's crashing in the open function's JNI code.  I'm not sure how to compile the 
JNI code for windows so I can't really troubleshoot or test, but there is some 
really suspicious stuff in the function, particularly the following:
char prefix[] = "\\\\.\\";
const char* port = env->GetStringUTFChars(portName, JNI_FALSE);
strcat(prefix, port);

strcat should take 'port' and append it to the char* at &prefix[0].  The issue 
is that prefix is a static array on the stack of a certain length.  I expect 
that the access violation happens when strcat tries to copy characters from 
port past the end of the prefix array.

Again, I haven't compiled/tested a fix for this, but I expect that it would be 
to either declare prefix to be a longer static array, say prefix[64] = 
"\\\\.\\", or to declare it on the heap, as preffix = malloc(4+strlen(port)) or 
something.

Let me know if any other info is required.  Not sure how no-one else ran into 
this issue...  Maybe I'm Doing It Wrong?

Original issue reported on code.google.com by charles....@gmail.com on 20 Feb 2013 at 7:10

Attachments:

GoogleCodeExporter commented 9 years ago
OK, a few updates here.

The issue I pointed out above is a real issue, but is not the cause of the 
behaviour I was seeing.

The root cause of the dump was that the String comport that I was passing in 
was actually null, but I was assuming it was legitimate.  In that case I was 
running into issue #22.

However, the strcat issue above is still relevant. I changed the library code 
to look like this to confirm the issue:

<jssc.cpp>
char other[] = "testingTESTINGtestingTESTING";
char prefix[] = "\\\\.\\";

printf("Before:");
printf(other);
printf("\n\r");
fflush(stdout);

const char* port = (const char*)env->GetStringUTFChars(portName, JNI_FALSE);
strcat(prefix, port);

printf("After:");
printf(other);
printf("\n\r");
fflush(stdout);
<end of changes to jssc.cpp>

And the output I get is 
Before:testingTESTINGtestingTESTING
After:OM1

This is showing that the strcat is overwriting random stuff on the stack.

Original comment by charles....@gmail.com on 20 Feb 2013 at 11:37

GoogleCodeExporter commented 9 years ago
String concatenation fixed. Version 2.1.0

The correct code should looks like that:

char prefix[] = "\\\\.\\";
const char* port = env->GetStringUTFChars(portName, JNI_FALSE);

char portFullName[strlen(prefix) + strlen(port) + 1];
strcpy(portFullName, prefix);
strcat(portFullName, port);

Original comment by scream3r.org@gmail.com on 12 Mar 2013 at 7:45