ptr_size = used_size + ext_size + cmt_size + 1;
ptr_size = used_size + ext_size + cmt_size + 1;  if(ptr_size <= used_size || ptr_size <= ext_size  || ptr_size <= cmt_size) { return XpmNoMemory; }
XpmCreateBufferFromXpmImage(buffer_return, image, info)    char **buffer_return;    XpmImage *image;    XpmInfo *info;{    /* calculation variables */    int ErrorStatus;    char buf[BUFSIZ];    unsigned int cmts, extensions, ext_size = 0;    unsigned int l, cmt_size = 0;    char *ptr = NULL, *p;    unsigned int ptr_size, used_size;    *buffer_return = NULL;    cmts = info && (info->valuemask & XpmComments);    extensions = info && (info->valuemask & XpmExtensions)	&& info->nextensions;    /* compute the extensions and comments size */    if (extensions)	ext_size = ExtensionsSize(info->extensions, info->nextensions);    if (cmts)	cmt_size = CommentsSize(info);    /* write the header line */#ifndef VOID_SPRINTF    used_size =#endif    sprintf(buf, "/* XPM */\nstatic char * image_name[] = {\n");#ifdef VOID_SPRINTF    used_size = strlen(buf);#endif    ptr_size = used_size + ext_size + cmt_size + 1;    ptr = (char *) XpmMalloc(ptr_size);    if (!ptr)	return XpmNoMemory;    strcpy(ptr, buf);    /* write the values line */    if (cmts && info->hints_cmt) {#ifndef VOID_SPRINTF	used_size +=#endif	sprintf(ptr + used_size, "/*%s*/\n", info->hints_cmt);#ifdef VOID_SPRINTF	used_size += strlen(info->hints_cmt) + 5;#endif    }#ifndef VOID_SPRINTF    l =#endif    sprintf(buf, "\"%d %d %d %d", image->width, image->height,	    image->ncolors, image->cpp);#ifdef VOID_SPRINTF    l = strlen(buf);#endif    if (info && (info->valuemask & XpmHotspot)) {#ifndef VOID_SPRINTF	l +=#endif	sprintf(buf + l, " %d %d", info->x_hotspot, info->y_hotspot);#ifdef VOID_SPRINTF	l = strlen(buf);#endif    }    if (extensions) {#ifndef VOID_SPRINTF	l +=#endif	sprintf(buf + l, " XPMEXT");#ifdef VOID_SPRINTF	l = strlen(buf);#endif    }#ifndef VOID_SPRINTF    l +=#endif    sprintf(buf + l, "\",\n");#ifdef VOID_SPRINTF    l = strlen(buf);#endif    ptr_size += l;    p = (char *) XpmRealloc(ptr, ptr_size);    if (!p)	RETURN(XpmNoMemory);    ptr = p;    strcpy(ptr + used_size, buf);    used_size += l;    /* write colors */    if (cmts && info->colors_cmt) {#ifndef VOID_SPRINTF	used_size +=#endif	sprintf(ptr + used_size, "/*%s*/\n", info->colors_cmt);#ifdef VOID_SPRINTF	used_size += strlen(info->colors_cmt) + 5;#endif    }    ErrorStatus = WriteColors(&ptr, &ptr_size, &used_size,			      image->colorTable, image->ncolors, image->cpp);     if (ErrorStatus != XpmSuccess)	RETURN(ErrorStatus);    /*     * now we know the exact size we need, realloc the data     * 4 = 1 (for '"') + 3 (for '",\n')     * 1 = - 2 (because the last line does not end with ',\n') + 3 (for '};\n')     */    ptr_size += image->height * (image->width * image->cpp + 4) + 1;    p = (char *) XpmRealloc(ptr, ptr_size);    if (!p)	RETURN(XpmNoMemory);    ptr = p;    /* print pixels */    if (cmts && info->pixels_cmt) {#ifndef VOID_SPRINTF	used_size +=#endif	sprintf(ptr + used_size, "/*%s*/\n", info->pixels_cmt);#ifdef VOID_SPRINTF	used_size += strlen(info->pixels_cmt) + 5;#endif    }    WritePixels(ptr + used_size, &used_size, image->width, image->height,		image->cpp, image->data, image->colorTable);    /* print extensions */    if (extensions)	WriteExtensions(ptr + used_size, &used_size,			info->extensions, info->nextensions);    /* close the array */    strcpy(ptr + used_size, "};\n");    *buffer_return = ptr;    return (XpmSuccess);/* exit point in case of error, free only locally allocated variables */error:    if (ptr)	XpmFree(ptr);    return (ErrorStatus);}