diff options
author | Deron Johnson <deron.johnson@sun.com> | 2005-02-17 22:44:24 +0000 |
---|---|---|
committer | Deron Johnson <deron.johnson@sun.com> | 2005-02-17 22:44:24 +0000 |
commit | 5b2b384c319513d102de3e009e1816399d93b81a (patch) | |
tree | f215864d9e5c09faa8d1399349f02c724341e2de /src/parse.c | |
parent | 2773a7214e282f6f673483f5233b880505947c3f (diff) |
Joined with branch XORG-6_8_2.lg3d-rel-0-6-2lg3d-masterlg3d-dev-0-7-1lg3d-dev-0-7-0lg3d-dev-0-6-2lg3d-dev-0-6-1-1
Diffstat (limited to 'src/parse.c')
-rw-r--r-- | src/parse.c | 101 |
1 files changed, 76 insertions, 25 deletions
diff --git a/src/parse.c b/src/parse.c index 3c819a2..39fe9b4 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1,3 +1,4 @@ +/* $XdotOrg: xc/extras/Xpm/lib/parse.c,v 1.2.4.2 2004/12/17 01:09:36 gisburn Exp $ */ /* * Copyright (C) 1989-95 GROUPE BULL * @@ -40,10 +41,30 @@ * HeDu (hedu@cul-ipn.uni-kiel.de) 4/94 */ +/* October 2004, source code review by Thomas Biege <thomas@suse.de> */ + #include "XpmI.h" #include <ctype.h> #include <string.h> +#ifdef HAS_STRLCAT +# define STRLCAT(dst, src, dstsize) do { \ + if (strlcat(dst, src, dstsize) >= (dstsize)) \ + return (XpmFileInvalid); } while(0) +# define STRLCPY(dst, src, dstsize) do { \ + if (strlcpy(dst, src, dstsize) >= (dstsize)) \ + return (XpmFileInvalid); } while(0) +#else +# define STRLCAT(dst, src, dstsize) do { \ + if ((strlen(dst) + strlen(src)) < (dstsize)) \ + strcat(dst, src); \ + else return (XpmFileInvalid); } while(0) +# define STRLCPY(dst, src, dstsize) do { \ + if (strlen(src) < (dstsize)) \ + strcpy(dst, src); \ + else return (XpmFileInvalid); } while(0) +#endif + LFUNC(ParsePixels, int, (xpmData *data, unsigned int width, unsigned int height, unsigned int ncolors, unsigned int cpp, XpmColor *colorTable, @@ -66,7 +87,7 @@ xpmParseValues(data, width, height, ncolors, cpp, unsigned int *extensions; { unsigned int l; - char buf[BUFSIZ]; + char buf[BUFSIZ + 1]; if (!data->format) { /* XPM 2 or 3 */ @@ -175,10 +196,10 @@ xpmParseColors(data, ncolors, cpp, colorTablePtr, hashtable) XpmColor **colorTablePtr; xpmHashTable *hashtable; { - unsigned int key = 0, l, a, b; + unsigned int key = 0, l, a, b, len; unsigned int curkey; /* current color key */ unsigned int lastwaskey; /* key read */ - char buf[BUFSIZ]; + char buf[BUFSIZ+1]; char curbuf[BUFSIZ]; /* current buffer */ char **sptr, *s; XpmColor *color; @@ -186,6 +207,8 @@ xpmParseColors(data, ncolors, cpp, colorTablePtr, hashtable) char **defaults; int ErrorStatus; + if (ncolors >= UINT_MAX / sizeof(XpmColor)) + return (XpmNoMemory); colorTable = (XpmColor *) XpmCalloc(ncolors, sizeof(XpmColor)); if (!colorTable) return (XpmNoMemory); @@ -197,6 +220,10 @@ xpmParseColors(data, ncolors, cpp, colorTablePtr, hashtable) /* * read pixel value */ + if (cpp >= UINT_MAX - 1) { + xpmFreeColorTable(colorTable, ncolors); + return (XpmNoMemory); + } color->string = (char *) XpmMalloc(cpp + 1); if (!color->string) { xpmFreeColorTable(colorTable, ncolors); @@ -234,13 +261,14 @@ xpmParseColors(data, ncolors, cpp, colorTablePtr, hashtable) } if (!lastwaskey && key < NKEYS) { /* open new key */ if (curkey) { /* flush string */ - s = (char *) XpmMalloc(strlen(curbuf) + 1); + len = strlen(curbuf) + 1; + s = (char *) XpmMalloc(len); if (!s) { xpmFreeColorTable(colorTable, ncolors); return (XpmNoMemory); } defaults[curkey] = s; - strcpy(s, curbuf); + memcpy(s, curbuf, len); } curkey = key + 1; /* set new key */ *curbuf = '\0'; /* reset curbuf */ @@ -251,9 +279,9 @@ xpmParseColors(data, ncolors, cpp, colorTablePtr, hashtable) return (XpmFileInvalid); } if (!lastwaskey) - strcat(curbuf, " "); /* append space */ + STRLCAT(curbuf, " ", sizeof(curbuf));/* append space */ buf[l] = '\0'; - strcat(curbuf, buf);/* append buf */ + STRLCAT(curbuf, buf, sizeof(curbuf)); /* append buf */ lastwaskey = 0; } } @@ -261,12 +289,13 @@ xpmParseColors(data, ncolors, cpp, colorTablePtr, hashtable) xpmFreeColorTable(colorTable, ncolors); return (XpmFileInvalid); } - s = defaults[curkey] = (char *) XpmMalloc(strlen(curbuf) + 1); + len = strlen(curbuf) + 1; /* integer overflow just theoretically possible */ + s = defaults[curkey] = (char *) XpmMalloc(len); if (!s) { xpmFreeColorTable(colorTable, ncolors); return (XpmNoMemory); } - strcpy(s, curbuf); + memcpy(s, curbuf, len); } } else { /* XPM 1 */ /* get to the beginning of the first string */ @@ -279,6 +308,10 @@ xpmParseColors(data, ncolors, cpp, colorTablePtr, hashtable) /* * read pixel value */ + if (cpp >= UINT_MAX - 1) { + xpmFreeColorTable(colorTable, ncolors); + return (XpmNoMemory); + } color->string = (char *) XpmMalloc(cpp + 1); if (!color->string) { xpmFreeColorTable(colorTable, ncolors); @@ -307,19 +340,20 @@ xpmParseColors(data, ncolors, cpp, colorTablePtr, hashtable) *curbuf = '\0'; /* init curbuf */ while ((l = xpmNextWord(data, buf, BUFSIZ))) { if (*curbuf != '\0') - strcat(curbuf, " ");/* append space */ + STRLCAT(curbuf, " ", sizeof(curbuf));/* append space */ buf[l] = '\0'; - strcat(curbuf, buf); /* append buf */ + STRLCAT(curbuf, buf, sizeof(curbuf)); /* append buf */ } - s = (char *) XpmMalloc(strlen(curbuf) + 1); + len = strlen(curbuf) + 1; + s = (char *) XpmMalloc(len); if (!s) { xpmFreeColorTable(colorTable, ncolors); return (XpmNoMemory); } - strcpy(s, curbuf); + memcpy(s, curbuf, len); color->c_color = s; *curbuf = '\0'; /* reset curbuf */ - if (a < ncolors - 1) + if (a < ncolors - 1) /* can we trust ncolors -> leave data's bounds */ xpmNextString(data); /* get to the next string */ } } @@ -338,9 +372,12 @@ ParsePixels(data, width, height, ncolors, cpp, colorTable, hashtable, pixels) xpmHashTable *hashtable; unsigned int **pixels; { - unsigned int *iptr, *iptr2; + unsigned int *iptr, *iptr2 = NULL; /* found by Egbert Eich */ unsigned int a, x, y; + if ((height > 0 && width >= UINT_MAX / height) || + width * height >= UINT_MAX / sizeof(unsigned int)) + return XpmNoMemory; #ifndef FOR_MSW iptr2 = (unsigned int *) XpmMalloc(sizeof(unsigned int) * width * height); #else @@ -364,6 +401,11 @@ ParsePixels(data, width, height, ncolors, cpp, colorTable, hashtable, pixels) { unsigned short colidx[256]; + if (ncolors > 256) { + XpmFree(iptr2); /* found by Egbert Eich */ + return (XpmFileInvalid); + } + bzero((char *)colidx, 256 * sizeof(short)); for (a = 0; a < ncolors; a++) colidx[(unsigned char)colorTable[a].string[0]] = a + 1; @@ -389,16 +431,20 @@ ParsePixels(data, width, height, ncolors, cpp, colorTable, hashtable, pixels) { /* free all allocated pointers at all exits */ -#define FREE_CIDX {int f; for (f = 0; f < 256; f++) \ -if (cidx[f]) XpmFree(cidx[f]);} +#define FREE_CIDX \ +do \ +{ \ + int f; for (f = 0; f < 256; f++) \ + if (cidx[f]) XpmFree(cidx[f]); \ +} while(0) /* array of pointers malloced by need */ unsigned short *cidx[256]; - int char1; + unsigned int char1; bzero((char *)cidx, 256 * sizeof(unsigned short *)); /* init */ for (a = 0; a < ncolors; a++) { - char1 = colorTable[a].string[0]; + char1 = (unsigned char) colorTable[a].string[0]; if (cidx[char1] == NULL) { /* get new memory */ cidx[char1] = (unsigned short *) XpmCalloc(256, sizeof(unsigned short)); @@ -442,6 +488,11 @@ if (cidx[f]) XpmFree(cidx[f]);} char *s; char buf[BUFSIZ]; + if (cpp >= sizeof(buf)) { + XpmFree(iptr2); /* found by Egbert Eich */ + return (XpmFileInvalid); + } + buf[cpp] = '\0'; if (USE_HASHTABLE) { xpmHashAtom *slot; @@ -450,7 +501,7 @@ if (cidx[f]) XpmFree(cidx[f]);} xpmNextString(data); for (x = 0; x < width; x++, iptr++) { for (a = 0, s = buf; a < cpp; a++, s++) - *s = xpmGetC(data); + *s = xpmGetC(data); /* int assigned to char, not a problem here */ slot = xpmHashSlot(hashtable, buf); if (!*slot) { /* no color matches */ XpmFree(iptr2); @@ -464,7 +515,7 @@ if (cidx[f]) XpmFree(cidx[f]);} xpmNextString(data); for (x = 0; x < width; x++, iptr++) { for (a = 0, s = buf; a < cpp; a++, s++) - *s = xpmGetC(data); + *s = xpmGetC(data); /* int assigned to char, not a problem here */ for (a = 0; a < ncolors; a++) if (!strcmp(colorTable[a].string, buf)) break; @@ -519,7 +570,7 @@ xpmParseExtensions(data, extensions, nextensions) while (!notstart && notend) { /* there starts an extension */ ext = (XpmExtension *) - XpmRealloc(exts, (num + 1) * sizeof(XpmExtension)); + XpmRealloc(exts, (num + 1) * sizeof(XpmExtension)); /* can the loop be forced to iterate often enough to make "(num + 1) * sizeof(XpmExtension)" wrapping? */ if (!ext) { XpmFree(string); XpmFreeExtensions(exts, num); @@ -556,7 +607,7 @@ xpmParseExtensions(data, extensions, nextensions) while ((notstart = strncmp("XPMEXT", string, 6)) && (notend = strncmp("XPMENDEXT", string, 9))) { sp = (char **) - XpmRealloc(ext->lines, (nlines + 1) * sizeof(char *)); + XpmRealloc(ext->lines, (nlines + 1) * sizeof(char *)); /* can we iterate enough for a wrapping? */ if (!sp) { XpmFree(string); ext->nlines = nlines; @@ -596,9 +647,9 @@ xpmParseExtensions(data, extensions, nextensions) /* function call in case of error */ #undef RETURN #define RETURN(status) \ -{ \ +do { \ goto error; \ -} +} while(0) /* * This function parses an Xpm file or data and store the found informations |