From 8bd1b91059ced339d56510ba96e6d62839263ac5 Mon Sep 17 00:00:00 2001 From: Fanie Oosthuysen Date: Sun, 27 Sep 2015 15:16:37 +0200 Subject: [PATCH] Update giflib to latest --- src/lib/gif/dgif_lib.c | 23 +++++++++++++++----- src/lib/gif/egif_lib.c | 42 ++++++++++++++++++------------------ src/lib/gif/gif_lib.h | 2 +- src/lib/gif/gifalloc.c | 48 +++++++++++++++++++++++++----------------- 4 files changed, 69 insertions(+), 46 deletions(-) diff --git a/src/lib/gif/dgif_lib.c b/src/lib/gif/dgif_lib.c index 5bd5bf8b..320fdb9e 100644 --- a/src/lib/gif/dgif_lib.c +++ b/src/lib/gif/dgif_lib.c @@ -113,6 +113,7 @@ DGifOpenFileHandle(int FileHandle, int *Error) /*@=mustfreeonly@*/ /* Let's see if this is a GIF file: */ + /* coverity[check_return] */ if (READ(GifFile, (unsigned char *)Buf, GIF_STAMP_LEN) != GIF_STAMP_LEN) { if (Error != NULL) *Error = D_GIF_ERR_READ_FAILED; @@ -188,6 +189,7 @@ DGifOpen(void *userData, InputFunc readFunc, int *Error) GifFile->UserData = userData; /* TVT */ /* Lets see if this is a GIF file: */ + /* coverity[check_return] */ if (READ(GifFile, (unsigned char *)Buf, GIF_STAMP_LEN) != GIF_STAMP_LEN) { if (Error != NULL) *Error = D_GIF_ERR_READ_FAILED; @@ -209,7 +211,8 @@ DGifOpen(void *userData, InputFunc readFunc, int *Error) if (DGifGetScreenDesc(GifFile) == GIF_ERROR) { free((char *)Private); free((char *)GifFile); - *Error = D_GIF_ERR_NO_SCRN_DSCR; + if (Error != NULL) + *Error = D_GIF_ERR_NO_SCRN_DSCR; return NULL; } @@ -267,6 +270,7 @@ DGifGetScreenDesc(GifFileType *GifFile) /* Get the global color map: */ GifFile->SColorMap->SortFlag = SortFlag; for (i = 0; i < GifFile->SColorMap->ColorCount; i++) { + /* coverity[check_return] */ if (READ(GifFile, Buf, 3) != 3) { GifFreeMapObject(GifFile->SColorMap); GifFile->SColorMap = NULL; @@ -299,6 +303,7 @@ DGifGetRecordType(GifFileType *GifFile, GifRecordType* Type) return GIF_ERROR; } + /* coverity[check_return] */ if (READ(GifFile, &Buf, 1) != 1) { GifFile->Error = D_GIF_ERR_READ_FAILED; return GIF_ERROR; @@ -372,6 +377,7 @@ DGifGetImageDesc(GifFileType *GifFile) /* Get the image local color map: */ for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) { + /* coverity[check_return] */ if (READ(GifFile, Buf, 3) != 3) { GifFreeMapObject(GifFile->Image.ColorMap); GifFile->Error = D_GIF_ERR_READ_FAILED; @@ -385,12 +391,14 @@ DGifGetImageDesc(GifFileType *GifFile) } if (GifFile->SavedImages) { - if ((GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages, - sizeof(SavedImage) * - (GifFile->ImageCount + 1))) == NULL) { + SavedImage* new_saved_images = + (SavedImage *)realloc(GifFile->SavedImages, + sizeof(SavedImage) * (GifFile->ImageCount + 1)); + if (new_saved_images == NULL) { GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM; return GIF_ERROR; } + GifFile->SavedImages = new_saved_images; } else { if ((GifFile->SavedImages = (SavedImage *) malloc(sizeof(SavedImage))) == NULL) { @@ -519,6 +527,7 @@ DGifGetExtension(GifFileType *GifFile, int *ExtCode, GifByteType **Extension) return GIF_ERROR; } + /* coverity[check_return] */ if (READ(GifFile, &Buf, 1) != 1) { GifFile->Error = D_GIF_ERR_READ_FAILED; return GIF_ERROR; @@ -546,7 +555,7 @@ DGifGetExtensionNext(GifFileType *GifFile, GifByteType ** Extension) if (Buf > 0) { *Extension = Private->Buf; /* Use private unused buffer. */ (*Extension)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */ - /* coverity[tainted_data] */ + /* coverity[tainted_data,check_return] */ if (READ(GifFile, &((*Extension)[1]), Buf) != Buf) { GifFile->Error = D_GIF_ERR_READ_FAILED; return GIF_ERROR; @@ -668,6 +677,7 @@ DGifGetWord(GifFileType *GifFile, GifWord *Word) { unsigned char c[2]; + /* coverity[check_return] */ if (READ(GifFile, c, 2) != 2) { GifFile->Error = D_GIF_ERR_READ_FAILED; return GIF_ERROR; @@ -712,6 +722,7 @@ DGifGetCodeNext(GifFileType *GifFile, GifByteType **CodeBlock) GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; /* coverity[tainted_data_argument] */ + /* coverity[check_return] */ if (READ(GifFile, &Buf, 1) != 1) { GifFile->Error = D_GIF_ERR_READ_FAILED; return GIF_ERROR; @@ -746,6 +757,7 @@ DGifSetupDecompress(GifFileType *GifFile) GifPrefixType *Prefix; GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; + /* coverity[check_return] */ if (READ(GifFile, &CodeSize, 1) < 1) { /* Read Code size from file. */ return GIF_ERROR; /* Failed to read Code size. */ } @@ -1021,6 +1033,7 @@ DGifBufferedInput(GifFileType *GifFile, GifByteType *Buf, GifByteType *NextByte) { if (Buf[0] == 0) { /* Needs to read the next buffer - this one is empty: */ + /* coverity[check_return] */ if (READ(GifFile, Buf, 1) != 1) { GifFile->Error = D_GIF_ERR_READ_FAILED; return GIF_ERROR; diff --git a/src/lib/gif/egif_lib.c b/src/lib/gif/egif_lib.c index c62e86c1..39a62b2e 100644 --- a/src/lib/gif/egif_lib.c +++ b/src/lib/gif/egif_lib.c @@ -58,10 +58,10 @@ EGifOpenFileName(const char *FileName, const bool TestExistence, int *Error) GifFileType *GifFile; if (TestExistence) - FileHandle = open(FileName, O_WRONLY | O_CREAT | O_EXCL, + FileHandle = open(FileName, O_WRONLY | O_CREAT | O_EXCL, S_IREAD | S_IWRITE); else - FileHandle = open(FileName, O_WRONLY | O_CREAT | O_TRUNC, + FileHandle = open(FileName, O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE); if (FileHandle == -1) { @@ -190,7 +190,7 @@ EGifGetGifVersion(GifFileType *GifFile) GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private; int i, j; - /* + /* * Bulletproofing - always write GIF89 if we need to. * Note, we don't clear the gif89 flag here because * users of the sequential API might have called EGifSetGifVersion() @@ -217,7 +217,7 @@ EGifGetGifVersion(GifFileType *GifFile) || function == APPLICATION_EXT_FUNC_CODE) Private->gif89 = true; } - + if (Private->gif89) return GIF89_STAMP; else @@ -226,7 +226,7 @@ EGifGetGifVersion(GifFileType *GifFile) /****************************************************************************** Set the GIF version. In the extremely unlikely event that there is ever - another version, replace the bool argument with an enum in which the + another version, replace the bool argument with an enum in which the GIF87 value is 0 (numerically the same as bool false) and the GIF89 value is 1 (numerically the same as bool true). That way we'll even preserve object-file compatibility! @@ -241,7 +241,7 @@ void EGifSetGifVersion(GifFileType *GifFile, const bool gif89) /****************************************************************************** All writes to the GIF should go through this. ******************************************************************************/ -static int InternalWrite(GifFileType *GifFileOut, +static int InternalWrite(GifFileType *GifFileOut, const unsigned char *buf, size_t len) { GifFilePrivateType *Private = (GifFilePrivateType*)GifFileOut->Private; @@ -561,7 +561,7 @@ EGifPutExtensionLeader(GifFileType *GifFile, const int ExtCode) Put extension block data (see GIF manual) into a GIF file. ******************************************************************************/ int -EGifPutExtensionBlock(GifFileType *GifFile, +EGifPutExtensionBlock(GifFileType *GifFile, const int ExtLen, const void *Extension) { @@ -660,7 +660,7 @@ size_t EGifGCBToExtension(const GraphicsControlBlock *GCB, Replace the Graphics Control Block for a saved image, if it exists. ******************************************************************************/ -int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB, +int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB, GifFileType *GifFile, int ImageIndex) { int i; @@ -708,7 +708,7 @@ EGifPutCode(GifFileType *GifFile, int CodeSize, const GifByteType *CodeBlock) } /* No need to dump code size as Compression set up does any for us: */ - /* + /* * Buf = CodeSize; * if (InternalWrite(GifFile, &Buf, 1) != 1) { * GifFile->Error = E_GIF_ERR_WRITE_FAILED; @@ -891,7 +891,7 @@ EGifCompressLine(GifFileType *GifFile, while (i < LineLen) { /* Decode LineLen items. */ Pixel = Line[i++]; /* Get next pixel from stream. */ - /* Form a new unique key to search hash table for the code combines + /* Form a new unique key to search hash table for the code combines * CrntCode as Prefix string with Pixel as postfix char. */ NewKey = (((uint32_t) CrntCode) << 8) + Pixel; @@ -1047,9 +1047,9 @@ EGifBufferedOutput(GifFileType *GifFile, ******************************************************************************/ static int -EGifWriteExtensions(GifFileType *GifFileOut, - ExtensionBlock *ExtensionBlocks, - int ExtensionBlockCount) +EGifWriteExtensions(GifFileType *GifFileOut, + ExtensionBlock *ExtensionBlocks, + int ExtensionBlockCount) { if (ExtensionBlocks) { ExtensionBlock *ep; @@ -1072,9 +1072,9 @@ EGifWriteExtensions(GifFileType *GifFileOut, } int -EGifSpew(GifFileType *GifFileOut) +EGifSpew(GifFileType *GifFileOut) { - int i, j; + int i, j; if (EGifPutScreenDesc(GifFileOut, GifFileOut->SWidth, @@ -1094,7 +1094,7 @@ EGifSpew(GifFileType *GifFileOut) if (sp->RasterBits == NULL) continue; - if (EGifWriteExtensions(GifFileOut, + if (EGifWriteExtensions(GifFileOut, sp->ExtensionBlocks, sp->ExtensionBlockCount) == GIF_ERROR) return (GIF_ERROR); @@ -1109,8 +1109,8 @@ EGifSpew(GifFileType *GifFileOut) return (GIF_ERROR); if (sp->ImageDesc.Interlace) { - /* - * The way an interlaced image should be written - + /* + * The way an interlaced image should be written - * offsets and jumps... */ int InterlacedOffset[] = { 0, 4, 2, 1 }; @@ -1118,11 +1118,11 @@ EGifSpew(GifFileType *GifFileOut) int k; /* Need to perform 4 passes on the images: */ for (k = 0; k < 4; k++) - for (j = InterlacedOffset[k]; + for (j = InterlacedOffset[k]; j < SavedHeight; j += InterlacedJumps[k]) { - if (EGifPutLine(GifFileOut, - sp->RasterBits + j * SavedWidth, + if (EGifPutLine(GifFileOut, + sp->RasterBits + j * SavedWidth, SavedWidth) == GIF_ERROR) return (GIF_ERROR); } diff --git a/src/lib/gif/gif_lib.h b/src/lib/gif/gif_lib.h index b556bc69..ac0307d7 100644 --- a/src/lib/gif/gif_lib.h +++ b/src/lib/gif/gif_lib.h @@ -13,7 +13,7 @@ extern "C" { #define GIFLIB_MAJOR 5 #define GIFLIB_MINOR 1 -#define GIFLIB_RELEASE 0 +#define GIFLIB_RELEASE 1 #define GIF_ERROR 0 #define GIF_OK 1 diff --git a/src/lib/gif/gifalloc.c b/src/lib/gif/gifalloc.c index 9862464e..f4118d6c 100644 --- a/src/lib/gif/gifalloc.c +++ b/src/lib/gif/gifalloc.c @@ -13,7 +13,7 @@ #define MAX(x, y) (((x) > (y)) ? (x) : (y)) /****************************************************************************** - Miscellaneous utility functions + Miscellaneous utility functions ******************************************************************************/ /* return smallest bitfield size n will fit in */ @@ -29,7 +29,7 @@ GifBitSize(int n) } /****************************************************************************** - Color map object functions + Color map object functions ******************************************************************************/ /* @@ -46,7 +46,7 @@ GifMakeMapObject(int ColorCount, const GifColorType *ColorMap) if (ColorCount != (1 << GifBitSize(ColorCount))) { return ((ColorMapObject *) NULL); } - + Object = (ColorMapObject *)malloc(sizeof(ColorMapObject)); if (Object == (ColorMapObject *) NULL) { return ((ColorMapObject *) NULL); @@ -104,7 +104,7 @@ DumpColorMap(ColorMapObject *Object, #endif /* DEBUG */ /******************************************************************************* - Compute the union of two given color maps and return it. If result can't + Compute the union of two given color maps and return it. If result can't fit into 256 colors, NULL is returned, the allocated union otherwise. ColorIn1 is copied as is to ColorUnion, while colors from ColorIn2 are copied iff they didn't exist before. ColorTransIn2 maps the old @@ -131,14 +131,14 @@ GifUnionColorMap(const ColorMapObject *ColorIn1, if (ColorUnion == NULL) return (NULL); - /* + /* * Copy ColorIn1 to ColorUnion. */ for (i = 0; i < ColorIn1->ColorCount; i++) ColorUnion->Colors[i] = ColorIn1->Colors[i]; CrntSlot = ColorIn1->ColorCount; - /* + /* * Potentially obnoxious hack: * * Back CrntSlot down past all contiguous {0, 0, 0} slots at the end @@ -154,7 +154,7 @@ GifUnionColorMap(const ColorMapObject *ColorIn1, for (i = 0; i < ColorIn2->ColorCount && CrntSlot <= 256; i++) { /* Let's see if this color already exists: */ for (j = 0; j < ColorIn1->ColorCount; j++) - if (memcmp (&ColorIn1->Colors[j], &ColorIn2->Colors[i], + if (memcmp (&ColorIn1->Colors[j], &ColorIn2->Colors[i], sizeof(GifColorType)) == 0) break; @@ -178,7 +178,7 @@ GifUnionColorMap(const ColorMapObject *ColorIn1, if (RoundUpTo != ColorUnion->ColorCount) { register GifColorType *Map = ColorUnion->Colors; - /* + /* * Zero out slots up to next power of 2. * We know these slots exist because of the way ColorUnion's * start dimension was computed. @@ -187,9 +187,15 @@ GifUnionColorMap(const ColorMapObject *ColorIn1, Map[j].Red = Map[j].Green = Map[j].Blue = 0; /* perhaps we can shrink the map? */ - if (RoundUpTo < ColorUnion->ColorCount) - ColorUnion->Colors = (GifColorType *)realloc(Map, + if (RoundUpTo < ColorUnion->ColorCount) { + GifColorType *new_map = (GifColorType *)realloc(Map, sizeof(GifColorType) * RoundUpTo); + if( new_map == NULL ) { + GifFreeMapObject(ColorUnion); + return ((ColorMapObject *) NULL); + } + ColorUnion->Colors = new_map; + } } ColorUnion->ColorCount = RoundUpTo; @@ -212,7 +218,7 @@ GifApplyTranslation(SavedImage *Image, GifPixelType Translation[]) } /****************************************************************************** - Extension record functions + Extension record functions ******************************************************************************/ int GifAddExtensionBlock(int *ExtensionBlockCount, @@ -225,10 +231,14 @@ GifAddExtensionBlock(int *ExtensionBlockCount, if (*ExtensionBlocks == NULL) *ExtensionBlocks=(ExtensionBlock *)malloc(sizeof(ExtensionBlock)); - else - *ExtensionBlocks = (ExtensionBlock *)realloc(*ExtensionBlocks, + else { + ExtensionBlock* ep_new = (ExtensionBlock *)realloc(*ExtensionBlocks, sizeof(ExtensionBlock) * (*ExtensionBlockCount + 1)); + if( ep_new == NULL ) + return (GIF_ERROR); + *ExtensionBlocks = ep_new; + } if (*ExtensionBlocks == NULL) return (GIF_ERROR); @@ -258,7 +268,7 @@ GifFreeExtensions(int *ExtensionBlockCount, return; for (ep = *ExtensionBlocks; - ep < (*ExtensionBlocks + *ExtensionBlockCount); + ep < (*ExtensionBlocks + *ExtensionBlockCount); ep++) (void)free((char *)ep->Bytes); (void)free((char *)*ExtensionBlocks); @@ -267,7 +277,7 @@ GifFreeExtensions(int *ExtensionBlockCount, } /****************************************************************************** - Image block allocation functions + Image block allocation functions ******************************************************************************/ /* Private Function: @@ -277,7 +287,7 @@ void FreeLastSavedImage(GifFileType *GifFile) { SavedImage *sp; - + if ((GifFile == NULL) || (GifFile->SavedImages == NULL)) return; @@ -307,7 +317,7 @@ FreeLastSavedImage(GifFileType *GifFile) } /* - * Append an image block to the SavedImages array + * Append an image block to the SavedImages array */ SavedImage * GifMakeSavedImage(GifFileType *GifFile, const SavedImage *CopyFrom) @@ -327,7 +337,7 @@ GifMakeSavedImage(GifFileType *GifFile, const SavedImage *CopyFrom) if (CopyFrom != NULL) { memcpy((char *)sp, CopyFrom, sizeof(SavedImage)); - /* + /* * Make our own allocated copies of the heap fields in the * copied record. This guards against potential aliasing * problems. @@ -391,7 +401,7 @@ GifFreeSavedImages(GifFileType *GifFile) if (sp->RasterBits != NULL) free((char *)sp->RasterBits); - + GifFreeExtensions(&sp->ExtensionBlockCount, &sp->ExtensionBlocks); } free((char *)GifFile->SavedImages);