diff --git a/src/xpdev/xpprintf.c b/src/xpdev/xpprintf.c
index 9459affd2fd681f8f05d64613b3cde9ffea70492..7ed05eb06a5348652771a04add0e8fa7b2adb668 100644
--- a/src/xpdev/xpprintf.c
+++ b/src/xpdev/xpprintf.c
@@ -1090,8 +1090,11 @@ char *xp_asprintf_next(char *format, int type, ...)
 	 * the string.
 	 */
 	newbuf=(char *)realloc(format, format_len-this_format_len+j+1);
-	if(newbuf==NULL)
+	if(newbuf==NULL) {
+		if(entry != entry_buf)
+			free(entry);
 		return(NULL);
+	}
 	format=newbuf;
 	/* Move trailing end to make space */
 	memmove(format+offset+j, format+offset+this_format_len, offset+format_len-this_format_len+1);