rem: envVarLen = strlen (envVar); CHECKLEN (buf, envVarLen); memcpy (bufptr, envVar, envVarLen); bufptr += envVarLen;
add: if (envVar != NULL) { envVarLen = strlen (envVar); CHECKLEN (buf, envVarLen); memcpy (bufptr, envVar, envVarLen); bufptr += envVarLen; }
context: expandPath (char *dest, size_t len, const char *src, int what){	char *destptr, *destend;	char *buf, *bufptr, *bufend;	const char *srcend;#define CHECKLEN(bufname, n) \		if (bufname##ptr + (n) >= bufname##end) \		{ \			errno = ENAMETOOLONG; \			return -1; \		} \		else \			(void) 0	destptr = dest;	destend = dest + len;	if (what & EP_ENVVARS)	{		buf = alloca (len);		bufptr = buf;		bufend = buf + len;		while (*src != '\0')		{			switch (*src)			{#ifdef WIN32				case '%':				{					/* Environment variable substitution in Windows */					const char *end;     // end of env var name in src					const char *envVar;					char *envName;					size_t envNameLen, envVarLen;										src++;					end = strchr (src, '%');					if (end == NULL)					{						errno = EINVAL;						return -1;					}										envNameLen = end - src;					envName = HMalloc (envNameLen + 1);					memcpy (envName, src, envNameLen + 1);					envName[envNameLen] = '\0';					envVar = getenv (envName);					HFree (envName);					if (envVar == NULL)					{#ifdef APPDATA_FALLBACK						if (strncmp (src, "APPDATA", envNameLen) != 0)						{							// Substitute an empty string							src = end + 1;							break;						}						// fallback for when the APPDATA env var is not set						// Using SHGetFolderPath or SHGetSpecialFolderPath						// is problematic (not everywhere available).						fprintf(stderr, "Warning: %%APPDATA%% is not set. "								"Falling back to \"%%USERPROFILE%%\\Application "								"Data\"\n");						envVar = getenv ("USERPROFILE");						if (envVar != NULL)						{#define APPDATA_STRING "\\Application Data"							envVarLen = strlen (envVar);							CHECKLEN (buf,									envVarLen + sizeof (APPDATA_STRING) - 1);							strcpy (bufptr, envVar);							bufptr += envVarLen;							strcpy (bufptr, APPDATA_STRING);							bufptr += sizeof (APPDATA_STRING) - 1;							src = end + 1;							break;						}												// fallback to "../userdata"						fprintf(stderr, "Warning: %%USERPROFILE%% is not set. "								"Falling back to \"..\\userdata\" for %%APPDATA%%"								"\n");#define APPDATA_FALLBACK_STRING "..\\userdata"						CHECKLEN (buf, sizeof (APPDATA_FALLBACK_STRING) - 1);						strcpy (bufptr, APPDATA_FALLBACK_STRING);						bufptr += sizeof (APPDATA_FALLBACK_STRING) - 1;						src = end + 1;						break;#else  /* !defined (APPDATA_FALLBACK) */						// Substitute an empty string						src = end + 1;						break;#endif  /* APPDATA_FALLBACK */					}					envVarLen = strlen (envVar);					CHECKLEN (buf, envVarLen);					strcpy (bufptr, envVar);					bufptr += envVarLen;					src = end + 1;					break;				}#endif#ifndef WIN32				case '$':				{					const char *end;					char *envName;					size_t envNameLen;					const char *envVar;					size_t envVarLen;					src++;					if (*src == '{')					{						src++;						end = strchr(src, '}');						if (end == NULL)						{							errno = EINVAL;							return -1;						}						envNameLen = end - src;						end++;  // Skip the '}'					}					else					{						end = src;						while ((*end >= 'A' && *end <= 'Z') ||								(*end >= 'a' && *end <= 'z') ||								(*end >= '0' && *end <= '9') ||								*end == '_')							end++;						envNameLen = end - src;					}					envName = HMalloc (envNameLen + 1);					memcpy (envName, src, envNameLen + 1);					envName[envNameLen] = '\0';					envVar = getenv (envName);					HFree (envName);					envVarLen = strlen (envVar);					CHECKLEN (buf, envVarLen);					memcpy (bufptr, envVar, envVarLen);					bufptr += envVarLen;					src = end;					break;				}#endif				default:					CHECKLEN(buf, 1);					*(bufptr++) = *(src++);					break;			}  // switch		}  // while		*bufptr = '\0';		src = buf;		srcend = bufptr;	}  // if (what & EP_ENVVARS)	else		srcend = src + strlen (src);		if (what & EP_HOME)	{		if (src[0] == '~')		{			const char *home;			size_t homelen;						if (src[1] != '/')			{				errno = EINVAL;				return -1;			}			home = getHomeDir ();			if (home == NULL)			{				errno = ENOENT;				return -1;			}			homelen = strlen (home);					if (what & EP_ABSOLUTE) {				destptr = expandPathAbsolute (dest, destend - dest,						home, what);				if (destptr == NULL)				{					// errno is set					return -1;				}				what &= ~EP_ABSOLUTE;						// The part after the '~' should not be seen						// as absolute.			}			CHECKLEN (dest, homelen);			memcpy (destptr, home, homelen);			destptr += homelen;			src++;  /* skip the ~ */		}	}		if (what & EP_ABSOLUTE)	{		destptr = expandPathAbsolute (destptr, destend - destptr, src,				what);		if (destptr == NULL)		{			// errno is set			return -1;		}	}	CHECKLEN (dest, srcend - src);	memcpy (destptr, src, srcend - src + 1);			// The +1 is for the '\0'. It is already taken into account by			// CHECKLEN.		if (what & EP_SLASHES)	{		/* Replacing backslashes in path by slashes. */		destptr = dest;		while (*destptr != '\0')		{			if (*destptr == '\\')				*destptr = '/';			destptr++;		}	}	return 0;}