2012-04-06 7 views
0

私は2つのDLLがあると仮定します。バッファオーバーフローを避けるためにstrlcpy関数を使用し、gtk-glib-2.0 dllを使ってバッファオーバーフロー "g_snprintf"を回避するために関数を再度エクスポートしました。私はこのコードをdllにし、両方の関数をPython言語からeasely呼び出しできるように、バッファの文字列値を返すようにしたいときに問題が発生しています。ここでの抜粋です:cでこのバッファ値を返す方法は?

#include <stdio.h> 
#include "dlfcn.h" 
#include <sys/types.h> 
#include <string.h> 

#define lib  "strlcpy.dll" 
#define func "strlcpy" 
#define lib2 "libglib-2.0-0.dll" 
#define func2 "g_snprintf" 

char* returnMsg(char *buff, unsigned long n, char *msg) 
{ 
int (*g_snprintf)(char *string, unsigned long n, char const *format,char *msg); 
void *handle2; 
int errorno; 

handle2 = dlopen(lib2, RTLD_LAZY); 
if (!handle2){ 
    printf("\nerror opening second dll\n"); 
    return 1; 

printf("got it at second dll: %p",handle2); 

g_snprintf = dlsym(handle2, func2); 
if (!g_snprintf) 
    printf("error getting g_snprintf symbol.."); 
    return 1; 
if ((errorno= g_snprintf(buff, n, "%s",msg)) !=0) 
    printf("error cannot use g_snprintf.."); 
    return 1; 
dlclose(handle2); 
return buff; 
} 

int main() 
{ 
char iv[32]; 
char *msg,*l; 
int k; 
unsigned int g; 
char buff[16]; 

size_t (*strlcpy)(char *dst, const char *src, size_t siz); 
void *handle; 

handle = dlopen (lib, RTLD_LAZY); 
if (!handle) { 
    printf("error cannot open library.."); 
    return 1; 
} 
printf("opening dll at %p\n", handle); 
strlcpy = dlsym(handle, func); 
if (!strlcpy) { 
    printf("error cannot find desired exported function.."); 
    return 1; 
} 
printf("got it, strlcpy function at %p\n",strlcpy); 

g = 16; 
msg = "this is messages boy!"; 
memset(iv,0,sizeof(iv)); 
strlcpy(iv,msg,sizeof(iv)); 
for(k=0;k<strlen(msg);k++){ 
    printf("%c",iv[k]); 
} 
printf("\n%ul",g); 
printf(" and %d",sizeof(iv)); 

l = returnMsg(buff, sizeof(buff),msg); 
printf("%s",l); 

dlclose(handle); 
} 

答えて

3
if ((errorno= g_snprintf(buff, n, "%s",msg)) !=0) 
    printf("error cannot use g_snprintf.."); 
    return 1; 

ボーイ、これはpythonのではありません。あなたはブラケット{}が必要です。

+0

ああ、それは私のせいです。だからあなたが目標とするポインタを使用する方法は? – hafidh

関連する問題