hafidh
hafidh

Reputation: 101

How to return this buffer value in c?

Suppose I have 2 dll's. the first one, I get this code from openbsd.org about on using strlcpy function to avoid buffer overflow and the second is using gtk-glib-2.0 dll exported function again to avoid buffer overflow "g_snprintf" rather than standard calling c function snprintf. The problem is arising when I want to make returning buffer string value, so that I can to make this code to be dll and call both of functions from python language easely. here is the snippet:

#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);
} 

Upvotes: 0

Views: 979

Answers (1)

J-16 SDiZ
J-16 SDiZ

Reputation: 26920

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

Boy, this is not python. You need bracket {}.

Upvotes: 3

Related Questions