From a2e6f82b127ee038ef1994e65c25f24148c56a60 Mon Sep 17 00:00:00 2001 From: Sven Panne Date: Sat, 5 Aug 2006 16:31:20 +0000 Subject: [PATCH] Check fgets for return value to avoid warnings. git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@695 7f0cb862-5218-0410-a997-914c9d46530a --- ChangeLog | 8 ++++++++ progs/demos/Fractals/fractals.c | 24 +++++++++++++++++------- progs/demos/Fractals_random/fractals_random.c | 24 +++++++++++++++++------- progs/demos/Lorenz/lorenz.c | 16 +++++++++++++--- 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index 372086c..bde2fcc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1168,3 +1168,11 @@ generated by autogen.sh. (303) Moved GLUT_INIT_STATE to , it is not part of the original GLUT. + +************************************************************************** +* Changes on August 05, 2006. +************************************************************************** + +(304) Updated build requirements for SuSE 10.1. + +(305) Check fgets for return value to avoid warnings. diff --git a/progs/demos/Fractals/fractals.c b/progs/demos/Fractals/fractals.c index 9bb3bfe..0852f1a 100644 --- a/progs/demos/Fractals/fractals.c +++ b/progs/demos/Fractals/fractals.c @@ -208,6 +208,16 @@ Special(int key, int x, int y) } +static void +checkedFGets ( char *s, int size, FILE *stream ) +{ + if ( fgets ( s, size, stream ) == NULL ) { + fprintf ( stderr, "fgets failed\n"); + exit ( EXIT_FAILURE ); + } +} + + void readConfigFile ( char *fnme ) { FILE *fptr = fopen ( fnme, "rt" ) ; @@ -217,13 +227,13 @@ void readConfigFile ( char *fnme ) if ( fptr ) { /* Read a header line */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; /* Read a comment line */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; /* Read the window title */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; /* We assume here that this line will not exceed 79 characters plus a newline (window_title is 80 characters long). That'll cause a buffer overflow. For a simple program like this, though, we're letting it @@ -232,21 +242,21 @@ void readConfigFile ( char *fnme ) sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ; /* Read a comment line */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; /* Read the number of affine transformations */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; sscanf ( inputline, "%d", &num_trans ) ; affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ; /* Read a comment line */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; for ( i = 0; i < num_trans; i++ ) { /* Read an affine transformation definition */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01, &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ; } diff --git a/progs/demos/Fractals_random/fractals_random.c b/progs/demos/Fractals_random/fractals_random.c index bf290bc..3ab62c0 100644 --- a/progs/demos/Fractals_random/fractals_random.c +++ b/progs/demos/Fractals_random/fractals_random.c @@ -255,6 +255,16 @@ MouseWheel ( int wheel_number, int direction, int x, int y ) } +static void +checkedFGets ( char *s, int size, FILE *stream ) +{ + if ( fgets ( s, size, stream ) == NULL ) { + fprintf ( stderr, "fgets failed\n"); + exit ( EXIT_FAILURE ); + } +} + + void readConfigFile ( char *fnme ) { FILE *fptr = fopen ( fnme, "rt" ) ; @@ -264,13 +274,13 @@ void readConfigFile ( char *fnme ) if ( fptr ) { /* Read a header line */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; /* Read a comment line */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; /* Read the window title */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; /* We assume here that this line will not exceed 79 characters plus a newline (window_title is 80 characters long). That'll cause a buffer overflow. For a simple program like this, though, we're letting it @@ -279,21 +289,21 @@ void readConfigFile ( char *fnme ) sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ; /* Read a comment line */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; /* Read the number of affine transformations */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; sscanf ( inputline, "%d", &num_trans ) ; affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ; /* Read a comment line */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; for ( i = 0; i < num_trans; i++ ) { /* Read an affine transformation definition */ - fgets ( inputline, 256, fptr ) ; + checkedFGets ( inputline, sizeof ( inputline ), fptr ) ; sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01, &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ; } diff --git a/progs/demos/Lorenz/lorenz.c b/progs/demos/Lorenz/lorenz.c index f2de979..93ed843 100644 --- a/progs/demos/Lorenz/lorenz.c +++ b/progs/demos/Lorenz/lorenz.c @@ -107,6 +107,16 @@ void advance_in_time ( double time_step, double position[3], double new_position ( deriv0[i] + 2.0 * ( deriv1[i] + deriv2[i] ) + deriv3[i] ) ; } +static void +checkedFGets ( char *s, int size, FILE *stream ) +{ + if ( fgets ( s, size, stream ) == NULL ) { + fprintf ( stderr, "fgets failed\n"); + exit ( EXIT_FAILURE ); + } +} + + /* GLUT callbacks */ #define INPUT_LINE_LENGTH 80 @@ -142,15 +152,15 @@ void key_cb ( unsigned char key, int x, int y ) case 'm' : case 'M' : /* Modify the Lorenz parameters */ printf ( "Please enter new value for (default %f, currently %f): ", s0, sigma ) ; - fgets ( inputline, INPUT_LINE_LENGTH-1, stdin ) ; + checkedFGets ( inputline, sizeof ( inputline ), stdin ) ; sscanf ( inputline, "%lf", &sigma ) ; printf ( "Please enter new value for (default %f, currently %f): ", b0, b ) ; - fgets ( inputline, INPUT_LINE_LENGTH-1, stdin ) ; + checkedFGets ( inputline, sizeof ( inputline ), stdin ) ; sscanf ( inputline, "%lf", &b ) ; printf ( "Please enter new value for (default %f, currently %f): ", r0, r ) ; - fgets ( inputline, INPUT_LINE_LENGTH-1, stdin ) ; + checkedFGets ( inputline, sizeof ( inputline ), stdin ) ; sscanf ( inputline, "%lf", &r ) ; break ; -- 1.7.10.4