Stack-Based Overflow Error(strcpy and strncpy)

167 Views Asked by At

I have the below code, where the compiler is complaining:

int len = strlen(prgpath);
char* ptr = strrchr(prgpath, '/');
char prgname[64]; 
memset(prgname, 0, sizeof(prgname));
if (ptr==NULL) 
     strcpy(prgname, prgpath);
else 
     strncpy(prgname, (ptr+1), len-(ptr-prgpath));

Compiler gives the below warning:

  "This call to strcpy() and strncpy() contains a buffer overflow. The source string 
   has an allocated size of (unavailable) bytes, and the destination buffer is 64 
   bytes."

How should I replace the above "strcpy" and "strncpy" to resolve the warning. Should I use strlcpy or any other API's present?

0

There are 0 best solutions below