From owner-freebsd-java@FreeBSD.ORG Thu Jun 4 01:13:39 2009 Return-Path: Delivered-To: freebsd-java@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 25C5D106564A for ; Thu, 4 Jun 2009 01:13:39 +0000 (UTC) (envelope-from davidn@datalinktech.com.au) Received: from outbound.icp-qv1-irony-out2.iinet.net.au (outbound.icp-qv1-irony-out2.iinet.net.au [203.59.1.107]) by mx1.freebsd.org (Postfix) with ESMTP id B0D2E8FC0C for ; Thu, 4 Jun 2009 01:13:37 +0000 (UTC) (envelope-from davidn@datalinktech.com.au) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao4FAH21JkrLzqKt/2dsb2JhbACBT84lhAwF X-IronPort-AV: E=Sophos;i="4.41,301,1241366400"; d="scan'208";a="502348649" Received: from unknown (HELO mail.datalinktech.com.au) ([203.206.162.173]) by outbound.icp-qv1-irony-out2.iinet.net.au with ESMTP; 04 Jun 2009 08:44:19 +0800 Received: from activate-sjc0.adobe.com (203-206-162-169.perm.iinet.net.au [203.206.162.169]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.datalinktech.com.au (Postfix) with ESMTPSA id 81A074CE9F0; Thu, 4 Jun 2009 10:44:18 +1000 (EST) Date: Thu, 04 Jun 2009 10:44:18 +1000 To: "Deshmukh, Pramod" , freebsd-java@freebsd.org From: "David Nugent" Organization: Datalink Technologies Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 References: <5CF2BB5045ED7D44B69F3E63642D693207E63FCF@ICE.scur.com> Content-Transfer-Encoding: 7bit Message-ID: In-Reply-To: <5CF2BB5045ED7D44B69F3E63642D693207E63FCF@ICE.scur.com> User-Agent: Opera Mail/9.64 (MacIntel) Cc: Subject: Re: Error while using strcat() in JNI X-BeenThere: freebsd-java@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Porting Java to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Jun 2009 01:13:39 -0000 On Thu, 04 Jun 2009 07:38:43 +1000, Deshmukh, Pramod wrote: > Below is the c program which is called by java. Java passed string > object to this method which is converted to char *. When I print using > printf() > function it prints the string (char *), but strcat() does not like char > *. Your diagnosis is incorrect. This issue has nothing to do with JNI as such, the crash is caused by a bug in your code. > Attached is the .h file the same. Please help me. Also I have attached > the .log file [..] > JNIEXPORT jstring JNICALL Java_nativetest_sayHello(JNIEnv *env, jobject > thisobject, jstring p_data, jstring p_salt) > { > printf("Inside C program \n"); > printf("=================================\n"); > char *cmdOpenSsl; > jboolean iscopy; > char* cDesc = strdup((*env)->GetStringUTFChars(env, p_data, > &iscopy)); > printf("cDesc == %s ", cDesc); > cmdOpenSsl = "echo -n "; > strcat(cmdOpenSsl, cDesc); You're assigning a non-writable address to the variable cmdOpenSsl and then trying to concatenate something to it. Make sure you a) can write to where you are strcat()ing to, and b) there is sufficient room there. Try something like: static const char *echo = "echo -n "; .. cmdOpenSsl = malloc(strlen(echo) + strlen(cDesc) + 1); strcpy(cmdOpenSsl, echo); strcat(cmdOpenSsl, cDesc); Personally I've found it wise to avoid use of malloc (and indirect use via strdup) in JNI code however. It can be done of course, but you have to be careful to not introduce memory leaks. I would prefer alloca() or use C++ locals. Regards, David