android - appending values each time when calling JNI method -


i using jni fetch signature of apk , getting well. when calling method java first time getting exact value. calling again getting appended values exact value (eg 1234456123456). pfb code using

char* getsignaturemd5(jnienv* env, jobject obj) {     char* sign = loadsignature(env, obj);     md5_ctx context = { 0 };     md5init(&context);     md5update(&context, (unsigned char*)sign, strlen(sign));     unsigned char dest[16] = { 0 };     md5final(dest, &context);     int i;     static char destination[32]={0};     (i = 0; < 16; i++) {             sprintf(destination, "%s%02x", destination, dest[i]);     }     return destination; } 

gettoken jni method

jniexport jstring jnicall java_com_sign_signaturecapturesbi_myadapter_gettoken(jnienv *env, jobject obj)                                                                      {     char* signvalue = getsignaturemd5(env, obj);     __android_log_print(android_log_verbose, "myapp", "signvalue %s", signvalue);     return (*env)->newstringutf(env, signvalue);     } 

these lines cause undefined behavior:

for (i = 0; < 16; i++) {         sprintf(destination, "%s%02x", destination, dest[i]); } 

man 3 printf:

c99 , posix.1-2001 specify results undefined if call sprintf(), snprintf(), vsprintf(), or vsnprintf() cause copying take place between objects overlap (e.g., if target string array , 1 of supplied input arguments refer same buffer).

moreover destination static , because of keeps content between calls. these points give such weird behavior.

since dest size known, can unroll loop, don't forget add 1 cell destination terminating \0. and, if possible, should use snprintf() instead:

static char destination[33];  snprintf(destination, sizeof destination,     "%02x%02x%02x%02x%02x%02x%02x%02x"     "%02x%02x%02x%02x%02x%02x%02x%02x",     dest[0], dest[1], dest[2], dest[3],     dest[4], dest[5], dest[6], dest[7],     dest[8], dest[9], dest[10], dest[11],     dest[12], dest[13], dest[14], dest[15]); 

in case can leave destination static one, since code doesn't relay on content anymore. note getsignaturemd5() returns pointer same buffer each time call it, result subsequent calls erase result obtained previous calls.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -