[PATCH v3 09/15] libfdt: overlay: Prepare fragment path helper for reuse

From: Douglas Anderson

Date: Sun Nov 01 2026 - 17:04:24 EST


Generalize `overlay_split_fragment_path()` in `libfdt/fdt_overlay.c` to
accept a length-bounded `path` slice that is not necessarily
NUL-terminated (using `memchr()` instead of `strchr()`, moving the
NUL-terminated property check back into `overlay_symbol_update()`, and
rejecting a trailing `'/'` after `"/__overlay__/"`) so it can also split
`:`-delimited paths from `/__fixups__` when updating
`/__local_fixups__`.

Assisted-by: Gemini:gemini-pro-prerelease
Signed-off-by: Douglas Anderson <dianders@xxxxxxxxxxxx>
---

Changes in v3:
- Move the fixup offset overflow fix into its own standalone patch at
the start of the series (Simon Glass)
- Move the removal of the redundant *sep != ':' checks into its own
standalone patch
- Reject paths ending with a trailing slash ("/<frag>/__overlay__/") in
overlay_split_fragment_path() (David Gibson)

Changes in v2:
- New patch generalizing overlay_split_fragment_path() for
non-NUL-terminated paths in libfdt/fdt_overlay.c

libfdt/fdt_overlay.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/libfdt/fdt_overlay.c b/libfdt/fdt_overlay.c
index 978c9c23bd56..36f2377cf7d8 100644
--- a/libfdt/fdt_overlay.c
+++ b/libfdt/fdt_overlay.c
@@ -944,8 +944,8 @@ static int get_path_len(const void *fdt, int nodeoffset)

/**
* overlay_split_fragment_path - Split "/<frag>/__overlay__[/<rel>]" into parts
- * @path: NUL-terminated input path string
- * @path_len: Length of @path in bytes (including terminating NUL)
+ * @path: Input path string (need not be NUL-terminated)
+ * @path_len: Length of @path in bytes (excluding any terminator)
* @frag_namep: Output pointer to the fragment name
* @frag_name_lenp: Output length of @frag_namep in bytes
* @rel_pathp: Output pointer to the relative subnode path after __overlay__
@@ -970,11 +970,11 @@ static int overlay_split_fragment_path(const char *path, int path_len,
/* keep end marker to avoid strlen() */
e = path + path_len;

- if (*path != '/')
+ if (path_len < 1 || *path != '/')
return -FDT_ERR_BADVALUE;

/* get fragment name first */
- s = strchr(path + 1, '/');
+ s = memchr(path + 1, '/', path_len - 1);
if (!s) {
/* Symbol refers to something that won't end
* up in the target tree */
@@ -984,13 +984,16 @@ static int overlay_split_fragment_path(const char *path, int path_len,
frag_name = path + 1;
frag_name_len = s - path - 1;

- /* verify format; safe since "s" lies in \0 terminated prop */
+ /* verify format */
len = sizeof("/__overlay__/") - 1;
if ((e - s) > len && (memcmp(s, "/__overlay__/", len) == 0)) {
/* /<fragment-name>/__overlay__/<relative-subnode-path> */
rel_path = s + len;
- rel_path_len = e - rel_path - 1;
- } else if ((e - s) == len
+ rel_path_len = e - rel_path;
+ } else if ((e - s) == len && (memcmp(s, "/__overlay__/", len) == 0)) {
+ /* Trailing slash after /__overlay__/ is invalid */
+ return -FDT_ERR_BADVALUE;
+ } else if ((e - s) == (len - 1)
&& (memcmp(s, "/__overlay__", len - 1) == 0)) {
/* /<fragment-name>/__overlay__ */
rel_path = "";
@@ -1062,7 +1065,7 @@ static int overlay_symbol_update(void *fdt, void *fdto)
if (path_len < 1 || memchr(path, '\0', path_len) != &path[path_len - 1])
return -FDT_ERR_BADVALUE;

- ret = overlay_split_fragment_path(path, path_len,
+ ret = overlay_split_fragment_path(path, path_len - 1,
&frag_name, &frag_name_len,
&rel_path, &rel_path_len);
if (ret < 0)
--
2.56.0.317.g454f8302e1-goog