Freebsd9.0 and the fgets directive in gcc

Erich Dollansky erichfreebsdlist at ovitrap.com
Wed Mar 21 15:42:03 UTC 2012


Hi,

On Wednesday 21 March 2012 21:50:25 Martin McCormick wrote:
> I've got some code which I wrote about 6 or 8 years ago that
> apparently doesn't get along right now with FreeBSD9.0. In the
> problem code, there is a loop that uses fgets to read a line
> from a file. It runs properly until the 2708TH iteration and
> then it dumps core with a segmentation fault.
> 
> char string0[256];
> more lines of code . . .
> 
> while ( fgets(string0,sizeof(string0),fp_config)) {

how long is one line? Could it be that the failing line has 256 or more characters?

I would use

while ( fgets(string0,sizeof(string0) -1,fp_config)) {

to make sure that the last character does not cause a buffer overrun, no matter what fgets is supposed to do.

Erich


More information about the freebsd-questions mailing list