diff options
author | Matthieu Herrb <matthieu@cvs.openbsd.org> | 2013-05-31 14:39:09 +0000 |
---|---|---|
committer | Matthieu Herrb <matthieu@cvs.openbsd.org> | 2013-05-31 14:39:09 +0000 |
commit | 1d548f50e204758d38783f7cae7a52a4507e0abd (patch) | |
tree | 61d79cffeac4ec8911e39c0493b08317b54950f3 /lib/libFS/src/FSFontInfo.c | |
parent | 67a54ddd6653b8c8756f825881ee8253b2e836b9 (diff) |
update to libFS 1.0.5
Diffstat (limited to 'lib/libFS/src/FSFontInfo.c')
-rw-r--r-- | lib/libFS/src/FSFontInfo.c | 235 |
1 files changed, 76 insertions, 159 deletions
diff --git a/lib/libFS/src/FSFontInfo.c b/lib/libFS/src/FSFontInfo.c index d9c84b68f..b51e04376 100644 --- a/lib/libFS/src/FSFontInfo.c +++ b/lib/libFS/src/FSFontInfo.c @@ -68,16 +68,17 @@ FSListFontsWithXInfo( int i, j; size_t size = 0; - FSXFontInfoHeader **fhdr = (FSXFontInfoHeader **) 0; - FSPropInfo **pi = (FSPropInfo **) 0; - FSPropOffset **po = (FSPropOffset **) 0; - unsigned char **pd = (unsigned char **) 0; + FSXFontInfoHeader **fhdr = (FSXFontInfoHeader **) NULL; + FSPropInfo **pi = (FSPropInfo **) NULL; + FSPropOffset **po = (FSPropOffset **) NULL; + unsigned char **pd = (unsigned char **) NULL; char **flist = NULL; fsListFontsWithXInfoReply reply; fsListFontsWithXInfoReq *req; fsPropInfo local_pi; fsPropOffset local_po; Status status; + Bool eat_data = True; GetReq(ListFontsWithXInfo, req); req->maxNames = maxNames; @@ -101,26 +102,8 @@ FSListFontsWithXInfo( SIZEOF(fsGenericReply)) >> 2), fsFalse); } if (!status) { - for (j = (i - 1); j >= 0; j--) { - FSfree((char *) fhdr[j]); - FSfree((char *) pi[j]); - FSfree((char *) po[j]); - FSfree((char *) pd[j]); - FSfree(flist[j]); - } - if (flist) - FSfree((char *) flist); - if (fhdr) - FSfree((char *) fhdr); - if (pi) - FSfree((char *) pi); - if (po) - FSfree((char *) po); - if (pd) - FSfree((char *) pd); - - SyncHandle(); - return (char **) NULL; + eat_data = False; + goto badmem; } if (reply.nameLength == 0) /* got last reply in version 1 */ break; @@ -134,136 +117,67 @@ FSListFontsWithXInfo( goto badmem; if (fhdr) { - FSXFontInfoHeader **tmp_fhdr = (FSXFontInfoHeader **) - FSrealloc((char *) fhdr, - (unsigned) (sizeof(FSXFontInfoHeader *) * size)); - char **tmp_flist = (char **) FSrealloc((char *) flist, - (unsigned) (sizeof(char *) * size)); - FSPropInfo **tmp_pi = (FSPropInfo **) - FSrealloc((char *) pi, - (unsigned) (sizeof(FSPropInfo *) * size)); - FSPropOffset **tmp_po = (FSPropOffset **) - FSrealloc((char *) po, - (unsigned) (sizeof(FSPropOffset *) * size)); - unsigned char **tmp_pd = (unsigned char **) - FSrealloc((char *) pd, - (unsigned) (sizeof(unsigned char *) * size)); - - if (!tmp_fhdr || !tmp_flist || !tmp_pi || !tmp_po || !tmp_pd) { - for (j = (i - 1); j >= 0; j--) { - FSfree((char *) flist[j]); - FSfree((char *) fhdr[j]); - FSfree((char *) pi[j]); - FSfree((char *) po[j]); - FSfree((char *) pd[j]); - } - if (tmp_flist) - FSfree((char *) tmp_flist); - else - FSfree((char *) flist); - if (tmp_fhdr) - FSfree((char *) tmp_fhdr); - else - FSfree((char *) fhdr); - if (tmp_pi) - FSfree((char *) tmp_pi); - else - FSfree((char *) pi); - if (tmp_po) - FSfree((char *) tmp_po); - else - FSfree((char *) po); - if (tmp_pd) - FSfree((char *) tmp_pd); - else - FSfree((char *) pd); - goto clearwire; +#define ResizeArray(var, type) { \ + type **tmp = FSrealloc(var, sizeof(type *) * size); \ + if (tmp) \ + var = tmp; \ + else \ + goto badmem; \ } - fhdr = tmp_fhdr; - flist = tmp_flist; - pi = tmp_pi; - po = tmp_po; - pd = tmp_pd; + + ResizeArray(fhdr, FSXFontInfoHeader) + ResizeArray(flist, char) + ResizeArray(pi, FSPropInfo) + ResizeArray(po, FSPropOffset) + ResizeArray(pd, unsigned char) } else { - if (!(fhdr = (FSXFontInfoHeader **) - FSmalloc((unsigned) (sizeof(FSXFontInfoHeader *) * size)))) - goto clearwire; - if (!(flist = (char **) - FSmalloc((unsigned) (sizeof(char *) * size)))) { - FSfree((char *) fhdr); - goto clearwire; - } - if (!(pi = (FSPropInfo **) - FSmalloc((unsigned) (sizeof(FSPropInfo *) * size)))) { - FSfree((char *) fhdr); - FSfree((char *) flist); - goto clearwire; - } - if (!(po = (FSPropOffset **) - FSmalloc((unsigned) (sizeof(FSPropOffset *) * size)))) { - FSfree((char *) fhdr); - FSfree((char *) flist); - FSfree((char *) pi); - goto clearwire; - } - if (!(pd = (unsigned char **) - FSmalloc((unsigned) (sizeof(unsigned char *) * size)))) { - FSfree((char *) fhdr); - FSfree((char *) flist); - FSfree((char *) pi); - FSfree((char *) po); - goto clearwire; +#define InitArray(var, type) \ + if ((var = FSmalloc(sizeof(type *) * size)) == NULL) { \ + goto badmem; \ } + + InitArray(fhdr, FSXFontInfoHeader) + InitArray(flist, char) + InitArray(pi, FSPropInfo) + InitArray(po, FSPropOffset) + InitArray(pd, unsigned char) } } - fhdr[i] = (FSXFontInfoHeader *) FSmalloc(sizeof(FSXFontInfoHeader)); + fhdr[i] = FSmalloc(sizeof(FSXFontInfoHeader)); if (!fhdr[i]) { goto badmem; } FSUnpack_XFontInfoHeader(&reply, fhdr[i], FSProtocolVersion(svr)); /* alloc space for the name */ - flist[i] = (char *) FSmalloc((unsigned int) (reply.nameLength + 1)); + flist[i] = FSmalloc(reply.nameLength + 1); + if (!flist[i]) + goto cleanfhdr; if (FSProtocolVersion(svr) == 1) { /* get the name */ - if (!flist[i]) { - nbytes = (reply.nameLength + 3) & ~3; - _FSEatData(svr, (unsigned long) nbytes); - goto badmem; - } _FSReadPad(svr, flist[i], (long) reply.nameLength); flist[i][reply.nameLength] = '\0'; } - pi[i] = (FSPropInfo *) FSmalloc(sizeof(FSPropInfo)); - if (!pi[i]) { - FSfree((char *) fhdr[i]); - goto badmem; - } + pi[i] = FSmalloc(sizeof(FSPropInfo)); + if (!pi[i]) + goto cleanflist; _FSReadPad(svr, (char *) &local_pi, SIZEOF(fsPropInfo)); pi[i]->num_offsets = local_pi.num_offsets; pi[i]->data_len = local_pi.data_len; #if SIZE_MAX <= UINT_MAX if (pi[i]->num_offsets > SIZE_MAX / sizeof(FSPropOffset)) - goto badmem; + goto cleanpi; #endif - po[i] = (FSPropOffset *) - FSmalloc(pi[i]->num_offsets * sizeof(FSPropOffset)); - if (!po[i]) { - FSfree((char *) fhdr[i]); - FSfree((char *) pi[i]); - goto badmem; - } - pd[i] = (unsigned char *) FSmalloc(pi[i]->data_len); - if (!pd[i]) { - FSfree((char *) fhdr[i]); - FSfree((char *) pi[i]); - FSfree((char *) po[i]); - goto badmem; - } + po[i] = FSmalloc(pi[i]->num_offsets * sizeof(FSPropOffset)); + if (!po[i]) + goto cleanpi; + pd[i] = FSmalloc(pi[i]->data_len); + if (!pd[i]) + goto cleanpo; /* get offsets */ for (j=0; j<pi[i]->num_offsets; j++) { @@ -284,11 +198,6 @@ FSListFontsWithXInfo( if (FSProtocolVersion(svr) != 1) { /* get the name */ - if (!flist[i]) { - nbytes = (reply.nameLength + 3) & ~3; - _FSEatData(svr, (unsigned long) nbytes); - goto badmem; - } _FSRead(svr, flist[i], (long) reply.nameLength); flist[i][reply.nameLength] = '\0'; @@ -297,7 +206,7 @@ FSListFontsWithXInfo( } /* avoid integer overflow */ if (i > INT_MAX - 1) { - goto badmem; + goto cleanpd; } } *info = fhdr; @@ -308,37 +217,45 @@ FSListFontsWithXInfo( SyncHandle(); return flist; +/* Error cleanup for when we're partway through filling in item #i in arrays */ +cleanpd: + FSfree(pd[i]); +cleanpo: + FSfree(po[i]); +cleanpi: + FSfree(pi[i]); +cleanflist: + FSfree(flist[i]); +cleanfhdr: + FSfree(fhdr[i]); +/* Error cleanup for all previously filled in items in the arrays */ badmem: for (j = (i - 1); j >= 0; j--) { - FSfree((char *) pi[j]); - FSfree((char *) po[j]); - FSfree((char *) pd[j]); + FSfree(pi[j]); + FSfree(po[j]); + FSfree(pd[j]); FSfree(flist[j]); - FSfree((char *) fhdr[j]); + FSfree(fhdr[j]); } - if (flist) - FSfree((char *) flist); - if (fhdr) - FSfree((char *) fhdr); - if (pi) - FSfree((char *) pi); - if (po) - FSfree((char *) po); - if (pd) - FSfree((char *) pd); + FSfree(flist); + FSfree(fhdr); + FSfree(pi); + FSfree(po); + FSfree(pd); + if (eat_data) { + do { + fsPropInfo ti; -clearwire: - do { - fsPropInfo ti; - - _FSEatData(svr, (reply.nameLength + 3) & ~3); - _FSReadPad(svr, (char *) &ti, SIZEOF(fsPropInfo)); - _FSEatData(svr, (SIZEOF(fsPropOffset) * ti.num_offsets)); - _FSEatData(svr, ti.data_len); - } while (_FSReply(svr, (fsReply *) & reply, - ((SIZEOF(fsListFontsWithXInfoReply) - - SIZEOF(fsGenericReply)) >> 2), fsFalse) && (reply.nameLength != 0)); + _FSEatData(svr, (reply.nameLength + 3) & ~3); + _FSReadPad(svr, (char *) &ti, SIZEOF(fsPropInfo)); + _FSEatData(svr, (SIZEOF(fsPropOffset) * ti.num_offsets)); + _FSEatData(svr, ti.data_len); + } while (_FSReply(svr, (fsReply *) &reply, + ((SIZEOF(fsListFontsWithXInfoReply) + - SIZEOF(fsGenericReply)) >> 2), fsFalse) + && (reply.nameLength != 0)); + } SyncHandle(); return (char **) NULL; } |