Skip to content
Snippets Groups Projects
Commit 8dcdb984 authored by Mick Jordan's avatar Mick Jordan
Browse files

fix mkdtemp bug

parent d37937a3
Branches
No related tags found
No related merge requests found
......@@ -58,11 +58,15 @@ Java_com_oracle_truffle_r_runtime_ffi_jni_JNI_1Base_native_1setwd(JNIEnv *env, j
JNIEXPORT jint JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jni_JNI_1Base_native_1mkdtemp(JNIEnv *env, jclass c, jbyteArray jtemplate) {
char *template = (*env)->GetPrimitiveArrayCritical(env, jtemplate, NULL);
char *template = (char*) (*env)->GetByteArrayElements(env, jtemplate, NULL);
char *r = mkdtemp(template);
if (r == NULL) return 0;
(*env)->ReleasePrimitiveArrayCritical(env, jtemplate, template, 0);
return 1;
int rc = 1;
if (r == NULL) {
// printf("mkdtemp errno: %d\n", errno);
rc = 0;
}
(*env)->ReleaseByteArrayElements(env, jtemplate, (jbyte*) template, rc == 1 ? 0 : JNI_ABORT);
return rc;
}
JNIEXPORT jint JNICALL
......
......@@ -72,12 +72,19 @@ public class JNI_Base implements BaseRFFI {
@Override
public String mkdtemp(String template) {
/*
* Not only must the (C) string end in XXXXXX it must also be null-terminated. Since it is
* modified by mkdtemp we must make a copy.
*/
byte[] bytes = template.getBytes();
long result = native_mkdtemp(bytes);
byte[] ztbytes = new byte[bytes.length + 1];
System.arraycopy(bytes, 0, ztbytes, 0, bytes.length);
ztbytes[bytes.length] = 0;
long result = native_mkdtemp(ztbytes);
if (result == 0) {
return null;
} else {
return new String(bytes);
return new String(ztbytes, 0, bytes.length);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment