Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do I get warning in this C code using fprintf?

fprintf(pFile,msg.c_str());

why do I get a warning in Xcode :

Format string is not a string literal (potentially insecure)

I assume I get this warning to prevent attacks were msg contains some thing like %s which stream the stack to the screen until it gets to null termination. Is there any safe way to use fprintf in this case?

like image 551
0x90 Avatar asked Nov 29 '22 17:11

0x90


1 Answers

You can either give a format string,

fprintf(pFile, "%s", msg.c_str());

or use fputs,

fputs(msg.c_str(), pFile);
like image 60
Daniel Fischer Avatar answered Dec 01 '22 07:12

Daniel Fischer