From patchwork Fri Jul 14 18:28:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Venekamp X-Patchwork-Id: 1808012 X-Patchwork-Delegate: hauke@hauke-m.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=3NCFgBBo; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R2g5q6XKHz20bh for ; Sat, 15 Jul 2023 04:30:55 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=OCloSuOiXrdUX9d9O4Q+n/2Ywhl+S1ik4mSw+2bA470=; b=3NCFgBBolpM37N 87Nyr18mdT2kOxRNO1V25MXi8Evp3BxuKhZVo9+PB8ITx7DpSHpNcYlhI8gqcpc82YgETH/iTF4IM HyaDFuTv5EjVruQmitqKXO1M5aKQ3GpZGyTHTI38jMHjEoYoM2s7y+/Zf4eKrgC/uvJyxF8lTFS/a Vmk5Xw1/eKUjIk5Ws0TEkLHcjU7YCWihHv4DxJaOHjbtZHMbbwcTHqyNWOm2dTYtoTgnoqtChqYzm rucqe504x8GAeNN+9pizpxlp+X1ghab37KpLFFTPL8GJ+jMjERDyABrd3nyNym3cXpq04yK6LvnRg sPxCxSvs31a6IR19o63w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qKNWh-006xl5-22; Fri, 14 Jul 2023 18:28:23 +0000 Received: from virt1.bvwebdesign.nl ([149.210.228.112]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qKNWa-006xiM-2W for openwrt-devel@lists.openwrt.org; Fri, 14 Jul 2023 18:28:18 +0000 Received: from localhost.localdomain (83-82-122-37.cable.dynamic.v4.ziggo.nl [83.82.122.37]) by virt1.bvwebdesign.nl (Postfix) with ESMTPSA id B753FA87219 for ; Fri, 14 Jul 2023 20:28:11 +0200 (CEST) From: Jan Venekamp To: openwrt-devel@lists.openwrt.org Subject: [PATCH uci 1/2] file: Fix uci -m import command Date: Fri, 14 Jul 2023 20:28:10 +0200 Message-Id: <20230714182811.20444-2-jan@venekamp.net> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20230714182811.20444-1-jan@venekamp.net> References: <20230714182811.20444-1-jan@venekamp.net> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230714_112816_975186_9F80340A X-CRM114-Status: GOOD ( 12.29 ) X-Spam-Score: -0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Without this change we see the following error: # uci -m import optic < /etc/optic-db/default uci: Parse error (option/list command found before the first section) at line 4, byte 1 ptr.last is still a null pointer in case the uci_lookup_list() call found a matching section and set ptr.s to it. The code expects that uci_set() updates the ptr.last pointer, but this is not done any [...] Content analysis details: (-0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Without this change we see the following error: # uci -m import optic < /etc/optic-db/default uci: Parse error (option/list command found before the first section) at line 4, byte 1 ptr.last is still a null pointer in case the uci_lookup_list() call found a matching section and set ptr.s to it. The code expects that uci_set() updates the ptr.last pointer, but this is not done any more. If case uci_lookup_list() did not found a section ptr->s is a null pointer and then uci_set() will allocate a new section. Fixes: ae61e1cad4a1 ("uci: optimize update section in uci_set") Co-authored-by: Hauke Mehrtens Signed-off-by: Jan Venekamp --- file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/file.c b/file.c index 93abfae..6610f53 100644 --- a/file.c +++ b/file.c @@ -459,7 +459,7 @@ static void uci_parse_config(struct uci_context *ctx) ctx->internal = !pctx->merge; UCI_NESTED(uci_set, ctx, &ptr); - pctx->section = uci_to_section(ptr.last); + pctx->section = ptr.s; } } From patchwork Fri Jul 14 18:28:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Venekamp X-Patchwork-Id: 1808013 X-Patchwork-Delegate: hauke@hauke-m.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=BnL5LeRA; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R2g604vCrz20bh for ; Sat, 15 Jul 2023 04:31:04 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=GDbpnEyprDt4fPkE2cIG2C7poSURxGyOoRjhlvXBnrs=; b=BnL5LeRAGArlc/ QdHKoKwoOcTejaNuOKrQ81s6uPeCo7JYAVIhU/fSfPdHiozs6hwYTBmL7bxEbBieKOMaEDD2c5Cda cLEjxSNBMgqVX4cVCBtU9yztt7lbGgO/lIG0XzS2VPnEbUO+7lRwz74ekZySE6HVR0PLXsSfuE60S BYbBJUheSbf+sZZnkkivcogRnkFKFu6dXtDAvkU6CEL8JoOv/1cXDMggnVj0idrC5UdnwiIgdsV/F +AzY+grkANmggJRqaaB8x85YOEEPzyZ+XtTBDA/TKlftN1TUqOMdJrYM9HsWtb49NWDurWXK+rN4h Shcwjrgk/LMfdGkeRjqw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qKNWi-006xlR-31; Fri, 14 Jul 2023 18:28:24 +0000 Received: from virt1.bvwebdesign.nl ([149.210.228.112]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qKNWa-006xiO-2l for openwrt-devel@lists.openwrt.org; Fri, 14 Jul 2023 18:28:20 +0000 Received: from localhost.localdomain (83-82-122-37.cable.dynamic.v4.ziggo.nl [83.82.122.37]) by virt1.bvwebdesign.nl (Postfix) with ESMTPSA id D039CA8721E for ; Fri, 14 Jul 2023 20:28:11 +0200 (CEST) From: Jan Venekamp To: openwrt-devel@lists.openwrt.org Subject: [PATCH uci 2/2] remove internal usage of redundant uci_ptr.last Date: Fri, 14 Jul 2023 20:28:11 +0200 Message-Id: <20230714182811.20444-3-jan@venekamp.net> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20230714182811.20444-1-jan@venekamp.net> References: <20230714182811.20444-1-jan@venekamp.net> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230714_112817_161314_93908059 X-CRM114-Status: GOOD ( 18.58 ) X-Spam-Score: -0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: In uci_lookup_ptr and uci_set the pointer uci_ptr ptr.last is set to the element corresponding to the first of: ptr.o, ptr.s, ptr.p. Thus, ptr.last is redundant and in case of uci_set is (and was) not always consistently set. Content analysis details: (-0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org In uci_lookup_ptr and uci_set the pointer uci_ptr ptr.last is set to the element corresponding to the first of: ptr.o, ptr.s, ptr.p. Thus, ptr.last is redundant and in case of uci_set is (and was) not always consistently set. In order to simplify the code this commit removes internal usage of ptr.last, and remove setting it from uci_set (and from uci_add_list that was never used anyway). As it is part of the public C api ptr.last cannot be completely removed though. A search on lxr.openwrt.org shows that it is used as the output of uci_lookup_ptr in several packages. So we leave setting ptr.last in uci_lookup_ptr intact. Signed-off-by: Jan Venekamp --- cli.c | 39 +++++++++++---------------------------- delta.c | 10 ++++++---- list.c | 6 ------ lua/uci.c | 42 +++++++++++++++--------------------------- 4 files changed, 32 insertions(+), 65 deletions(-) diff --git a/cli.c b/cli.c index b4c4f95..f169e42 100644 --- a/cli.c +++ b/cli.c @@ -314,7 +314,6 @@ static void uci_show_changes(struct uci_package *p) static int package_cmd(int cmd, char *tuple) { - struct uci_element *e = NULL; struct uci_ptr ptr; int ret = 1; @@ -323,7 +322,6 @@ static int package_cmd(int cmd, char *tuple) return 1; } - e = ptr.last; switch(cmd) { case CMD_CHANGES: uci_show_changes(ptr.p); @@ -349,20 +347,14 @@ static int package_cmd(int cmd, char *tuple) cli_perror(); goto out; } - switch(e->type) { - case UCI_TYPE_PACKAGE: - uci_show_package(ptr.p); - break; - case UCI_TYPE_SECTION: - uci_show_section(ptr.s); - break; - case UCI_TYPE_OPTION: - uci_show_option(ptr.o, true); - break; - default: - /* should not happen */ - goto out; - } + if (ptr.o) + uci_show_option(ptr.o, true); + else if (ptr.s) + uci_show_section(ptr.s); + else if (ptr.p) + uci_show_package(ptr.p); + else + goto out; /* should not happen */ break; } @@ -475,7 +467,6 @@ done: static int uci_do_section_cmd(int cmd, int argc, char **argv) { - struct uci_element *e; struct uci_ptr ptr; int ret = UCI_OK; int dummy; @@ -493,7 +484,6 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv) (cmd != CMD_RENAME) && (cmd != CMD_REORDER)) return 1; - e = ptr.last; switch(cmd) { case CMD_GET: if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) { @@ -501,17 +491,10 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv) cli_perror(); return 1; } - switch(e->type) { - case UCI_TYPE_SECTION: - printf("%s\n", ptr.s->type); - break; - case UCI_TYPE_OPTION: + if (ptr.o) uci_show_value(ptr.o, false); - break; - default: - break; - } - /* throw the value to stdout */ + else if (ptr.s) + printf("%s\n", ptr.s->type); break; case CMD_RENAME: ret = uci_rename(ctx, &ptr); diff --git a/delta.c b/delta.c index 85ec970..5437fc1 100644 --- a/delta.c +++ b/delta.c @@ -213,7 +213,6 @@ error: static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p) { - struct uci_element *e = NULL; struct uci_ptr ptr; int cmd; @@ -244,11 +243,14 @@ static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p) UCI_INTERNAL(uci_del_list, ctx, &ptr); break; case UCI_CMD_ADD: + UCI_INTERNAL(uci_set, ctx, &ptr); + if (!ptr.option && ptr.s) + ptr.s->anonymous = true; + break; case UCI_CMD_CHANGE: UCI_INTERNAL(uci_set, ctx, &ptr); - e = ptr.last; - if (!ptr.option && e && (cmd == UCI_CMD_ADD)) - uci_to_section(e)->anonymous = true; + break; + default: break; } return; diff --git a/list.c b/list.c index 1640213..304c9e1 100644 --- a/list.c +++ b/list.c @@ -616,7 +616,6 @@ int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr) UCI_TRAP_SAVE(ctx, error); ptr->o = uci_alloc_list(ptr->s, ptr->option, NULL); UCI_TRAP_RESTORE(ctx); - ptr->last = &ptr->o->e; } else if (ptr->o->type == UCI_TYPE_STRING) { /* create new list and add old string value as item to list */ struct uci_option *old = ptr->o; @@ -630,7 +629,6 @@ int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr) if (ptr->option == old->e.name) ptr->option = ptr->o->e.name; uci_free_option(old); - ptr->last = &ptr->o->e; } /* add new item to list */ @@ -708,10 +706,8 @@ int uci_set(struct uci_context *ctx, struct uci_ptr *ptr) return uci_delete(ctx, ptr); } else if (!ptr->o && ptr->option) { /* new option */ ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value, NULL); - ptr->last = &ptr->o->e; } else if (!ptr->s && ptr->section) { /* new section */ ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section, NULL); - ptr->last = &ptr->s->e; } else if (ptr->o && ptr->option) { /* update option */ if (ptr->o->type == UCI_TYPE_STRING && !strcmp(ptr->o->v.string, ptr->value)) return 0; @@ -724,7 +720,6 @@ int uci_set(struct uci_context *ctx, struct uci_ptr *ptr) if (ptr->option == old->e.name) ptr->option = ptr->o->e.name; uci_free_option(old); - ptr->last = &ptr->o->e; } } else if (ptr->s && ptr->section) { /* update section */ if (!strcmp(ptr->s->type, ptr->value)) @@ -740,7 +735,6 @@ int uci_set(struct uci_context *ctx, struct uci_ptr *ptr) ptr->section = ptr->s->e.name; uci_free_section(old); ptr->s->package->n_section--; - ptr->last = &ptr->s->e; } } else { UCI_THROW(ctx, UCI_ERR_INVAL); diff --git a/lua/uci.c b/lua/uci.c index 196a25b..78b5e1f 100644 --- a/lua/uci.c +++ b/lua/uci.c @@ -374,19 +374,16 @@ static int uci_lua_get_any(lua_State *L, bool all) { struct uci_context *ctx; - struct uci_element *e = NULL; struct uci_ptr ptr; int offset = 0; int nret = 1; char *s = NULL; - int err = UCI_ERR_NOTFOUND; ctx = find_context(L, &offset); if (lookup_args(L, ctx, offset, &ptr, &s)) goto error; - lookup_ptr(ctx, &ptr, NULL, true); if (!all && !ptr.s) { ctx->err = UCI_ERR_INVAL; goto error; @@ -396,33 +393,24 @@ uci_lua_get_any(lua_State *L, bool all) goto error; } - err = UCI_OK; - e = ptr.last; - switch(e->type) { - case UCI_TYPE_PACKAGE: - uci_push_package(L, ptr.p); - break; - case UCI_TYPE_SECTION: - if (all) { - uci_push_section(L, ptr.s, -1); - } - else { - lua_pushstring(L, ptr.s->type); - lua_pushstring(L, ptr.s->e.name); - nret++; - } - break; - case UCI_TYPE_OPTION: - uci_push_option(L, ptr.o); - break; - default: - ctx->err = UCI_ERR_INVAL; - goto error; + if (ptr.o) { + uci_push_option(L, ptr.o); + } else if (ptr.s) { + if (all) { + uci_push_section(L, ptr.s, -1); + } + else { + lua_pushstring(L, ptr.s->type); + lua_pushstring(L, ptr.s->e.name); + nret++; + } + } else { + uci_push_package(L, ptr.p); } + if (s) free(s); - if (!err) - return nret; + return nret; error: if (s)