From patchwork Fri Mar 27 09:49:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 1262672 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=nvDaZKWs; dkim-atps=neutral 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 ozlabs.org (Postfix) with ESMTPS id 48pcY06F7Nz9sRR for ; Fri, 27 Mar 2020 20:50:28 +1100 (AEDT) Received: from localhost ([::1]:39180 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jHldC-0003U9-Oi for incoming@patchwork.ozlabs.org; Fri, 27 Mar 2020 05:50:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37823) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jHlcd-0003NO-GI for qemu-devel@nongnu.org; Fri, 27 Mar 2020 05:49:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jHlcc-0003iL-Jj for qemu-devel@nongnu.org; Fri, 27 Mar 2020 05:49:51 -0400 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]:52754) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jHlcc-0003gA-D8 for qemu-devel@nongnu.org; Fri, 27 Mar 2020 05:49:50 -0400 Received: by mail-wm1-x343.google.com with SMTP id z18so10746827wmk.2 for ; Fri, 27 Mar 2020 02:49:50 -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=OZ5+uHHTVpcB1oDwgp/gP+r+CwoI6bpOzQcnXUvRt7M=; b=nvDaZKWsPycaLMQ1RALZosCGJIKvemRNj59SqxKFKLnKx5ecNnh1lnaWABMhX4fFNj 9mh56d58nUUZrPR8BYWE9VbEDk2mGbOsG1ezrz0pbLATi/3/1aWVlytLFFbfc+wnsS7/ lR3EgpDX6+J4xh/8tIjGzmCAY9iJZcWfYpkL1Rl2707f7AbkKw0jHTtzHNftHB3LZ9OH JFITs9PZkea+q8bvt607m0Vp3Fo/P/yywcMy9nsiJzmNVYd8JGhHl3wNnfuo3asR3HSW ukRq/cIBqIC3mJFuJuTJLV5xc8FBOWMZaLNovcQEIZ7+mustAOZzR5TRW2E61ayMeveR HtIg== 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=OZ5+uHHTVpcB1oDwgp/gP+r+CwoI6bpOzQcnXUvRt7M=; b=CgqFeOBb1F9yNcl8Jn+Cuga70eStqdRuAxdakTq2Vd26pv1sDfqjZKw4YQGBpiNjjX K0UgznqmzWvKj9gB1o6IMDVJnFScJv0MRrvlxyV3jf1V47mxIXbTCGYg4TL9qabj1jb8 ZBY6XmLGFQwcn070oTOUfqufjKTGi7cC5tXk88DPmzQe7PxxtcNrbY9b/HXw52QQidWC pTvXZUVaP+cFdxR+IaysoLzIJ1d285AvFWMu58UUJvIqRfmNWbs7JQbS2itX51ss452N v7MfTLkP4sIQq+pnaMEYTQMeY9/y7OyG/qSKMLNxLCUQbvTw2jbkiUj9D3JU3K8HMOsW ZDnw== X-Gm-Message-State: ANhLgQ1K65wcXPcys2rY2WVjWwbaRIKqkyNSuYumGTHMQipj/Yy1de9a 4sFubf7v45spKw9+eNA4QrmrGrjTcNI= X-Google-Smtp-Source: ADFU+vuAcWwrS4ZRUCgnGd0L7d5mzvotLCKvp8zCW/3NXcGEJ/L0I3ns+3L0hgJ0ZAeYH/E+tRoaow== X-Received: by 2002:a1c:6285:: with SMTP id w127mr4720362wmb.133.1585302589326; Fri, 27 Mar 2020 02:49:49 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id c189sm7367293wmd.12.2020.03.27.02.49.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Mar 2020 02:49:46 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 78A731FF87; Fri, 27 Mar 2020 09:49:45 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v1 1/7] elf-ops: bail out if we have no function symbols Date: Fri, 27 Mar 2020 09:49:39 +0000 Message-Id: <20200327094945.23768-2-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200327094945.23768-1-alex.bennee@linaro.org> References: <20200327094945.23768-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::343 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" It's perfectly possible to have no function symbols in your elf file and if we do the undefined behaviour sanitizer rightly complains about us passing NULL to qsort. Check nsyms before we go ahead. Signed-off-by: Alex Bennée Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Richard Henderson --- include/hw/elf_ops.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h index a1411bfcab6..b5d4074d1e3 100644 --- a/include/hw/elf_ops.h +++ b/include/hw/elf_ops.h @@ -170,8 +170,13 @@ static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab, } i++; } - syms = g_realloc(syms, nsyms * sizeof(*syms)); + /* check we have symbols left */ + if (nsyms == 0) { + goto fail; + } + + syms = g_realloc(syms, nsyms * sizeof(*syms)); qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ)); for (i = 0; i < nsyms - 1; i++) { if (syms[i].st_size == 0) {