src/lib/gifwrite.c 1.7

1.7 2009/04/22 01:57:00 galt
fix tiny buffer overflow bug discovered by user with ubuntu 8.10 and FORTIFY -- use strncpy instead of strcpy
Index: src/lib/gifwrite.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/gifwrite.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -b -B -U 1000000 -r1.6 -r1.7
--- src/lib/gifwrite.c	21 May 2003 21:02:22 -0000	1.6
+++ src/lib/gifwrite.c	22 Apr 2009 01:57:00 -0000	1.7
@@ -1,72 +1,72 @@
 /* writegif.c - stuff to write out a GIF file.  See also comprs.c */
 
 #include "common.h"
 #include "memgfx.h"
 #include "gifcodes.h"
 
 static char const rcsid[] = "$Id$";
 
 static char gifsig[] = "GIF87a";
 
 
 boolean mgSaveToGif(FILE *gif_file, struct memGfx *screen)
 /* Save GIF to an already open file. */
 {
 int i;
 struct gif_header gif;
 struct gif_image gim;
 long gif_wcount;
 
 gif_wcount = (long)screen->width * screen->height;
 zeroBytes(&gif, sizeof(gif));
-strcpy(gif.giftype, gifsig);
+strncpy(gif.giftype, gifsig, sizeof(gif.giftype));
 gif.wlo = gim.wlo = ((screen->width)&0xff);
 gif.whi = gim.whi = ((screen->width>>8)&0xff);
 gif.hlo = gim.hlo = ((screen->height)&0xff);
 gif.hhi = gim.hhi = ((screen->height>>8)&0xff);
 gim.xlo = gim.xhi = gim.ylo = gim.yhi = gim.flags = 0;
 gif.colpix = COLPIXVGA13;
 if (fwrite(&gif, sizeof(gif), 1, gif_file ) < 1)
     goto TRUNCOUT;
 /* write global color map */
 if (fwrite(screen->colorMap, 3, 256, gif_file) < 256)
     goto TRUNCOUT;
 if (fputc(',', gif_file) < 0) /* comma to start image */
     goto TRUNCOUT;
 if (fwrite(&gim, sizeof(gim), 1, gif_file) < 1)
     goto TRUNCOUT;
 fputc(8,gif_file);
 fflush(gif_file);
 i = gif_compress_data(8, screen->pixels, gif_wcount, gif_file);
 switch (i)
     {
     case 0:
         break;
     case -2:
 	warn("Out of memory writing GIF");
 	goto BADOUT;
     case -3:
 	goto TRUNCOUT;
     default:
 	warn("Error code %d writing gif", i);
 	goto BADOUT;
     }
 fputc(';', gif_file); /* end of file for gif */
 return(TRUE);
 TRUNCOUT:
 warn("Disk full writing GIF");
 BADOUT:
 return(FALSE);
 }
 
 void mgSaveGif(struct memGfx *screen, char *name)
 {
 FILE *gifFile = mustOpen(name, "wb");
 if (!mgSaveToGif(gifFile, screen))
     {
     remove(name);
     errAbort("Couldn't save %s", name);
     }
 if (fclose(gifFile) != 0)
     errnoAbort("fclose failed");
 }