ucode: backport uci.foreach fix

Signed-off-by: John Crispin <john@phrozen.org>
This commit is contained in:
John Crispin
2023-05-22 09:56:06 +02:00
parent 33c4c732c9
commit e8875a7375

View File

@@ -0,0 +1,58 @@
From d2cc00310dba05de233ff18d3c830c8638f9bfdf Mon Sep 17 00:00:00 2001
From: Jo-Philipp Wich <jo@mein.io>
Date: Wed, 23 Nov 2022 12:48:07 +0100
Subject: [PATCH] uci: auto-load package in `ctx.foreach()` and
`ctx.get_first()`
Functions that use `uci_lookup_ptr()` internally, such as `ctx.get()`,
`ctx.set()` or `ctx.delete()`, implicitly load the given configuration
name while the higher level functions `ctx.foreach()` or `ctx.get_first()`
do not.
This behaviour violates the principle of least surprise and might lead to
non-deterministic program behavior as the outcome of these functions
depends on prior uci operations performed on the cursor.
Fix this issue by invoking `uci_load()` internally in case the given
uci package name cannot be found in the cursor's package cache.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
---
lib/uci.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/lib/uci.c b/lib/uci.c
index 50cc050e..b88dabd1 100644
--- a/lib/uci.c
+++ b/lib/uci.c
@@ -346,8 +346,8 @@ uc_uci_get_first(uc_vm_t *vm, size_t nargs)
break;
}
- if (!p)
- err_return(UCI_ERR_NOTFOUND);
+ if (!p && uci_load(*c, ucv_string_get(conf), &p))
+ err_return((*c)->err);
uci_foreach_element(&p->sections, e) {
sc = uci_to_section(e);
@@ -917,8 +917,8 @@ uc_uci_foreach(uc_vm_t *vm, size_t nargs)
break;
}
- if (!p)
- err_return(UCI_ERR_NOTFOUND);
+ if (!p && uci_load(*c, ucv_string_get(conf), &p))
+ err_return((*c)->err);
uci_foreach_element_safe(&p->sections, tmp, e) {
sc = uci_to_section(e);
@@ -946,8 +946,6 @@ uc_uci_foreach(uc_vm_t *vm, size_t nargs)
break;
}
- /* XXX: rethrow */
-
return ucv_boolean_new(ret);
}