From patchwork Thu Sep 8 14:03:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Petrescu X-Patchwork-Id: 604070 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B6E1C38145 for ; Thu, 8 Sep 2022 14:03:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230204AbiIHODX (ORCPT ); Thu, 8 Sep 2022 10:03:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231752AbiIHODV (ORCPT ); Thu, 8 Sep 2022 10:03:21 -0400 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A4698C462 for ; Thu, 8 Sep 2022 07:03:18 -0700 (PDT) Received: by mail-qv1-xf29.google.com with SMTP id ml1so7022924qvb.1 for ; Thu, 08 Sep 2022 07:03:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:user-agent:date:cc:to:from:subject:message-id:from:to :cc:subject:date; bh=zdRRrOYsheHEiqXbG/ac/5ZW26Zos6Yr2Z60sFlKj+E=; b=oBdHMZlgLcONwLkUbQRGTFiTjwDgWefQIUyhkL8eIh2dBP9j7FDYgc5p3z5Hfr4zlj 6umyKp8Yfpz4sGNvQzSR2nF2NxDfO3JCRN5aANRDtfJN+IPtpsOnoACXqkCKQpp7GyOR gmCw7YsKHJxHsNOEbUJNaDfnxK4Ao1IKI41M89ydPbwb3EgTfxmQ3j1/qy5pZ1YPXD1v fFrwGggF3wAMn7JBzQdaSHeDweuVKvvPXwsUUy6nl+GpIq8lNDEy6bZwDuLc9TopRoBG HXE3s7tWRp3Ctbaf9+G8j5UeGD8F2mE4hrK9xe4Gy86yPUs+ERyDcvz5LfCZcMwX4lsI P1+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:date:cc:to:from:subject:message-id :x-gm-message-state:from:to:cc:subject:date; bh=zdRRrOYsheHEiqXbG/ac/5ZW26Zos6Yr2Z60sFlKj+E=; b=r7oqM5Rp/FpTRQgj1bJ9c9l8FnrEjFUZZjL9C0x1Xx3P7BpRrRypfMmJ98n2bwFhDi c4iO0sIjcPMw3l0FVZBwlOzd2tCUJptHAhh4X/xebKPss/hspx8powpood7K6PrOlcJi xtEy3ZUGiNiNdhUJCKGYFkAq5S/xq1AoB24TZRg8ufNecNiXwMEf5HfFejE4BodReIff /f9DIaqssI01y3LgPYSmj3p9cPms9uJrbfAAtZwNwBDU26RVES0wUoWkn4x7PMppfu/5 ePrJv1Uf617HWZI5qTCZaeM5wO/RQj9ToZdAtvlVOh3Wss0Rf4HXp57stYtOwjx/4CWq ZA3w== X-Gm-Message-State: ACgBeo3FVLrmA1LPJhuaZUSRsZk67MfQ5V27mrmspHeTy1E9zPj+njRZ MnLzk2hByJr5GU/q880pTE2Q64DUpEY= X-Google-Smtp-Source: AA6agR5NZ6hgDy5hCpfWlk8b04DHIDxJuKtx3/uOy4ISb1Em4+jXeMM8jt+HaA2zF9d9y4gu8r2YdQ== X-Received: by 2002:a05:6214:19c7:b0:4a6:765e:d2fd with SMTP id j7-20020a05621419c700b004a6765ed2fdmr7437122qvc.72.1662645796903; Thu, 08 Sep 2022 07:03:16 -0700 (PDT) Received: from asus.cpetrescu.net (cpe-98-10-48-140.rochester.res.rr.com. [98.10.48.140]) by smtp.gmail.com with ESMTPSA id d16-20020ac86150000000b0033a5048464fsm15521732qtm.11.2022.09.08.07.03.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 07:03:14 -0700 (PDT) Message-ID: Subject: [PATCH] tools: Change asprintf return code check From: Catalin Petrescu To: linux-gpio@vger.kernel.org Cc: Bartosz Golaszewski Date: Thu, 08 Sep 2022 10:03:13 -0400 User-Agent: Evolution 3.44.1-0ubuntu1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Hi there, I found a small error (IMHO) in libgpiod. The issue is that asprintf may return -1 if it fails to allocate memory, and if that happens, chip_open_by_number will pass a NULL pointer to gpiod_chip_open. I hope this helps. Thanks, Catalin. >From e92b71df3c9f8304a91e0dbe0094614df3784941 Mon Sep 17 00:00:00 2001 From: Catalin Petrescu Date: Tue, 6 Sep 2022 19:07:25 -0400 Subject: [PATCH] tools: Change asprintf return code check Asprintf may return negative numbers in some cases (failure to allocate memory, for example). Treat negative values as errors. --- tools/tools-common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tools-common.c b/tools/tools-common.c index 80087ee..8521c93 100644 --- a/tools/tools-common.c +++ b/tools/tools-common.c @@ -137,7 +137,7 @@ static struct gpiod_chip *chip_open_by_number(unsigned int num) int ret; ret = asprintf(&path, "/dev/gpiochip%u", num); - if (!ret) + if (ret <= 0) return NULL; chip = gpiod_chip_open(path); -- 2.34.1