Whenever I am trying to use sprintf() while coding in C, I am getting a warning saying :
"warning: ‘%s’ directive writing up to 49 bytes into a region of size 39 [-Wformat-overflow=]"
It is also producing a note saying :
"note: ‘sprintf’ output between 13 and 62 bytes into a destination of size 50 62 | sprintf(msg,"fopen-ing "%s"",data_file);"
Below I am giving some part of my code, mainly where I am getting this warning.
char data_file[50]; // Global
void initialize_from_data_file()
{
FILE *fpS;
if((fpS = fopen(data_file,"r")) == NULL)
{
char msg[50];
sprintf(msg,"fopen-ing \"%s\"",data_file);
perror(msg);
exit(1);
}
...
}
As I am newly using this language so unable to understand how to remove this warning.
msgis too small to accommodate the result ofsprintf.sprintfdoes? Can you estimate how many characters it's going to produce? Can you verify if that output fits the destination array?