Search code examples
cunixsystem-callsmemsetshellforge

Undefined reference to memset


I wrote simple c program using system call and it worked fine when I compiled it with gcc. But when I tried to generate shell code (shellforge) using command

./sf.py username.c

I got error stating

Undefined reference to memset

How to resolve this? Here is my code

#define O_CREAT     0100
#define O_WRONLY    01
#define O_APPEND    02000
#define IDLEN 100
int main(void)
{
    char user_name[IDLEN]="";
    char pass1[IDLEN]="";
    char pass2[IDLEN]="";
    int fd,a,b,c;

    //to give user direction
    char user[]="Please type your username and hit enter:\n";
    char p1[]="Please type password and hit enter:\n";
    char p2[]="Please type password and hit enter:\n";

    write(1,user,sizeof(user));
    a=read(0,user_name,IDLEN);
    user_name[a-1]='\0';
    while(1)
    {
        write(1,p1,sizeof(p1));
        b=read(0,pass1,IDLEN);
        pass1[b-1]='\0';
        write(1,p2,sizeof(p2));
        c=read(0,pass2,IDLEN);
        pass2[c-1]='\0';

        int temp=0;
        while(pass1[temp] == pass2[temp])
        {
            if(pass1[temp] == '\0' || pass2[temp] == '\0')
            {
                break;
            }
            temp++;
        }
        if(pass1[temp] == '\0' && pass2[temp] == '\0')
        {
            break;
        }else{
            char error[] = "password does not match! please type your password again";
            write(1,error,sizeof(error));
        }
    }

    fd = open("user.txt",O_CREAT|O_WRONLY|O_APPEND,0666);
    if(fd<0)
    {
        return 1;
    }

    char file_user[]="\nUsername:";
    char file_pass[]="\nPassword:";
    write(fd,file_user,sizeof(file_user)-1);
    write(fd,user_name,a-1);
    write(fd,file_pass,sizeof(file_pass)-1);
    write(fd,pass1,b-1);
}

Solution

  • include <string.h>; memset() is usually a macro and not a symbol in the standard libraries.

    EDIT: although not called explicitly, the compiler might call it for char user_name[IDLEN]=""; and the other string variables.

    You should compile with '-W -Wall' to detect such issues.