Index: libfdproto/messages.c
===================================================================
--- libfdproto/messages.c	(revision 1248)
+++ libfdproto/messages.c	(revision 1298)
@@ -232,5 +232,5 @@
 		struct dict_avp_data dictdata;
 		
-		CHECK_FCT(  fd_dict_getval(model, &dictdata)  );
+		CHECK_FCT_DO(  fd_dict_getval(model, &dictdata), { free(new); return __ret__; }  );
 	
 		new->avp_model = model;
@@ -248,5 +248,5 @@
 		new->avp_rawlen = (*avp)->avp_public.avp_len - GETAVPHDRSZ( (*avp)->avp_public.avp_flags );
 		if (new->avp_rawlen) {
-			CHECK_MALLOC(  new->avp_rawdata = malloc(new->avp_rawlen)  );
+			CHECK_MALLOC_DO(  new->avp_rawdata = malloc(new->avp_rawlen), { free(new); return __ret__; }  );
 			memset(new->avp_rawdata, 0x00, new->avp_rawlen);
 		}
@@ -286,6 +286,6 @@
 		struct dict_object     	*dictappl;
 		
-		CHECK_FCT( fd_dict_getdict(model, &dict) );
-		CHECK_FCT( fd_dict_getval(model, &dictdata)  );
+		CHECK_FCT_DO( fd_dict_getdict(model, &dict), { free(new); return __ret__; } );
+		CHECK_FCT_DO( fd_dict_getval(model, &dictdata), { free(new); return __ret__; }  );
 		
 		new->msg_model = model;
@@ -294,8 +294,8 @@
 
 		/* Initialize application from the parent, if any */
-		CHECK_FCT(  fd_dict_search( dict, DICT_APPLICATION, APPLICATION_OF_COMMAND, model, &dictappl, 0)  );
+		CHECK_FCT_DO(  fd_dict_search( dict, DICT_APPLICATION, APPLICATION_OF_COMMAND, model, &dictappl, 0), { free(new); return __ret__; }  );
 		if (dictappl != NULL) {
 			struct dict_application_data appdata;
-			CHECK_FCT(  fd_dict_getval(dictappl, &appdata)  );
+			CHECK_FCT_DO(  fd_dict_getval(dictappl, &appdata), { free(new); return __ret__; }  );
 			new->msg_public.msg_appl = appdata.application_id;
 		}
@@ -365,14 +365,14 @@
 		
 		if (!sess_id_avp) {
-			CHECK_FCT( fd_dict_search( dict, DICT_AVP, AVP_BY_NAME, "Session-Id", &sess_id_avp, ENOENT) );
-		}
-		CHECK_FCT( fd_sess_getsid ( sess, &sid, &sidlen ) );
-		CHECK_FCT( fd_msg_avp_new ( sess_id_avp, 0, &avp ) );
+			CHECK_FCT_DO( fd_dict_search( dict, DICT_AVP, AVP_BY_NAME, "Session-Id", &sess_id_avp, ENOENT), { free(ans); return __ret__; } );
+		}
+		CHECK_FCT_DO( fd_sess_getsid ( sess, &sid, &sidlen ), { free(ans); return __ret__; } );
+		CHECK_FCT_DO( fd_msg_avp_new ( sess_id_avp, 0, &avp ), { free(ans); return __ret__; } );
 		val.os.data = sid;
 		val.os.len  = sidlen;
-		CHECK_FCT( fd_msg_avp_setvalue( avp, &val ) );
-		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_FIRST_CHILD, avp ) );
+		CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), { free(avp); free(ans); return __ret__; } );
+		CHECK_FCT_DO( fd_msg_avp_add( ans, MSG_BRW_FIRST_CHILD, avp ), { free(avp); free(ans); return __ret__; } );
 		ans->msg_sess = sess;
-		CHECK_FCT( fd_sess_ref_msg(sess) );
+		CHECK_FCT_DO( fd_sess_ref_msg(sess), { free(ans); return __ret__; }  );
 	}
 	
@@ -383,5 +383,5 @@
 		struct fd_list avpcpylist = FD_LIST_INITIALIZER(avpcpylist);
 		
-		CHECK_FCT(  fd_msg_browse(qry, MSG_BRW_FIRST_CHILD, &avp, NULL)  );
+		CHECK_FCT_DO(  fd_msg_browse(qry, MSG_BRW_FIRST_CHILD, &avp, NULL) , { free(ans); return __ret__; } );
 		while (avp) {
 			if ( (avp->avp_public.avp_code   == AC_PROXY_INFO)
@@ -394,13 +394,13 @@
 
 				/* Create a buffer with the content of the AVP. This is easier than going through the list */
-				CHECK_FCT(  fd_msg_update_length(avp)  );
-				CHECK_MALLOC(  buf = malloc(avp->avp_public.avp_len)  );
-				CHECK_FCT( bufferize_avp(buf, avp->avp_public.avp_len, &offset, avp)  );
+				CHECK_FCT_DO(  fd_msg_update_length(avp), { free(ans); return __ret__; }  );
+				CHECK_MALLOC_DO(  buf = malloc(avp->avp_public.avp_len), { free(ans); return __ret__; }  );
+				CHECK_FCT_DO( bufferize_avp(buf, avp->avp_public.avp_len, &offset, avp), { free(buf); free(ans); return __ret__; }  );
 
 				/* Now we parse this buffer to create a copy AVP */
-				CHECK_FCT( parsebuf_list(buf, avp->avp_public.avp_len, &avpcpylist) );
+				CHECK_FCT_DO( parsebuf_list(buf, avp->avp_public.avp_len, &avpcpylist), { free(buf); free(ans); return __ret__; } );
 				
 				/* Parse dictionary objects now to remove the dependency on the buffer */
-				CHECK_FCT( parsedict_do_chain(dict, &avpcpylist, 0, &pei) );
+				CHECK_FCT_DO( parsedict_do_chain(dict, &avpcpylist, 0, &pei), { /* leaking the avpcpylist -- this should never happen anyway */ free(buf); free(ans); return __ret__; } );
 
 				/* Done for this AVP */
@@ -411,5 +411,5 @@
 			}
 			/* move to next AVP in the message, we can have several Proxy-Info instances */
-			CHECK_FCT( fd_msg_browse(avp, MSG_BRW_NEXT, &avp, NULL) );
+			CHECK_FCT_DO( fd_msg_browse(avp, MSG_BRW_NEXT, &avp, NULL), { free(ans); return __ret__; } );
 		}
 	}
