From nobody Mon Feb 9 14:38:03 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620398612; cv=none; d=zohomail.com; s=zohoarc; b=MogCsqEG6y838tmzg7XvYXIwCA/5rPOIerVpYPmb1gMtjySAJvh5dg9wfxLXPQEsXCK3PlxS0w++eVPS0lXQ7M8QbrNLL4xMzTarMVPkfKZMSekwl6vzW3Ahan9nrTMgOB+B9FrRRUAAeQkqVuXcSRPyQTAWPbm5CmeYcSOBnzM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620398612; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=cWe+dxIQxlwOjXTVq4nmSO21Ag5LIQeGVfXOgPjRmt8=; b=G0bPUYnKUGoA6e9Grh52iHzwR8HE4FmfhXZAAe/iT2KwZib0Si0AK1R8JEdl8LbALs0exfvYmfHlzERG3RRXcypIkIpWPQmKcj2Ml5+tQjg8kmzChkCSM8P8cUdza9aapdkGPd/kGIdX5QamcgCWsl81C2X3qvnKNRQZBoZmkRQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1620398612705319.48404337255795; Fri, 7 May 2021 07:43:32 -0700 (PDT) Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-393-vMgJDzk8OSGjoR-ylxsuJg-1; Fri, 07 May 2021 10:43:29 -0400 Received: by mail-wm1-f69.google.com with SMTP id u126-20020a1c60840000b029013f6df1cd5bso1487971wmb.0 for ; Fri, 07 May 2021 07:43:28 -0700 (PDT) Return-Path: Return-Path: Received: from localhost.localdomain (astrasbourg-652-1-219-60.w90-40.abo.wanadoo.fr. [90.40.114.60]) by smtp.gmail.com with ESMTPSA id o15sm8521863wru.42.2021.05.07.07.43.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 May 2021 07:43:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620398611; 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=cWe+dxIQxlwOjXTVq4nmSO21Ag5LIQeGVfXOgPjRmt8=; b=ZaB6gKevzPVzKBe3fJgPItBVYmUHv/bwN0gfdmm0dfg3tRtRd/R1l1o9zO3oT88Uy2AMvE ZwUNW4L9mhpu6QkIWk0o7qcUf63+fo3ZjHtuOGU4r3FFOZKzmjrI9XvOA8iN/fYZToI9EO /Pl48VEDcNb0G5bhepCWg7Scn6UTGBc= X-MC-Unique: vMgJDzk8OSGjoR-ylxsuJg-1 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=cWe+dxIQxlwOjXTVq4nmSO21Ag5LIQeGVfXOgPjRmt8=; b=g+z9eBhemjFUTULXFXSxS2ibCEyMuyjw9mM0VaD2Fs2bRwd6Zi3TVR2BkN7Qq5XxR/ y4aXahLS5qS4/4aFbmpQkpjQHVJXiywE3XnlLDOyLBdPjG5eL66oCxS12P4o+aBX1mCO 3mspnKsfbf5xFKDQ7IvXOyrD3jgLoRkaN+ZHgF1Fg7NSFSTmjddmVjiAOmLmb5piqud2 F/O9NVL/Wv/fkKfNd4GVZW8VRKG9yR5taJzVLK4V2e0XgX/ehYcEc2z3EQFngVTZiCPM u+112dAzZhOcJkM6LbZ/c5D178m5nQlvIwz27OJSBO/WTDqHx18KxbcwVJtq2UUY3lP5 GFFA== X-Gm-Message-State: AOAM530Vdlwh3VAo0HxGy2DM5ejmTrGs7XznP9qQ8aksugmVa/eSw4d0 3qieM3SDDRZ1QNppBFY00wnulixeuD/YmibfVk5MTy3wwi4zG+itZDYKX8bn5IPvfQUScR/v6bF 9aNbVtgAPDYatBg== X-Received: by 2002:a05:600c:293:: with SMTP id 19mr10481684wmk.144.1620398607269; Fri, 07 May 2021 07:43:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyzJXjh5EIUZ5UvBdcTDtVAODkhggy3oiheKgCV4ZKOSMg1GrJfTFeIVPOdUMHt/oXW6+6GrQ== X-Received: by 2002:a05:600c:293:: with SMTP id 19mr10481664wmk.144.1620398607116; Fri, 07 May 2021 07:43:27 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Laurent Vivier , Paolo Bonzini , qemu-ppc@nongnu.org, Peter Maydell , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Gerd Hoffmann , qemu-arm@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v3 02/17] linux-user/elfload: Replace alloca() by g_try_malloc() Date: Fri, 7 May 2021 16:43:00 +0200 Message-Id: <20210507144315.1994337-3-philmd@redhat.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20210507144315.1994337-1-philmd@redhat.com> References: <20210507144315.1994337-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The ALLOCA(3) man-page mentions its "use is discouraged". Use autofree heap allocation instead (returning ENOMEM on failure). Suggested-by: Peter Maydell Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- linux-user/elfload.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index c6731013fde..dad2dac24a4 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2591,6 +2591,7 @@ static void load_elf_image(const char *image_name, in= t image_fd, { struct elfhdr *ehdr =3D (struct elfhdr *)bprm_buf; struct elf_phdr *phdr; + g_autofree void *phdr_alloc =3D NULL; abi_ulong load_addr, load_bias, loaddr, hiaddr, error; int i, retval, prot_exec; Error *err =3D NULL; @@ -2610,7 +2611,12 @@ static void load_elf_image(const char *image_name, i= nt image_fd, if (ehdr->e_phoff + i <=3D BPRM_BUF_SIZE) { phdr =3D (struct elf_phdr *)(bprm_buf + ehdr->e_phoff); } else { - phdr =3D (struct elf_phdr *) alloca(i); + phdr_alloc =3D g_try_malloc(i); + if (!phdr_alloc) { + error_setg(&err, "Not enough memory to load ELF program header= "); + goto exit_errmsg; + } + phdr =3D (struct elf_phdr *) phdr_alloc; retval =3D pread(image_fd, phdr, i, ehdr->e_phoff); if (retval !=3D i) { goto exit_read; @@ -2979,15 +2985,15 @@ static void load_symbols(struct elfhdr *hdr, int fd= , abi_ulong load_bias) { int i, shnum, nsyms, sym_idx =3D 0, str_idx =3D 0; uint64_t segsz; - struct elf_shdr *shdr; + g_autofree struct elf_shdr *shdr; char *strings =3D NULL; struct syminfo *s =3D NULL; struct elf_sym *new_syms, *syms =3D NULL; =20 shnum =3D hdr->e_shnum; i =3D shnum * sizeof(struct elf_shdr); - shdr =3D (struct elf_shdr *)alloca(i); - if (pread(fd, shdr, i, hdr->e_shoff) !=3D i) { + shdr =3D (struct elf_shdr *)g_try_malloc(i); + if (shdr =3D=3D NULL || pread(fd, shdr, i, hdr->e_shoff) !=3D i) { return; } =20 --=20 2.26.3