From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 4E07F3857438 for ; Mon, 27 Feb 2023 21:29:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4E07F3857438 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677533386; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jrru08rXxCGvnisVoLreP+YEDQAHH4Kvr0gsUWaYByw=; b=ijxVaT99BT9B97fz1aCCA/YUaP2Xuj23/RTUQJQta+pcKOpKOGmqlj2rHJUtOZVyUF0cE8 xwpWMWaQF6Tnd9kbYJNDrXRbEjH3niqMCedYBzwn+HHSeaKhCOc6sC+3I7HbyZfLC6lg+R F5iy1tiXSpTnyjtbpRqblBinNqgpSSg= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-613-oZhEs-TFN36gmLgZEzrJ5Q-1; Mon, 27 Feb 2023 16:29:45 -0500 X-MC-Unique: oZhEs-TFN36gmLgZEzrJ5Q-1 Received: by mail-wm1-f72.google.com with SMTP id k26-20020a05600c0b5a00b003dfe4bae099so3441416wmr.0 for ; Mon, 27 Feb 2023 13:29:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677533384; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Jrru08rXxCGvnisVoLreP+YEDQAHH4Kvr0gsUWaYByw=; b=cEgrhDxL8pfy9uvDfcrRi28fs70rs4jC8LEkSg7tvgGlPBWktJ6BBI3hwiLk7OwQba 76n+P5KTYd7WZdtq1LIX+prqVgIxfqk6SZX2YG5Tys8ExsMxyoIdwwkPl+SHWW2U/8pn O9ms8UNkczxO6dFWXmf+oUZsJqZaRIv5SRygTvyEwL+RinEqPlh0Ny5Tm9iSBiZSS1wn 4w10Al2gSyWw79VxNro2yjdxu/d2DGVbyzl5h5ybiGlbmaIURqpK1Pr9qHgDpRPujNvj BJUzfYuPci4vjlJQg1m/LpA3FAQK8rapSh/+kp4Xd/eg3IxCyZ0FD/QQVULviAcRTlKd FwZg== X-Gm-Message-State: AO0yUKUBzI1knln68kB/GviqzWWYW6IdM6dRnzdEsVQfNxdFTmNqL3jE ln7/UjCZGWMoc4iTAviWvuzuoozVDIa5FSEhA6P91OpD74YaFI+WIBtNh8i+7OiWT49Ov/m4QOB 5jED0Gk85LmR0fvMYG29lF6G6ef6f3eI8ia+BGbhR+7b6HXxnqOsld76vcQ62jeT+KaUpJCDPUJ /iofY= X-Received: by 2002:a5d:4e51:0:b0:2c8:4bca:7fc2 with SMTP id r17-20020a5d4e51000000b002c84bca7fc2mr493356wrt.4.1677533384340; Mon, 27 Feb 2023 13:29:44 -0800 (PST) X-Google-Smtp-Source: AK7set/4zE1qcA9wmc+8Nc0/FAF0Rd5ugH0n/MfWYgMM3ZxrhbnIZhL423D0ZpbDq+jtS7eK4HR6qQ== X-Received: by 2002:a5d:4e51:0:b0:2c8:4bca:7fc2 with SMTP id r17-20020a5d4e51000000b002c84bca7fc2mr493343wrt.4.1677533383991; Mon, 27 Feb 2023 13:29:43 -0800 (PST) Received: from localhost (95.72.115.87.dyn.plus.net. [87.115.72.95]) by smtp.gmail.com with ESMTPSA id v14-20020adfedce000000b002c7b229b1basm8091017wro.15.2023.02.27.13.29.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 13:29:43 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 10/13] gdb: remove some uses of alloca from printcmd.c Date: Mon, 27 Feb 2023 21:29:23 +0000 Message-Id: <6329d414746b1b6fe6d5c32d9ab5c183b5719719.1677533215.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Remove a couple of uses of alloca from printcmd.c, and replace them with gdb::byte_vector. Unlike most times when alloca is replace with gdb::byte_vector, the size of the vector is not specified at the byte_vector creation time. In this case the vector can be sized differently depending on which path of an `if` is taken, however, the lifetime of the data within the vector must extend beyond the `if` itself. There is a remaining use of alloca in this file, but that case will not be replace with gdb::byte_vector, so I've left that for now. There should be no user visible changes after this commit. --- gdb/printcmd.c | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/gdb/printcmd.c b/gdb/printcmd.c index 0d3bc292d4e..1efd3cabf54 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -2443,7 +2443,7 @@ static void printf_c_string (struct ui_file *stream, const char *format, struct value *value) { - const gdb_byte *str; + gdb::byte_vector str; if (value->type ()->code () != TYPE_CODE_PTR && value->lval () == lval_internalvar @@ -2455,11 +2455,10 @@ printf_c_string (struct ui_file *stream, const char *format, character. This protects against corrupted C-style strings that lack the terminating null char. It also allows Ada-style strings (not null terminated) to be printed without problems. */ - gdb_byte *tem_str = (gdb_byte *) alloca (len + 1); + str.resize (len + 1); - memcpy (tem_str, value->contents ().data (), len); - tem_str [len] = 0; - str = tem_str; + memcpy (str.data (), value->contents ().data (), len); + str [len] = 0; } else { @@ -2474,31 +2473,30 @@ printf_c_string (struct ui_file *stream, const char *format, return; } - /* This is a %s argument. Find the length of the string. */ - size_t len; - - for (len = 0;; len++) + /* This is a %s argument. Build the string in STR which is + currently empty. */ + gdb_assert (str.size () == 0); + for (size_t len = 0;; len++) { gdb_byte c; QUIT; read_memory (tem + len, &c, 1); + str.push_back (c); if (c == 0) break; } - /* Copy the string contents into a string inside GDB. */ - gdb_byte *tem_str = (gdb_byte *) alloca (len + 1); - - if (len != 0) - read_memory (tem, tem_str, len); - tem_str[len] = 0; - str = tem_str; + /* We will have passed through the above loop at least once, and will + only exit the loop when we have pushed a zero byte onto the end of + STR. */ + gdb_assert (str.size () > 0); + gdb_assert (str.back () == 0); } DIAGNOSTIC_PUSH DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL - gdb_printf (stream, format, (char *) str); + gdb_printf (stream, format, (char *) str.data ()); DIAGNOSTIC_POP } @@ -2539,23 +2537,23 @@ printf_wide_c_string (struct ui_file *stream, const char *format, /* This is a %s argument. Find the length of the string. */ enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); - gdb_byte *buf = (gdb_byte *) alloca (wcwidth); + gdb::byte_vector buf (wcwidth); for (len = 0;; len += wcwidth) { QUIT; - read_memory (tem + len, buf, wcwidth); - if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0) + read_memory (tem + len, buf.data (), wcwidth); + if (extract_unsigned_integer (buf.data (), wcwidth, byte_order) == 0) break; } /* Copy the string contents into a string inside GDB. */ - gdb_byte *tem_str = (gdb_byte *) alloca (len + wcwidth); + gdb::byte_vector tem_str (len + wcwidth); if (len != 0) - read_memory (tem, tem_str, len); + read_memory (tem, tem_str.data (), len); memset (&tem_str[len], 0, wcwidth); - str = tem_str; + str = tem_str.data (); } auto_obstack output; -- 2.25.4