M
mathog
Is this sscanf adequate to prevent a buffer overflow if
the input text field is >127 characters in length? The goal
is to put everything from the first character of the text
following the 3 floats to the end of the line into fontname.
But only if it will all fit into the buffer.
int elements;
float f1,f2,f3;
char fontname[128];
char *string;
while(1){
/* load the string from a file, or exit loop on EOF
Input lines look like:
1.23 2.34 -1.2323 some text here
*/
elements = sscanf(string,
"%lf %lf %lf %127[^\n]",
&f1,&f2,&f3, &fontname[0]);
/* do something with the values */
}
The code at present lacks the 127, and I assume
that it would generate a memory access error on an excessively long
input line.
Thanks,
David Mathog
the input text field is >127 characters in length? The goal
is to put everything from the first character of the text
following the 3 floats to the end of the line into fontname.
But only if it will all fit into the buffer.
int elements;
float f1,f2,f3;
char fontname[128];
char *string;
while(1){
/* load the string from a file, or exit loop on EOF
Input lines look like:
1.23 2.34 -1.2323 some text here
*/
elements = sscanf(string,
"%lf %lf %lf %127[^\n]",
&f1,&f2,&f3, &fontname[0]);
/* do something with the values */
}
The code at present lacks the 127, and I assume
that it would generate a memory access error on an excessively long
input line.
Thanks,
David Mathog