From patchwork Fri Nov 27 12:26:14 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Erlbeck X-Patchwork-Id: 549428 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.osmocom.org (unknown [IPv6:2a01:4f8:191:444b::2:7]) by ozlabs.org (Postfix) with ESMTP id 594A11402D4 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 90737A290; Fri, 27 Nov 2015 12:27:02 +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 F2590A265 for ; Fri, 27 Nov 2015 12:27:00 +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 BF9121D6A8D; Fri, 27 Nov 2015 12:27:00 +0000 (UTC) From: Jacob Erlbeck To: openbsc@lists.osmocom.org Subject: [PATCH 2/8] msgb: Let msgb_hexdump be more tolerant Date: Fri, 27 Nov 2015 13:26:14 +0100 Message-Id: <1448627180-10603-3-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 patch makes msgb_hexdump accept out of range lXh pointers and shows info about them instead of aborting the dump entirely. Sponsored-by: On-Waves ehf --- src/msgb.c | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/msgb.c b/src/msgb.c index 3132644..4b108a4 100644 --- a/src/msgb.c +++ b/src/msgb.c @@ -266,10 +266,25 @@ const char *msgb_hexdump(const struct msgb *msg) if (!lxhs[i]) continue; - if (lxhs[i] < msg->data) - goto out_of_range; + if (lxhs[i] < msg->head) + continue; + if (lxhs[i] > msg->head + msg->data_len) + continue; if (lxhs[i] > msg->tail) - goto out_of_range; + continue; + if (lxhs[i] < msg->data || lxhs[i] > msg->tail) { + nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, + "(L%d=data%+d) ", + i+1, lxhs[i] - msg->data); + buf_offs += nchars; + continue; + } + if (lxhs[i] < start) { + nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, + "(L%d%+d) ", i+1, start - lxhs[i]); + buf_offs += nchars; + continue; + } nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, "%s[L%d]> ", osmo_hexdump(start, lxhs[i] - start), @@ -285,11 +300,28 @@ const char *msgb_hexdump(const struct msgb *msg) if (nchars < 0 || nchars + buf_offs >= sizeof(buf)) return "ERROR"; - return buf; + buf_offs += nchars; + + for (i = 0; i < ARRAY_SIZE(lxhs); i++) { + if (!lxhs[i]) + continue; + + if (lxhs[i] < msg->head || lxhs[i] > msg->head + msg->data_len) { + nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, + "(L%d out of range) ", i+1); + } else if (lxhs[i] <= msg->data + msg->data_len && + lxhs[i] > msg->tail) { + nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs, + "(L%d=tail%+d) ", + i+1, lxhs[i] - msg->tail); + } else + continue; + + if (nchars < 0 || nchars + buf_offs >= sizeof(buf)) + return "ERROR"; + buf_offs += nchars; + } -out_of_range: - nchars = snprintf(buf, sizeof(buf) - buf_offs, - "!!! L%d out of range", i+1); return buf; }