From patchwork Tue Jun 28 04:53:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 12897833 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AE5D3CCA47E for ; Tue, 28 Jun 2022 07:12:00 +0000 (UTC) Received: from localhost ([::1]:49758 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o65OB-0001eA-Id for qemu-devel@archiver.kernel.org; Tue, 28 Jun 2022 03:11:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43100) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o63F5-0002Lm-If for qemu-devel@nongnu.org; Tue, 28 Jun 2022 00:54:27 -0400 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]:53002) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o63F3-0008Is-QM for qemu-devel@nongnu.org; Tue, 28 Jun 2022 00:54:27 -0400 Received: by mail-pj1-x102a.google.com with SMTP id g7so6378232pjj.2 for ; Mon, 27 Jun 2022 21:54:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mnyz4RGMr9davoKlXgtFTdnnm6cHsKuNwuwlINalmn0=; b=H6JhjGgUAEzlI0amOJ4rxtH1w1wZeKsl8HQVy4ZB1r63+EJtEheRrzR17UZCPZ4aSe Ch5OgmsP6xafoc6d480BpDQuW43U13+dbaaeVWRkRjsMFvJ2knbaDXFmdZP9AmcqsKqu HfPagvQHyRpzFRsgIiSFNRWfifWhd+tO87rsn092Vjktj+pzJrQmwV84DfQIaZkJtoOF fiwy8VZWApiKe8pBdjHe4sgp/v4qRdk+UBe+yw2Q0/zeuYN0CCwuz7DPXiGSuC5DNJY4 pCyzfaWr8tnwiJ1Q1VaaiH+z2Ij8632dUTdQmA9e0UVCTA7tk5yQz2iUeAfgFHvfFSPH GEPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mnyz4RGMr9davoKlXgtFTdnnm6cHsKuNwuwlINalmn0=; b=d0AX1WlrcL2SHregalCP5UsSE31zUNW5pL/nGm0QBUEBFtoJsC/2W+ShLe2zTYwNG3 2HYeLuZedMndBxCEOgpe/ANA1lWR6Lm4M0D1p6dwzzFduWvle6Gr2c109t7ZsrnAbsJf vWmXkIDQMuP1PEftFN9NJZpGXUZ6JpXyAHiSqzTTTXIPXmbKAmmZlzuU739PrE1hABNO yrXYWReRmnB+H7iNrVX7ypBbR5hvSk1avYWpial650FRVOgHScBsoUb27eo++iS/va5O CgBUAhW5mjJQe08D8blEQOhLscfDWiBV2dlFKavHL7njc8mtaJRVr/nZaB/6c7ELLq80 wBdg== X-Gm-Message-State: AJIora+Mw2boHeOKtR9p4sv+MjGuoDO1M8L/kyhPt4dKuxCrIIppKCJp 8Vp+o6oYpd8mv5Lhsi9Hy5Ot4Qq+hb49EA== X-Google-Smtp-Source: AGRyM1vEgL3lrbjGVzYU/O8+CKT4jSsqVKonkvPSbkMjswHxb6D3htO2E3feRS5goiktnHL6yUMosg== X-Received: by 2002:a17:902:860b:b0:16b:7927:b62c with SMTP id f11-20020a170902860b00b0016b7927b62cmr3071500plo.95.1656392064516; Mon, 27 Jun 2022 21:54:24 -0700 (PDT) Received: from stoup.. ([122.255.60.245]) by smtp.gmail.com with ESMTPSA id w8-20020a1709026f0800b00168c523032fsm8073735plk.269.2022.06.27.21.54.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 21:54:24 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= , Peter Maydell Subject: [PULL 07/60] semihosting: Simplify softmmu_lock_user_string Date: Tue, 28 Jun 2022 10:23:10 +0530 Message-Id: <20220628045403.508716-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220628045403.508716-1-richard.henderson@linaro.org> References: <20220628045403.508716-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102a; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" We are not currently bounding the search to the 1024 bytes that we allocated, possibly overrunning the buffer. Use softmmu_strlen_user to find the length and allocate the correct size from the beginning. Reviewed-by: Alex Bennée Reviewed-by: Peter Maydell Signed-off-by: Richard Henderson --- semihosting/uaccess.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/semihosting/uaccess.c b/semihosting/uaccess.c index d6997e3c65..8018828069 100644 --- a/semihosting/uaccess.c +++ b/semihosting/uaccess.c @@ -74,20 +74,11 @@ ssize_t softmmu_strlen_user(CPUArchState *env, target_ulong addr) char *softmmu_lock_user_string(CPUArchState *env, target_ulong addr) { - /* TODO: Make this something that isn't fixed size. */ - char *s = malloc(1024); - size_t len = 0; - - if (!s) { + ssize_t len = softmmu_strlen_user(env, addr); + if (len < 0) { return NULL; } - do { - if (cpu_memory_rw_debug(env_cpu(env), addr++, s + len, 1, 0)) { - free(s); - return NULL; - } - } while (s[len++]); - return s; + return softmmu_lock_user(env, addr, len + 1, true); } void softmmu_unlock_user(CPUArchState *env, void *p,