-
strncpy core dump.
Hi,
Prog-1 works fine. But not Prog-2. It gives core dump. Can you pls help.
Prog-1
char str1[]= "To be or not to be";
char str2[6];
strncpy (str2,str1,5);
str2[5]='\0';
Printf(XYZ, "%s(): strncpy str2 = %s \n", __FUNCTION__, str2);
Output: To be
Fine.
Prog-2
main()
{
char scl_ofile[] = "/mnt/ttt.mp4";
abc(scl_ofile);
}
int abc(char * filename)
{
char *args1[] = {"abc", "25", "ttt.h264", ""}; //I need this frame work.
Printf(XYZ, "%s(): strncpy args1[3] = %s \n", __FUNCTION__, args1[3]); //It prints fine.
strncpy(args1[3], filename, 50); //Looks like core dump is happening here.Changing it to the exact size of 12 also did not work.
Printf(XYZ, "%s(): strncpy args1[3] = %s \n", __FUNCTION__, args1[3]); //Nothig is printed
// pqr() //to send as main(argc,argv) type
}
-
When I used strn1=strn2 like in CPP, it works fine. But I did not understand, why strncpy won't work line in regular C.
-
In this line:
Code:
strncpy(args1[3], filename, 50);
you have a couple of problems that wlil cause a core dump.
1. args1[3] is the empty terminating member "". It is only 1 char in size (for the NUL byte), yet you are copying up to 50 chars there.
2. args1[] is an array of char pointers, yet is initialized with string literal values, which, depending upon the system and compiler flags, may not be writeable.