From: <and...@us...> - 2009-01-26 09:25:15
|
Revision: 9390 http://plplot.svn.sourceforge.net/plplot/?rev=9390&view=rev Author: andrewross Date: 2009-01-26 09:25:06 +0000 (Mon, 26 Jan 2009) Log Message: ----------- Properly check the return value of various functions for errors. Problems shown up via the gcc warn_unused_result attribute when compiling Debian packages. Modified Paths: -------------- trunk/bindings/octave/massage.c trunk/drivers/gd.c trunk/drivers/pbm.c trunk/examples/c/x20c.c trunk/src/plcore.c trunk/utils/pltek.c Modified: trunk/bindings/octave/massage.c =================================================================== --- trunk/bindings/octave/massage.c 2009-01-26 09:06:30 UTC (rev 9389) +++ trunk/bindings/octave/massage.c 2009-01-26 09:25:06 UTC (rev 9390) @@ -44,8 +44,7 @@ exit(1); } - while (! feof(fp)){ - fgets(b, sizeof(b), fp); + while (! feof(fp) && fgets(b, sizeof(b), fp) != NULL ){ if (strchr(b, '-')) strcpy(doc[item++], b); } @@ -56,8 +55,7 @@ exit(1); } - while (! feof(fp)){ - fgets(b, sizeof(b), fp); + while (! feof(fp) && fgets(b, sizeof(b), fp) != NULL){ if ((p2 = strchr(b, '('))){ /* function ... = ...( */ p1 = p2; while(*p1-- != ' '); @@ -67,7 +65,10 @@ strncpy(tok, p1, p2-p1); *(tok+(int)(p2-p1))='\0'; printf("%s", b); - fgets(b, sizeof(b), fp); + if (fgets(b, sizeof(b), fp) == NULL) { + fprintf(stderr,"Error reading line\n"); + return 1; + } printf("%s%%\n", b); /* % function ... = ...(*/ sprintf(b,"plplot_octave_txt/%s.txt", tok); @@ -89,16 +90,24 @@ } else { printf("%% Original PLplot call documentation:\n%%\n"); fp1 = fopen(b,"r"); - while(!feof(fp1)) { - fgets(b, sizeof(b), fp1); + while(!feof(fp1) && fgets(b, sizeof(b), fp1) != NULL) { printf("%% %s", b); } fclose(fp1); } - fgets(b, sizeof(b), fp); /* % No doc...*/ - fgets(b, sizeof(b), fp); + if (fgets(b, sizeof(b), fp) == NULL) { /* % No doc...*/ + fprintf(stderr,"Error reading line\n"); + return 1; + } + if (fgets(b, sizeof(b), fp) == NULL) { + fprintf(stderr,"Error reading line\n"); + return 1; + } printf("%s", b); /* plplot_oct...*/ - fgets(b, sizeof(b), fp); + if (fgets(b, sizeof(b), fp) == NULL) { + fprintf(stderr,"Error reading line\n"); + return 1; + } printf("%s\n", b); /* endfunction*/ } } Modified: trunk/drivers/gd.c =================================================================== --- trunk/drivers/gd.c 2009-01-26 09:06:30 UTC (rev 9389) +++ trunk/drivers/gd.c 2009-01-26 09:25:06 UTC (rev 9390) @@ -1305,6 +1305,7 @@ int im_size=0; int png_compression; void *im_ptr=NULL; + size_t nwrite; if (pls->family || pls->page == 1) { @@ -1348,7 +1349,9 @@ im_ptr = gdImagePngPtr(dev->im_out, &im_size); #endif if( im_ptr ) { - fwrite(im_ptr, sizeof(char), im_size, pls->OutFile); + nwrite = fwrite(im_ptr, sizeof(char), im_size, pls->OutFile); + if (nwrite != im_size) + plabort("gd driver: Error writing png file"); gdFree(im_ptr); } @@ -1528,6 +1531,7 @@ png_Dev *dev=(png_Dev *)pls->dev; int im_size=0; void *im_ptr=NULL; + size_t nwrite; if (pls->family || pls->page == 1) { /* image is written to output file by the driver @@ -1536,7 +1540,9 @@ /* gdImageJpeg(dev->im_out, pls->OutFile, pls->dev_compression); */ im_ptr = gdImageJpegPtr(dev->im_out, &im_size, pls->dev_compression); if( im_ptr ) { - fwrite(im_ptr, sizeof(char), im_size, pls->OutFile); + nwrite = fwrite(im_ptr, sizeof(char), im_size, pls->OutFile); + if (nwrite != im_size) + plabort("gd driver: Error writing png file"); gdFree(im_ptr); } @@ -1560,6 +1566,7 @@ png_Dev *dev=(png_Dev *)pls->dev; int im_size=0; void *im_ptr=NULL; + size_t nwrite; if (pls->family || pls->page == 1) { /* image is written to output file by the driver @@ -1568,7 +1575,9 @@ /* gdImageGif(dev->im_out, pls->OutFile); */ im_ptr = gdImageGifPtr(dev->im_out, &im_size); if( im_ptr ) { - fwrite(im_ptr, sizeof(char), im_size, pls->OutFile); + nwrite = fwrite(im_ptr, sizeof(char), im_size, pls->OutFile); + if (nwrite != im_size) + plabort("gd driver: Error writing png file"); gdFree(im_ptr); } Modified: trunk/drivers/pbm.c =================================================================== --- trunk/drivers/pbm.c 2009-01-26 09:06:30 UTC (rev 9389) +++ trunk/drivers/pbm.c 2009-01-26 09:25:06 UTC (rev 9390) @@ -235,6 +235,7 @@ plD_eop_pbm(PLStream *pls) { FILE *fp = pls->OutFile; + size_t im_size, nwrite; if (fp != NULL) { fprintf(fp, "%s\n", "P6"); @@ -249,7 +250,10 @@ fprintf(fp, "%c", cmap[i][j][k]); } */ - fwrite( cmap, 1, pls->xlength * pls->ylength * 3, fp ); + im_size = pls->xlength*pls->ylength*3; + nwrite = fwrite( cmap, 1, im_size, fp ); + if (nwrite != im_size) + plabort("gd driver: Error writing png file"); fclose(fp); } Modified: trunk/examples/c/x20c.c =================================================================== --- trunk/examples/c/x20c.c 2009-01-26 09:06:30 UTC (rev 9389) +++ trunk/examples/c/x20c.c 2009-01-26 09:25:06 UTC (rev 9390) @@ -303,25 +303,41 @@ if ((fp = fopen(fname,"rb")) == NULL) return 1; - fscanf(fp,"%s\n", ver); /* version */ + if (fscanf(fp,"%s\n", ver) != 1) { /* version */ + fclose(fp); + return 1; + } /* printf("version: %s\n", ver);*/ - if (strcmp(ver, "P5")) /* I only understand this! */ + if (strcmp(ver, "P5")) { /* I only understand this! */ + fclose(fp); return 1; + } while((i=fgetc(fp)) == '#') { - fgets(ver, 80, fp); /* comments */ + if (fgets(ver, 80, fp) == NULL) { /* comments */ + fclose(fp); + return 1; + } /* printf("%s", ver); */ } ungetc(i, fp); - fscanf(fp,"%d%d%d\n", &w, &h, num_col); /* width, height num colors */ + if (fscanf(fp,"%d%d%d\n", &w, &h, num_col) != 3) { /* width, height num colors */ + fclose(fp); + return 1; + } /* printf("width=%d height=%d num_col=%d\n", w, h, *num_col); */ img = (unsigned char *) malloc(w*h*sizeof(char)); plAlloc2dGrid(&imf, w, h); - fread(img, sizeof(char), w*h, fp); + if (fread(img, sizeof(char), w*h, fp) != w*h) { + fclose(fp); + free(img); + plFree2dGrid(imf,w,h); + return 1; + } fclose(fp); for (i=0; i<w; i++) Modified: trunk/src/plcore.c =================================================================== --- trunk/src/plcore.c 2009-01-26 09:06:30 UTC (rev 9389) +++ trunk/src/plcore.c 2009-01-26 09:25:06 UTC (rev 9390) @@ -2150,7 +2150,8 @@ inBuildTree = 1; } } - chdir(currdir); + if (chdir(currdir) != 0) + pldebug("plInBuildTree():", "Unable to chdir to current directory"); } } inited = 1; Modified: trunk/utils/pltek.c =================================================================== --- trunk/utils/pltek.c 2009-01-26 09:06:30 UTC (rev 9389) +++ trunk/utils/pltek.c 2009-01-26 09:25:06 UTC (rev 9390) @@ -36,6 +36,7 @@ long start[MAXPAGES]; /* start (offset) of each page */ char buf[BUFSZ], xtra, lastchar = '\0'; char c, ibuf[128], *t; + size_t nwrite; if (argc < 2) { describe(); @@ -134,7 +135,10 @@ istop = 0; for (i = 0; i < 8196; i++) { /* less than 8MB per page! */ if (xtra) { - fwrite(&xtra, 1, 1, stdout); + if (fwrite(&xtra, 1, 1, stdout) != 1) { + fprintf(stderr,"Error writing to stdout\n"); + exit(1); + } xtra = '\0'; } nb = fread(buf, 1, BUFSZ, fd); @@ -143,7 +147,10 @@ ifirst = 0; for (j = 0; j < nb; j++) { if (buf[j] == '\f') { - fwrite(&buf[ifirst], 1, j - ifirst, stdout); + if (fwrite(&buf[ifirst], 1, j - ifirst, stdout) != j-ifirst) { + fprintf(stderr,"Error writing to stdout\n"); + exit(1); + } fflush(stdout); istop = 1; break; @@ -155,7 +162,10 @@ xtra = ESC; j--; } - fwrite(&buf[ifirst], 1, j - ifirst, stdout); + if (fwrite(&buf[ifirst], 1, j - ifirst, stdout) != j-ifirst ) { + fprintf(stderr,"Error writing to stdout\n"); + exit(1); + } } if ( termtype == xterm ) tek_mode(ALPHA_MODE); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |