From patchwork Fri Nov 27 12:26:15 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Erlbeck X-Patchwork-Id: 549429 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.osmocom.org (tmp.osmocom.org [144.76.43.76]) by ozlabs.org (Postfix) with ESMTP id 9A78514031B for ; Fri, 27 Nov 2015 23:27:05 +1100 (AEDT) Received: from lists.osmocom.org (lists.osmocom.org [144.76.43.76]) by lists.osmocom.org (Postfix) with ESMTP id 66B5EA2CE; Fri, 27 Nov 2015 12:27:04 +0000 (UTC) X-Original-To: openbsc@lists.osmocom.org Delivered-To: openbsc@lists.osmocom.org Received: from mail.sysmocom.de (mail.sysmocom.de [IPv6:2a01:4f8:191:444c::2:4]) by lists.osmocom.org (Postfix) with ESMTP id 1E7A6A267 for ; Fri, 27 Nov 2015 12:27:01 +0000 (UTC) Received: from sysmocom-tmp.am93.sysmocom.de (ip5b4185b8.dynamic.kabel-deutschland.de [91.65.133.184]) by mail.sysmocom.de (Postfix) with ESMTPSA id E84DA1D6A92; Fri, 27 Nov 2015 12:27:00 +0000 (UTC) From: Jacob Erlbeck To: openbsc@lists.osmocom.org Subject: [PATCH 3/8] msgb: Add msgb_test_invariant function Date: Fri, 27 Nov 2015 13:26:15 +0100 Message-Id: <1448627180-10603-4-git-send-email-jerlbeck@sysmocom.de> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1448627180-10603-1-git-send-email-jerlbeck@sysmocom.de> References: <1447753069-17466-1-git-send-email-jerlbeck@sysmocom.de> <1448627180-10603-1-git-send-email-jerlbeck@sysmocom.de> X-BeenThere: openbsc@lists.osmocom.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Development of the OpenBSC GSM base station controller List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openbsc-bounces@lists.osmocom.org Sender: "OpenBSC" This adds a function that verifies whether a mgsb is consistent. Sponsored-by: On-Waves ehf --- include/osmocom/core/msgb.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h index 9ffc64e..9c99cad 100644 --- a/include/osmocom/core/msgb.h +++ b/include/osmocom/core/msgb.h @@ -77,6 +77,7 @@ extern const char *msgb_hexdump(const struct msgb *msg); extern int msgb_resize_area(struct msgb *msg, uint8_t *area, int old_size, int new_size); extern struct msgb *msgb_copy(const struct msgb *msg, const char *name); +static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure)); #ifdef MSGB_DEBUG #include @@ -412,6 +413,45 @@ static inline struct msgb *msgb_alloc_headroom(int size, int headroom, return msg; } +/*! \brief Check a message buffer for consistency + * \param[in] msg message buffer + * \returns 0 (false) if inconsistent, != 0 (true) otherwise + */ +static inline int msgb_test_invariant(const struct msgb *msg) +{ + const unsigned char *lbound; + if (!msg || !msg->data || !msg->tail || + (msg->data + msg->len != msg->tail) || + (msg->data < msg->head) || + (msg->tail > msg->head + msg->data_len)) + return 0; + + lbound = msg->head; + + if (msg->l1h) { + if (msg->l1h < lbound) + return 0; + lbound = msg->l1h; + } + if (msg->l2h) { + if (msg->l2h < lbound) + return 0; + lbound = msg->l2h; + } + if (msg->l3h) { + if (msg->l3h < lbound) + return 0; + lbound = msg->l3h; + } + if (msg->l4h) { + if (msg->l4h < lbound) + return 0; + lbound = msg->l4h; + } + + return lbound <= msg->head + msg->data_len; +} + /* non inline functions to ease binding */ uint8_t *msgb_data(const struct msgb *msg);