From patchwork Thu Nov 15 16:02:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 998418 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-490191-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="dKosNJhd"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42wmNw3N36z9s9G for ; Fri, 16 Nov 2018 03:03:11 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=ASBIj+G9Exx84Zr2jXAKEhL2TQmb1AZAGyFt9JFjsSeM2tBxMl k3SPEBnffOXo4jjSwbp27V0jgzOsYuXvfOjI49LmwOWakUyCA3CAAIgrG2yOAXQE GPzngtUjdnNEIq6cNysFMvbIlam7ZPz9v/Ua23l4rNvfoluhNUMW/w9u4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=0FUz7sAaJvWhPzRCjfJXNd/hroM=; b=dKosNJhdUz0RSt6Q5Cr2 9Hv32uaFv5P2yL1BFYP2bGtg/G2Uo+5/8Bm6B5a0MwWuvAlVLqbvh+0tFEvL8SCm KOtDQl6r7SRvxZlxoFsvYYu/Vn7lsMGSjzm+8RrWPrkrOdKC9tDcU8Pv4yq9lSPd F29zxoVKL68nDt5v9rsLvhs= Received: (qmail 78012 invoked by alias); 15 Nov 2018 16:03:03 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 77986 invoked by uid 89); 15 Nov 2018 16:03:02 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.2 spammy=H*Ad:U*ian, vries, Vries, 6948 X-HELO: mx1.suse.de Received: from mx2.suse.de (HELO mx1.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 15 Nov 2018 16:02:58 +0000 Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 4F89CB054; Thu, 15 Nov 2018 16:02:56 +0000 (UTC) Date: Thu, 15 Nov 2018 17:02:56 +0100 From: Tom de Vries To: gcc-patches@gcc.gnu.org Cc: Ian Lance Taylor Subject: [PATCH][libbacktrace] Factor out read_string Message-ID: <20181115160255.GA32417@delia> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-IsSubscribed: yes Hi, This patch factors out new function read_string in dwarf.c. Bootstrapped and reg-tested on x86_64. OK for trunk (or, for stage1)? Thanks, - Tom [libbacktrace] Factor out read_string 2018-11-15 Tom de Vries * dwarf.c (read_string): Factor out of ... (read_attribute, read_line_header, read_line_program): ... here. --- libbacktrace/dwarf.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/libbacktrace/dwarf.c b/libbacktrace/dwarf.c index 4566d37cf2f..c4f8732c7eb 100644 --- a/libbacktrace/dwarf.c +++ b/libbacktrace/dwarf.c @@ -411,6 +411,25 @@ advance (struct dwarf_buf *buf, size_t count) return 1; } +/* Read one zero-terminated string from BUF and advance past the string. */ + +static const char * +read_string (struct dwarf_buf *buf) +{ + const char *p = (const char *)buf->buf; + size_t len = strnlen (p, buf->left); + + /* - If len == left, we ran out of buffer before finding the zero terminator. + Generate an error by advancing len + 1. + - If len < left, advance by len + 1 to skip past the zero terminator. */ + size_t count = len + 1; + + if (!advance (buf, count)) + return NULL; + + return p; +} + /* Read one byte from BUF and advance 1 byte. */ static unsigned char @@ -694,8 +713,8 @@ read_attribute (enum dwarf_form form, struct dwarf_buf *buf, return 1; case DW_FORM_string: val->encoding = ATTR_VAL_STRING; - val->u.string = (const char *) buf->buf; - return advance (buf, strnlen ((const char *) buf->buf, buf->left) + 1); + val->u.string = read_string (buf); + return val->u.string == NULL ? 0 : 1; case DW_FORM_block: val->encoding = ATTR_VAL_BLOCK; return advance (buf, read_uleb128 (buf)); @@ -1649,11 +1668,10 @@ read_line_header (struct backtrace_state *state, struct unit *u, if (hdr_buf.reported_underflow) return 0; - hdr->dirs[i] = (const char *) hdr_buf.buf; - ++i; - if (!advance (&hdr_buf, - strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1)) + hdr->dirs[i] = read_string (&hdr_buf); + if (hdr->dirs[i] == NULL) return 0; + ++i; } if (!advance (&hdr_buf, 1)) return 0; @@ -1687,9 +1705,8 @@ read_line_header (struct backtrace_state *state, struct unit *u, if (hdr_buf.reported_underflow) return 0; - filename = (const char *) hdr_buf.buf; - if (!advance (&hdr_buf, - strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1)) + filename = read_string (&hdr_buf); + if (filename == NULL) return 0; dir_index = read_uleb128 (&hdr_buf); if (IS_ABSOLUTE_PATH (filename) @@ -1808,8 +1825,8 @@ read_line_program (struct backtrace_state *state, struct dwarf_data *ddata, const char *f; unsigned int dir_index; - f = (const char *) line_buf->buf; - if (!advance (line_buf, strnlen (f, line_buf->left) + 1)) + f = read_string (line_buf); + if (f == NULL) return 0; dir_index = read_uleb128 (line_buf); /* Ignore that time and length. */