From patchwork Tue Jun 12 06:04:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 928116 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="MDy7e38U"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 414ffd286kz9rxs for ; Tue, 12 Jun 2018 16:11:53 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id E1DBEC21E5B; Tue, 12 Jun 2018 06:09:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_MSPIKE_H2, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 04562C21DCA; Tue, 12 Jun 2018 06:06:21 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id D1C91C21D8E; Tue, 12 Jun 2018 06:05:40 +0000 (UTC) Received: from mail-io0-f195.google.com (mail-io0-f195.google.com [209.85.223.195]) by lists.denx.de (Postfix) with ESMTPS id 2A7FAC21E2C for ; Tue, 12 Jun 2018 06:05:37 +0000 (UTC) Received: by mail-io0-f195.google.com with SMTP id r24-v6so26685011ioh.9 for ; Mon, 11 Jun 2018 23:05:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=o9k/AgIXQkfIYHcQqAf+3UffrIhY0tQhdtec1hrQUtc=; b=MDy7e38UGCo2F100U9xcE5AHHIwO7Z1cAxkbkeEgRRdfqhAwZdIVIMbEkgXOyh2Ffx edlCz9raBQhWZ7ZcRpgbLdp3Vma+/3hYp4R5fXW1deoldelf99NHa9aePdW6DG3Fsymy 0LjUm/jAFSlc0skyf9gBbjSjkFmHktyZoQt/8e60rt54iTgIXp8AuDKwC4kThlfeX5iR bIpqnNrja0M6KiUgngjprkYaYSCi3c6mWIu2p3VDPEigot3/9FFERl0YODHvCgD/32dM K4zzMHm/2uQmCtS0YBpxSADPCMJ7nweJ1bWTmVosqPeXMAEZg78e+8elsOI2InsOTJEZ q45w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=o9k/AgIXQkfIYHcQqAf+3UffrIhY0tQhdtec1hrQUtc=; b=po4F94gMrq9WkZFuoSAIulYBiSYc8FVp3Jf2RaD5k+RQ3TI2MBgF1OHbKGvA8YObDv hmSwoYpGVwRuuRqAMvnWSUl/SbxF8t8SV2hLrx/xdlWl8kumgPCsCz+LUPaO+kIEixPV Ul2ikq2AIb/Qtr1zFo3J/f2EY0L1a5j0rdg/ID7qENZlfQObqOi2vRJp0FJGLjDGRd0M R0lobaV0hBD7Lj09e7N5cERL7JZieA/z3C0E196k5bvJmKJsyzH0IcNVBA8Mvt2QIWJX AyRnCRwWq7iOd+2xzPKSySr2CmU/aJz89Q8/pZYoZ7rM9W7eGmhssAzhL+oCFqwPUXAQ jthg== X-Gm-Message-State: APt69E04Ej4fnTIu5bvq8zjSrMfjFlz3T3DHs20vhMI/fDgMjHxSq0rE ZdEF8U//XXyKXhDa8/eQ2qqJCA== X-Google-Smtp-Source: ADUXVKKus6lJSGXQcSNP1ENCGu16EhCBHV9vFCLwTKJ3VNntoa5kFmph+pbvYeJ052Dl/jqDVPZrWg== X-Received: by 2002:a5e:9805:: with SMTP id s5-v6mr1920187ioj.96.1528783535620; Mon, 11 Jun 2018 23:05:35 -0700 (PDT) Received: from kiwi.bld.corp.google.com ([100.67.80.24]) by smtp.gmail.com with ESMTPSA id o89-v6sm22194ioi.85.2018.06.11.23.05.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 11 Jun 2018 23:05:34 -0700 (PDT) Received: by kiwi.bld.corp.google.com (Postfix, from userid 121222) id D5FBA1403E9; Tue, 12 Jun 2018 00:05:33 -0600 (MDT) From: Simon Glass To: U-Boot Mailing List Date: Tue, 12 Jun 2018 00:04:59 -0600 Message-Id: <20180612060502.196817-6-sjg@chromium.org> X-Mailer: git-send-email 2.18.0.rc1.244.gcf134e6275-goog In-Reply-To: <20180612060502.196817-1-sjg@chromium.org> References: <20180612060502.196817-1-sjg@chromium.org> Cc: Heinrich Schuchardt , Tom Rini Subject: [U-Boot] [PATCH v2 5/8] fdtgrep: Separate out checking of two allocations X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" The current code might succeed on the first allocation and fail on the second. Separate the checks to avoid this problem. Of course, free() will never fail and the chances that (when allocating two small areas) one will succeed and one will fail are just as remote. But this keeps coverity happy. Reported-by: Coverity (CID: 131226) Signed-off-by: Simon Glass --- Changes in v2: None tools/fdtgrep.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/fdtgrep.c b/tools/fdtgrep.c index d9f6fb0740..99f955b629 100644 --- a/tools/fdtgrep.c +++ b/tools/fdtgrep.c @@ -133,11 +133,11 @@ static int value_add(struct display_info *disp, struct value_node **headp, } str = strdup(str); + if (!str) + goto err_mem; node = malloc(sizeof(*node)); - if (!str || !node) { - fprintf(stderr, "Out of memory\n"); - return -1; - } + if (!node) + goto err_mem; node->next = *headp; node->type = type; node->include = include; @@ -145,6 +145,9 @@ static int value_add(struct display_info *disp, struct value_node **headp, *headp = node; return 0; +err_mem: + fprintf(stderr, "Out of memory\n"); + return -1; } static bool util_is_printable_string(const void *data, int len)