From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BF8EC11F64 for ; Thu, 1 Jul 2021 06:20:34 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0C5AE6148E for ; Thu, 1 Jul 2021 06:20:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0C5AE6148E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id DCA9E832C0; Thu, 1 Jul 2021 08:17:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="XTR0i9KP"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 46CB083298; Thu, 1 Jul 2021 08:17:08 +0200 (CEST) Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 830D483281 for ; Thu, 1 Jul 2021 08:16:33 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qk1-x736.google.com with SMTP id bj15so4973002qkb.11 for ; Wed, 30 Jun 2021 23:16:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aPwHpYL1pzStz7vZLKt4IBJ3Xaduazk98qn6MPvYRgI=; b=XTR0i9KPSNIoqrUBq/QW+RWY+hvv+Xj2eDjo/vTyZnUxHjrglx9smnsivlK8IjG5zM dc+qYDcm7o0hj4Et3vLFxOZ/JoRzQXM4dJDf8kV39BCiHJb7z3uJnbJaUld8Cmkd5xDr GT3K9AlG84Nbhx6VLAl8lj9uUBX70s8ie8ixMARNmVrT/oXdSWjfNUjXj1dmT5iiUzdh PfKrydaF+qm+3jeAsz/99jknuT16h20d+RUR5nvR3mi2AaaulQ7wBXrfQ5hE/z0SjVBk kVsmIjeB6VsMpcG717fpGsgjHxPZ745QdgRaFXINxKVBr4R2rPHP45epgEVJYIq95g2Y DpWg== 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=aPwHpYL1pzStz7vZLKt4IBJ3Xaduazk98qn6MPvYRgI=; b=MMQyey22lUAbHQFy538+jlIhUVs65hLVYGvrzESUcuJqKnOLpNT9mwLO8Hhkx5OPt4 C2+UE47QrIFhqPyVT2ql34WSiuBzlHdoOpt8iw5qpoZsApNtQece5kZgx5sxlPtfp1ea UmzQjtLQ2Q4GBGbGDk+M2+BbyeFp2/nYro70FOco4imfTNa0KegOcGKn4Yy6TcEgGx01 Ehr9wFBL9q/5WHh137PmK8kgtBOKsJ8QhgokuGTMRr0S8Uine4odXHLxZe2s4SQ5Q97B XkRhRNAmTGePSIvps4Aak+8AQaTKLN24RvjPhtFLI8OM/vYtzGw3r5yiDIKNxJiAzinD aHdA== X-Gm-Message-State: AOAM532yqsdKx3v6M5NUFp6p9IC8D/g7YL/jA3sTpXBgGt3qWdj54Kpe FTWbU4K3/twD2Zkl/Vvgiu2OJoimY1A= X-Google-Smtp-Source: ABdhPJwGXIFQlVgfeQbA+rzRtE9guP8QLdtXGsqU3Jxa5rJX7RUp1lwaOCBjsvy7NmHp8voYqx1Exg== X-Received: by 2002:a37:8345:: with SMTP id f66mr40059449qkd.396.1625120192091; Wed, 30 Jun 2021 23:16:32 -0700 (PDT) Received: from godwin.fios-router.home (pool-74-96-87-9.washdc.fios.verizon.net. [74.96.87.9]) by smtp.gmail.com with ESMTPSA id g21sm1684673qts.90.2021.06.30.23.16.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 23:16:31 -0700 (PDT) From: Sean Anderson To: u-boot@lists.denx.de, Tom Rini Cc: =?UTF-8?q?Marek=20Beh=C3=BAn?= , Wolfgang Denk , Simon Glass , Roland Gaudig , Heinrich Schuchardt , Kostas Michalopoulos , Sean Anderson Subject: [RFC PATCH 23/28] cli: lil: Handle OOM for hm_put Date: Thu, 1 Jul 2021 02:16:06 -0400 Message-Id: <20210701061611.957918-24-seanga2@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210701061611.957918-1-seanga2@gmail.com> References: <20210701061611.957918-1-seanga2@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean hm_put allocates memory, and this can fail. Instead of failing silently, return an error code. This also fixes up callers to handle this error. Signed-off-by: Sean Anderson --- common/cli_lil.c | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/common/cli_lil.c b/common/cli_lil.c index 2ed96ebc2d..7ec73675f3 100644 --- a/common/cli_lil.c +++ b/common/cli_lil.c @@ -326,22 +326,30 @@ static void hm_destroy(struct hashmap *hm) } } -static void hm_put(struct hashmap *hm, const char *key, void *value) +static enum lil_error hm_put(struct hashmap *hm, const char *key, void *value) { struct hashcell *cell = hm->cell + (hm_hash(key) & HASHMAP_CELLMASK); + struct hashentry *newe; size_t i; for (i = 0; i < cell->c; i++) { if (!strcmp(key, cell->e[i].k)) { cell->e[i].v = value; - return; + return LIL_ERR_NONE; } } - cell->e = realloc(cell->e, sizeof(struct hashentry) * (cell->c + 1)); - cell->e[cell->c].k = strdup(key); - cell->e[cell->c].v = value; + newe = realloc(cell->e, sizeof(struct hashentry) * (cell->c + 1)); + if (!newe) + return LIL_ERR_OOM; + cell->e = newe; + + newe[cell->c].k = strdup(key); + if (!newe[cell->c].k) + return LIL_ERR_OOM; + newe[cell->c].v = value; cell->c++; + return LIL_ERR_NONE; } static void *hm_get(struct hashmap *hm, const char *key) @@ -738,19 +746,24 @@ static struct lil_func *add_func(struct lil *lil, const char *name) cmd = calloc(1, sizeof(struct lil_func)); if (!cmd) - return NULL; + goto oom; cmd->name = strdup(name); ncmd = realloc(lil->cmd, sizeof(struct lil_func *) * (lil->cmds + 1)); - if (!ncmd) { - free(cmd); - return NULL; - } - + if (!ncmd) + goto oom; lil->cmd = ncmd; + ncmd[lil->cmds++] = cmd; - hm_put(&lil->cmdmap, name, cmd); + if (hm_put(&lil->cmdmap, name, cmd)) + goto oom; + return cmd; + +oom: + free(cmd); + lil_set_error_oom(lil); + return NULL; } static void del_func(struct lil *lil, struct lil_func *cmd) @@ -766,7 +779,11 @@ static void del_func(struct lil *lil, struct lil_func *cmd) if (index == lil->cmds) return; - hm_put(&lil->cmdmap, cmd->name, 0); + /* + * The only way this fails is if we don't find the command; this + * means our caller wants to delete a command which doesn't exist + */ + assert(!hm_put(&lil->cmdmap, cmd->name, NULL)); if (cmd->argnames) lil_free_list(cmd->argnames); @@ -783,9 +800,9 @@ int lil_register(struct lil *lil, const char *name, lil_func_proc_t proc) struct lil_func *cmd = add_func(lil, name); if (!cmd) - return 0; + return LIL_ERR_OOM; cmd->proc = proc; - return 1; + return LIL_ERR_NONE; } struct lil_var *lil_set_var(struct lil *lil, const char *name, -- 2.32.0