msgb: Check for available headroom/tailroom and abort() if we violate it
diff --git a/include/osmocore/msgb.h b/include/osmocore/msgb.h
index 2841dc5..fb4a701 100644
--- a/include/osmocore/msgb.h
+++ b/include/osmocore/msgb.h
@@ -23,6 +23,8 @@
 #include <stdint.h>
 #include "linuxlist.h"
 
+#define MSGB_DEBUG
+
 struct msgb {
 	struct llist_head list;
 
@@ -58,6 +60,16 @@
 extern struct msgb *msgb_dequeue(struct llist_head *queue);
 extern void msgb_reset(struct msgb *m);
 
+#ifdef MSGB_DEBUG
+#include <stdio.h>
+#include <stdlib.h>
+static inline void msgb_abort(struct msgb *msg, const char *text)
+{
+	fprintf(stderr, text);
+	abort();
+}
+#endif
+
 #define msgb_l1(m)	((void *)(m->l1h))
 #define msgb_l2(m)	((void *)(m->l2h))
 #define msgb_l3(m)	((void *)(m->l3h))
@@ -82,9 +94,24 @@
 {
 	return msgb->len - msgb->data_len;
 }
+
+static inline int msgb_tailroom(const struct msgb *msgb)
+{
+	return (msgb->head + msgb->data_len) - msgb->tail;
+}
+
+static inline int msgb_headroom(const struct msgb *msgb)
+{
+	return (msgb->data - msgb->head);
+}
+
 static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
 {
 	unsigned char *tmp = msgb->tail;
+#ifdef MSGB_DEBUG
+	if (msgb_tailroom(msgb) < len)
+		msgb_abort(msgb, "Not enough tailroom\n");
+#endif
 	msgb->tail += len;
 	msgb->len += len;
 	return tmp;
@@ -132,6 +159,10 @@
 }
 static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
 {
+#ifdef MSGB_DEBUG
+	if (msgb_headroom(msgb) < len)
+		msgb_abort(msgb, "Not enough headroom\n");
+#endif
 	msgb->data -= len;
 	msgb->len += len;
 	return msgb->data;
@@ -141,10 +172,6 @@
 	msgb->len -= len;
 	return msgb->data += len;
 }
-static inline int msgb_tailroom(const struct msgb *msgb)
-{
-	return (msgb->head + msgb->data_len) - msgb->tail;
-}
 
 /* increase the headroom of an empty msgb, reducing the tailroom */
 static inline void msgb_reserve(struct msgb *msg, int len)