From patchwork Mon Mar 19 21:18:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Clark X-Patchwork-Id: 10295309 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 011E560385 for ; Mon, 19 Mar 2018 21:38:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E4593262F2 for ; Mon, 19 Mar 2018 21:38:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D94FF29449; Mon, 19 Mar 2018 21:38:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 71147262F2 for ; Mon, 19 Mar 2018 21:38:05 +0000 (UTC) Received: from localhost ([::1]:44085 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ey2Tk-0000PP-MX for patchwork-qemu-devel@patchwork.kernel.org; Mon, 19 Mar 2018 17:38:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33455) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ey2CM-0001aU-Gl for qemu-devel@nongnu.org; Mon, 19 Mar 2018 17:20:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ey2CL-0002py-Gh for qemu-devel@nongnu.org; Mon, 19 Mar 2018 17:20:06 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:39603) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ey2CL-0002pJ-B4 for qemu-devel@nongnu.org; Mon, 19 Mar 2018 17:20:05 -0400 Received: by mail-pf0-x244.google.com with SMTP id u5so7555974pfh.6 for ; Mon, 19 Mar 2018 14:20:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=B2G8KPDqqrNUnAweq8z8F0If+xUTQBerYi1HFa2FLrw=; b=HLShSUzJVUU0RFwiXUxiiK5KB73grhRYKeUpdNt31hII3OvjedYkXwrNMk1hf5yq2d RFSTRg51ypY29aGFkSs8+h9OHpvXgx46VL0qZWaksmpZMKpyowoxf1NU0wS/GGoA1SE3 7mfZrLU4EM3pP2H1U1qrli2ALbWpgsuPpU0LtM78xzH7Ytkb+jeKEsf4O0vDi+GISl/j vk4AboOblIxnKamqwSPJTFt++6dFxk1nhcMKoucFIZqu4QHO6hDh0VdhXq4/VCK6Y31y M39eFkHJnYZKdjb9y+NU9ugaaRZcchyD5w5fQDBbrhe7MxPmFvK0ajw+wXHSWxcCY2R0 /jpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=B2G8KPDqqrNUnAweq8z8F0If+xUTQBerYi1HFa2FLrw=; b=EyIn910zYEVMCCEFDfMJkRhQ0h1++FiCd9ZVaJShGtYcI8viIQnNjocEtayXwExi6B 9o4aKUSerlueFO9ut/SWgKMhohqP8Dfsyq9TuODxpNNVabXZQuhuRIJd41iYNYNyH6YC 9N5WJq3OK90FQc2TJ/AQMtoYNRJLbXT/NsjKWI09k7xyi4NOiGO0ocje2Iw+lN2V1AnT jvvv38KRV5aKEnVRu43vV2O/ugabEuMlB6+RxJ7Bxd6A0gAxLMZyVwz7Zi7gzjbUY1JY 7MXZ/oZC8/tPdkaRDt0FbOM/XBRXMswwBCxukix/KUVj8Mtq9PST1FLt+QFOaVv76KUn GMiA== X-Gm-Message-State: AElRT7FoYY+kB55broSv8i6ZVlpuBygPIWva62qeZW4kUHOfzv2GQLKc Z7QkJ516xYWH64VHTh4j7DWUq9/3heI= X-Google-Smtp-Source: AG47ELu+eFwf8RMF6mc6/pbscFS20VvP+IkfK7fwktl0C8gt8rmmiBh6385fo9fn5KgxsIEr9i2oKQ== X-Received: by 10.98.214.91 with SMTP id r88mr2123726pfg.8.1521494404440; Mon, 19 Mar 2018 14:20:04 -0700 (PDT) Received: from monty.com ([12.206.222.5]) by smtp.gmail.com with ESMTPSA id i127sm120500pfc.136.2018.03.19.14.20.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 19 Mar 2018 14:20:03 -0700 (PDT) From: Michael Clark To: qemu-devel@nongnu.org Date: Mon, 19 Mar 2018 14:18:49 -0700 Message-Id: <1521494329-19546-27-git-send-email-mjc@sifive.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1521494329-19546-1-git-send-email-mjc@sifive.com> References: <1521494329-19546-1-git-send-email-mjc@sifive.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::244 Subject: [Qemu-devel] [PATCH v4 26/26] RISC-V: Fix riscv_isa_string memory size bug X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: patches@groups.riscv.org, Michael Clark , Palmer Dabbelt , Peter Maydell Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This version uses a constant size memory buffer sized for the maximum possible ISA string length. It also uses g_new instead of g_new0, uses more efficient logic to append extensions and adds manual zero termination of the string. Cc: Palmer Dabbelt Cc: Peter Maydell Signed-off-by: Michael Clark Reviewed-by: Philippe Mathieu-Daudé --- target/riscv/cpu.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 1f25968..c82359f 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -360,16 +360,16 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data) char *riscv_isa_string(RISCVCPU *cpu) { int i; - size_t maxlen = 5 + ctz32(cpu->env.misa); - char *isa_string = g_new0(char, maxlen); - snprintf(isa_string, maxlen, "rv%d", TARGET_LONG_BITS); + const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1; + char *isa_str = g_new(char, maxlen); + char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS); for (i = 0; i < sizeof(riscv_exts); i++) { if (cpu->env.misa & RV(riscv_exts[i])) { - isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a'; - + *p++ = tolower(riscv_exts[i]); } } - return isa_string; + *p = '\0'; + return isa_str; } typedef struct RISCVCPUListState {